2-perf.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. flattenedWorkers[k + '.' + l] = async (file, name, resetTimer) => {
  21. const fileClone = bClone(file);
  22. let buf = fileClone;
  23. if (preprocessors[l]) {
  24. buf = bClone(cache[l][name] ||= Buffer.from(
  25. await preprocessors[l as keyof typeof preprocessors](buf, [buf.buffer])
  26. ));
  27. resetTimer();
  28. }
  29. const opt2 = preprocessors[l]
  30. ? k === 'tinyInflate'
  31. ? new Uint8Array(file.length)
  32. : null
  33. : { level: 1 };
  34. await workers[k][l]([buf, opt2], opt2 instanceof Uint8Array
  35. ? [buf.buffer, opt2.buffer]
  36. : [buf.buffer]);
  37. }
  38. }
  39. }
  40. testSuites(flattenedWorkers).then(perf => {
  41. writeFileSync(join(__dirname, 'results', 'timings.json'), JSON.stringify(perf, null, 2));
  42. });