simple-benchmark-fixed-queue.js 801 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const { Piscina, FixedQueue } = require('..');
  3. const { resolve } = require('path');
  4. async function simpleBenchmark ({ duration = 10000 } = {}) {
  5. const pool = new Piscina({
  6. filename: resolve(__dirname, 'fixtures/add.js'),
  7. taskQueue: new FixedQueue()
  8. });
  9. let done = 0;
  10. const results = [];
  11. const start = process.hrtime.bigint();
  12. while (pool.queueSize === 0) {
  13. results.push(scheduleTasks());
  14. }
  15. async function scheduleTasks () {
  16. while ((process.hrtime.bigint() - start) / 1_000_000n < duration) {
  17. await pool.runTask({ a: 4, b: 6 });
  18. done++;
  19. }
  20. }
  21. await Promise.all(results);
  22. return done / duration * 1e3;
  23. }
  24. simpleBenchmark().then((opsPerSecond) => {
  25. console.log(`opsPerSecond: ${opsPerSecond} (with FixedQueue as taskQueue)`);
  26. });