piscina-queue-comparison.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { Bench } = require('tinybench');
  2. const { Piscina, FixedQueue, ArrayTaskQueue } = require('..');
  3. const { resolve } = require('node:path');
  4. const QUEUE_SIZE = 100_000;
  5. const bench = new Bench({ time: 100 });
  6. bench
  7. .add('Piscina with ArrayTaskQueue', async () => {
  8. const queue = new ArrayTaskQueue();
  9. const pool = new Piscina({
  10. filename: resolve(__dirname, 'fixtures/add.js'),
  11. taskQueue: queue
  12. });
  13. const tasks = [];
  14. for (let i = 0; i < QUEUE_SIZE; i++) {
  15. tasks.push(pool.runTask({ a: 4, b: 6 }));
  16. }
  17. await Promise.all(tasks);
  18. await pool.destroy();
  19. })
  20. .add('Piscina with FixedQueue', async () => {
  21. const queue = new FixedQueue();
  22. const pool = new Piscina({
  23. filename: resolve(__dirname, 'fixtures/add.js'),
  24. taskQueue: queue
  25. });
  26. const tasks = [];
  27. for (let i = 0; i < QUEUE_SIZE; i++) {
  28. tasks.push(pool.runTask({ a: 4, b: 6 }));
  29. }
  30. await Promise.all(tasks);
  31. await pool.destroy();
  32. });
  33. (async () => {
  34. await bench.warmup();
  35. await bench.run();
  36. console.table(bench.table());
  37. })();