2-perf.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { testSuites, workers, bClone, TestHandler } from './util';
  2. import { writeFileSync } from 'fs';
  3. import { join } from 'path';
  4. const preprocessors = {
  5. inflate: workers.zlib.deflate,
  6. gunzip: workers.zlib.gzip,
  7. unzlib: workers.zlib.zlib
  8. };
  9. const cache: Record<string, Record<string, Buffer>> = {
  10. deflate: {},
  11. inflate: {},
  12. gzip: {},
  13. gunzip: {},
  14. zlib: {},
  15. unzlib: {}
  16. };
  17. const flattenedWorkers: Record<string, TestHandler> = {};
  18. for (const k in workers) {
  19. for (const l in workers[k]) {
  20. if (l == 'zip' || l == 'unzip') continue;
  21. flattenedWorkers[k + '.' + l] = async (file, name, resetTimer) => {
  22. const fileClone = bClone(file);
  23. let buf = fileClone;
  24. if (preprocessors[l]) {
  25. buf = bClone(cache[l][name] ||= Buffer.from(
  26. await preprocessors[l as keyof typeof preprocessors](buf, [buf.buffer])
  27. ));
  28. resetTimer();
  29. }
  30. const opt2 = preprocessors[l]
  31. ? k === 'tinyInflate'
  32. ? new Uint8Array(file.length)
  33. : null
  34. : { level: 1 };
  35. await workers[k][l]([buf, opt2], opt2 instanceof Uint8Array
  36. ? [buf.buffer, opt2.buffer]
  37. : [buf.buffer]);
  38. }
  39. }
  40. }
  41. testSuites(flattenedWorkers).then(perf => {
  42. writeFileSync(join(__dirname, 'results', 'timings.json'), JSON.stringify(perf, null, 2));
  43. });