๐Ÿ“ฆ kitten / multitars

๐Ÿ“„ tarInput.bench.ts ยท 92 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92import { describe, bench, afterAll } from 'vitest';
import { Readable } from 'node:stream';
import { generate, cleanup } from './fixtures/generateTars';

import * as multitars from './fixtures/lib';
import * as modernTar from 'modern-tar';
import * as tarParser from '@remix-run/tar-parser';
import * as tarStream from 'tar-stream';
import * as tar from 'tar';

const files = await generate();

afterAll(async () => {
  await cleanup();
});

describe.each([
  { name: '2500 x 1KB', file: files.smallFiles },
  { name: '20 x 5MB', file: files.largeFiles },
  { name: 'nested 1KB', file: files.nestedFiles },
  { name: 'worker sample', file: files.workerSample },
])('untar ($name)', input => {
  bench('multitars', async () => {
    await runMultitars(input.file);
  });

  bench('modern-tar', async () => {
    await runModernTar(input.file);
  });

  bench('@remix-run/tar-parser', async () => {
    await runTarParser(input.file);
  });

  bench('tar', async () => {
    await runTar(input.file);
  });

  bench('tar-stream', async () => {
    await runTarStream(input.file);
  });
});

async function runMultitars(input: Buffer) {
  const blob = new Blob([input as any]);
  for await (const _ of multitars.untar(blob.stream())) {
    // noop
  }
}

async function runModernTar(input: Buffer) {
  const blob = new Blob([input as any]);
  for await (const _ of blob
    .stream()
    .pipeThrough(modernTar.createTarDecoder())) {
    // noop
  }
}

async function runTarParser(input: Buffer) {
  const blob = new Blob([input as any]);
  await tarParser.parseTar(blob.stream(), () => {});
}

async function runTar(input: Buffer) {
  await new Promise(resolve => {
    Readable.fromWeb(new Blob([input as any]).stream() as any)
      .pipe(tar.t())
      .on('entry', entry => {
        entry.resume();
      })
      .on('finish', () => {
        resolve(null);
      });
  });
}

async function runTarStream(input: Buffer) {
  await new Promise<void>((resolve, reject) => {
    Readable.fromWeb(new Blob([input as any]).stream() as any)
      .pipe(tarStream.extract())
      .on('error', reject)
      .on('entry', (_header, stream, next) => {
        stream.on('end', () => next());
        stream.resume();
      })
      .on('finish', () => {
        resolve();
      });
  });
}