<img src="./images/logo.png" width="340px" height="266px"/>
</div>
-<h2 align="center">Node Thread Pool and Cluster Pool :arrow_double_up: :on:</h2>
+<h1 align="center">Node Thread Pool and Cluster Pool</h1>
<p align="center">
<a href="https://github.com/poolifier/poolifier/graphs/commit-activity">
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_index)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
- Code security [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=security_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
-## Contents
-
-<h3 align="center">
- <a href="#overview">Overview</a>
- <span> · </span>
- <a href="#installation">Installation</a>
- <span> · </span>
- <a href="#usage">Usage</a>
- <span> · </span>
- <a href="#node-versions">Node versions</a>
- <span> · </span>
- <a href="#api">API</a>
- <span> · </span>
- <a href="#general-guidelines">General guidelines</a>
- <span> · </span>
- <a href="#contribute">Contribute</a>
- <span> · </span>
- <a href="#team">Team</a>
- <span> · </span>
- <a href="#license">License</a>
-</h3>
+## Table of contents
+
+- [Overview](#overview)
+- [Installation](#installation)
+- [Usage](#usage)
+- [Node versions](#node-versions)
+- [API](#api)
+- [General guidelines](#general-guidelines)
+- [Worker choice strategies](#worker-choice-strategies)
+- [Contribute](#contribute)
+- [Team](#team)
+- [License](#license)
## Overview
## [API](./docs/api.md)
-## [General Guidelines](./docs/general-guidelines.md)
+## [General guidelines](./docs/general-guidelines.md)
+
+## [Worker choice strategies](./docs/worker-choice-strategies.md)
## Contribute
> :warning: **We would need funds to run our benchmarks more often and on Cloud VMs, please consider to sponsor this project**
-### Internal
+Read [README.md](./versus-external-pools/README.md) to know how to run the benchmarks.
-To run the internal benchmarks, you just need to navigate to the root of poolifier project and run `pnpm benchmark`
-
-## Versus other pools
+## Internal
-Read [README.md](./versus-external-pools/README.md)
+To run the internal benchmarks, you just need to navigate to the root of poolifier project and run `pnpm benchmark`
- Run `pnpm install` into the `versus-external-pools` folder
- Run the `./bench.sh` script into the `versus-external-pools` folder
-> :warning: **Please be sure to use a quite PC when you run the benchmarks**
+> :warning: **Please be sure to use a quiet PC when you run the benchmarks**
-## [API](https://poolifier.github.io/poolifier/)
+# [API](https://poolifier.github.io/poolifier/)
+
+## Table of contents
+
+- [Pool](#pool)
+ - [`pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`](#pool--new-fixedthreadpoolfixedclusterpoolnumberofthreadsnumberofworkers-filepath-opts)
+ - [`pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`](#pool--new-dynamicthreadpooldynamicclusterpoolmin-max-filepath-opts)
+ - [`pool.execute(data, name)`](#poolexecutedata-name)
+ - [`pool.destroy()`](#pooldestroy)
+ - [`PoolOptions`](#pooloptions)
+ - [``ThreadPoolOptions extends PoolOptions`](#threadpooloptions-extends-pooloptions)
+ - [`ClusterPoolOptions extends PoolOptions`](#clusterpooloptions-extends-pooloptions)
+- [Worker](#worker)
+ - [`class YourWorker extends ThreadWorker/ClusterWorker`](#class-yourworker-extends-threadworkerclusterworker)
+
+## Pool
### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
- `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
+## Worker
+
### `class YourWorker extends ThreadWorker/ClusterWorker`
`taskFunctions` (mandatory) The task function or task functions object `{ name_1: fn_1, ..., name_n: fn_n }` that you want to execute on the worker
-## General Guidelines
+# General guidelines
Performance is one of the main target of these worker pool implementations, poolifier team wants to have a strong focus on this.
Poolifier already has a [benchmarks](./benchmarks/) folder where you can find some comparisons.
-### Internal Node.js thread pool
+## Table of contents
+
+- [Internal Node.js thread pool](#internal-nodejs-thread-pool)
+- [Cluster vs Threads worker pools](#cluster-vs-threads-worker-pools)
+- [Fixed vs Dynamic pools](#fixed-vs-dynamic-pools)
+
+## Internal Node.js thread pool
Before to jump into each poolifier pool type, let highlight that **Node.js comes with a thread pool already**, the libuv thread pool where some particular tasks already run by default.
Please take a look at [which tasks run on the libuv thread pool](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/#what-code-runs-on-the-worker-pool).
- Use poolifier cluster pools that are spawning child processes, they will also increase the number of libuv threads since that any new child process comes with a separated libuv thread pool. **More threads does not mean more fast, so please tune your application**.
-### Cluster vs Threads worker pools
+## Cluster vs Threads worker pools
**If your task does not run into libuv thread pool** and is CPU intensive then poolifier **thread pools** (_FixedThreadPool_ and _DynamicThreadPool_) are suggested to run CPU intensive tasks, you can still run I/O intensive tasks into thread pools, but performance enhancement is expected to be minimal.
Thread pools are built on top of Node.js [worker_threads](https://nodejs.org/api/worker_threads.html) module.
If your task contains code that runs on libuv plus code that is CPU intensive or I/O intensive you either split it either combine more strategies (i.e. tune the number of libuv threads and use cluster/thread pools).
But in general, **always profile your application**.
-### Fixed vs Dynamic pools
+## Fixed vs Dynamic pools
To choose your pool consider first that with a _FixedThreadPool_/_FixedClusterPool_ or a _DynamicThreadPool_/_DynamicClusterPool_ your application memory footprint will increase.
By doing so, your application will be ready to execute in parallel more tasks, but during idle time your application will consume more memory.
# Worker choice strategies
-All duration or timestamp are expressed in milliseconds.
+## Table of contents
+
+- [Strategies](#strategies)
+ - [Fair share](#fair-share)
+ - [Weighted round robin](#weighted-round-robin)
+ - [Interleaved weighted round robin](#interleaved-weighted-round-robin)
+- [Statistics](#statistics)
+ - [Median](#median)
## Strategies
+All duration or timestamp are expressed in milliseconds.
+
### Fair share
Its goal is to distribute the load evenly across all workers. To achieve this, the strategy keeps track of the average task execution time for each worker and assigns the next task to the worker with the lowest task end prediction time: `task_end_prediction = max(current_time, task_end_prediction) + average_task_execution_time`.