poolize
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

poolize

NPM Version Node Version

make resource pooling

⚠️ Deprecated, please goto 👉 cycle-pool

Installation

npm i poolize -S

Usage

import Pool from 'poolize';
// for commonjs -> const Pool = require('poolize').default;

class TaskHandler {
  // your heavy progress
  async exec(a: number, b: number): Promise<number> {
    await sleep(300);
    return a * b;
  }
}

const pool = new Pool<TaskHandler>({
  min: 10,
  worker: TaskHandler,
});

async function test() {
  for (let i = 0; i < 22; i++) {
    ;(async function () {
      const result = await pool.exec(i, i);

      console.log('job:', i, 'result:', result, 'running:', pool.running, 'idle:', pool.idleSize);
    })().catch((err) => {
      console.error(err);
    });
  }
}

test();

also you can release resource manually

const worker = await pool.acquire();
const result = await worker.exec(i, i);
pool.release(worker);

console.log('job:', i, 'result:', result, 'running:', pool.running, 'idle:', pool.idleSize);

Examples

examples are listed at examples

Performance

autocannon http://127.0.0.1:3000/bcrypt

normal bcrypt

┌─────────┬────────┬────────┬────────┬────────┬───────────┬──────────┬───────────┐
│ Stat    │ 2.5%   │ 50%    │ 97.5%  │ 99%    │ Avg       │ Stdev    │ Max       │
├─────────┼────────┼────────┼────────┼────────┼───────────┼──────────┼───────────┤
│ Latency │ 131 ms │ 164 ms │ 245 ms │ 262 ms │ 165.16 ms │ 25.29 ms │ 315.39 ms │
└─────────┴────────┴────────┴────────┴────────┴───────────┴──────────┴───────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬───────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg   │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼───────┼─────────┼─────────┤
│ Req/Sec   │ 50      │ 50      │ 60      │ 67      │ 60.2  │ 4.31    │ 50      │
├───────────┼─────────┼─────────┼─────────┼─────────┼───────┼─────────┼─────────┤
│ Bytes/Sec │ 13.3 kB │ 13.3 kB │ 15.9 kB │ 17.8 kB │ 16 kB │ 1.14 kB │ 13.3 kB │
└───────────┴─────────┴─────────┴─────────┴─────────┴───────┴─────────┴─────────┘

after poolize with worker_threads

┌─────────┬───────┬───────┬───────┬───────┬──────────┬─────────┬───────────┐
│ Stat    │ 2.5%  │ 50%   │ 97.5% │ 99%   │ Avg      │ Stdev   │ Max       │
├─────────┼───────┼───────┼───────┼───────┼──────────┼─────────┼───────────┤
│ Latency │ 20 ms │ 23 ms │ 26 ms │ 28 ms │ 23.06 ms │ 3.93 ms │ 103.37 ms │
└─────────┴───────┴───────┴───────┴───────┴──────────┴─────────┴───────────┘
┌───────────┬────────┬────────┬────────┬────────┬────────┬─────────┬────────┐
│ Stat      │ 1%     │ 2.5%   │ 50%    │ 97.5%  │ Avg    │ Stdev   │ Min    │
├───────────┼────────┼────────┼────────┼────────┼────────┼─────────┼────────┤
│ Req/Sec   │ 388    │ 388    │ 427    │ 464    │ 423.91 │ 23.73   │ 388    │
├───────────┼────────┼────────┼────────┼────────┼────────┼─────────┼────────┤
│ Bytes/Sec │ 103 kB │ 103 kB │ 113 kB │ 123 kB │ 112 kB │ 6.28 kB │ 103 kB │
└───────────┴────────┴────────┴────────┴────────┴────────┴─────────┴────────┘

Todo

  • [ ] idle release

Others

License

MIT

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i poolize

    Weekly Downloads

    2

    Version

    1.0.6

    License

    MIT

    Unpacked Size

    16.6 kB

    Total Files

    5

    Last publish

    Collaborators

    • cooperhsiung