feat: use SMA and SMM for worker tasks usage
[poolifier.git] / tests / utils.test.js
1 const { expect } = require('expect')
2 const {
3 CircularArray,
4 DEFAULT_CIRCULAR_ARRAY_SIZE
5 } = require('../lib/circular-array')
6 const {
7 availableParallelism,
8 average,
9 isAsyncFunction,
10 isKillBehavior,
11 isPlainObject,
12 median,
13 round,
14 updateMeasurementStatistics
15 } = require('../lib/utils')
16 const { KillBehaviors } = require('../lib/worker/worker-options')
17
18 describe('Utils test suite', () => {
19 it('Verify availableParallelism() behavior', () => {
20 expect(typeof availableParallelism() === 'number').toBe(true)
21 expect(availableParallelism()).toBeGreaterThan(0)
22 expect(Number.isSafeInteger(availableParallelism())).toBe(true)
23 })
24
25 it('Verify average() computation', () => {
26 expect(average([])).toBe(0)
27 expect(average([0.08])).toBe(0.08)
28 expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(
29 3.1642857142857146
30 )
31 expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(
32 2.8533333333333335
33 )
34 })
35
36 it('Verify median() computation', () => {
37 expect(median([])).toBe(0)
38 expect(median([0.08])).toBe(0.08)
39 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
40 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
41 })
42
43 it('Verify round() behavior', () => {
44 expect(round(0)).toBe(0)
45 expect(round(0.5, 0)).toBe(1)
46 expect(round(0.5)).toBe(0.5)
47 expect(round(-0.5, 0)).toBe(-1)
48 expect(round(-0.5)).toBe(-0.5)
49 expect(round(1.005)).toBe(1.01)
50 expect(round(2.175)).toBe(2.18)
51 expect(round(5.015)).toBe(5.02)
52 expect(round(-1.005)).toBe(-1.01)
53 expect(round(-2.175)).toBe(-2.18)
54 expect(round(-5.015)).toBe(-5.02)
55 })
56
57 it('Verify isPlainObject() behavior', () => {
58 expect(isPlainObject(null)).toBe(false)
59 expect(isPlainObject(undefined)).toBe(false)
60 expect(isPlainObject(true)).toBe(false)
61 expect(isPlainObject(false)).toBe(false)
62 expect(isPlainObject(0)).toBe(false)
63 expect(isPlainObject('')).toBe(false)
64 expect(isPlainObject([])).toBe(false)
65 expect(isPlainObject(() => {})).toBe(false)
66 expect(isPlainObject(new Date())).toBe(false)
67 expect(isPlainObject(new RegExp())).toBe(false)
68 expect(isPlainObject(new Error())).toBe(false)
69 expect(isPlainObject(new Map())).toBe(false)
70 expect(isPlainObject(new Set())).toBe(false)
71 expect(isPlainObject(new WeakMap())).toBe(false)
72 expect(isPlainObject(new WeakSet())).toBe(false)
73 expect(isPlainObject(new Int8Array())).toBe(false)
74 expect(isPlainObject(new Uint8Array())).toBe(false)
75 expect(isPlainObject(new Uint8ClampedArray())).toBe(false)
76 expect(isPlainObject(new Int16Array())).toBe(false)
77 expect(isPlainObject(new Uint16Array())).toBe(false)
78 expect(isPlainObject(new Int32Array())).toBe(false)
79 expect(isPlainObject(new Uint32Array())).toBe(false)
80 expect(isPlainObject(new Float32Array())).toBe(false)
81 expect(isPlainObject(new Float64Array())).toBe(false)
82 expect(isPlainObject(new BigInt64Array())).toBe(false)
83 expect(isPlainObject(new BigUint64Array())).toBe(false)
84 expect(isPlainObject(new Promise(() => {}))).toBe(false)
85 expect(isPlainObject(new WeakRef({}))).toBe(false)
86 expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false)
87 expect(isPlainObject(new ArrayBuffer())).toBe(false)
88 expect(isPlainObject(new SharedArrayBuffer())).toBe(false)
89 expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false)
90 expect(isPlainObject({})).toBe(true)
91 expect(isPlainObject({ a: 1 })).toBe(true)
92 })
93
94 it('Verify isKillBehavior() behavior', () => {
95 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
96 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
97 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
98 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
99 expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
100 expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
101 expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
102 expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false)
103 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
104 })
105
106 it('Verify isAsyncFunction() behavior', () => {
107 expect(isAsyncFunction(null)).toBe(false)
108 expect(isAsyncFunction(undefined)).toBe(false)
109 expect(isAsyncFunction(true)).toBe(false)
110 expect(isAsyncFunction(false)).toBe(false)
111 expect(isAsyncFunction(0)).toBe(false)
112 expect(isAsyncFunction('')).toBe(false)
113 expect(isAsyncFunction([])).toBe(false)
114 expect(isAsyncFunction(new Date())).toBe(false)
115 expect(isAsyncFunction(new RegExp())).toBe(false)
116 expect(isAsyncFunction(new Error())).toBe(false)
117 expect(isAsyncFunction(new Map())).toBe(false)
118 expect(isAsyncFunction(new Set())).toBe(false)
119 expect(isAsyncFunction(new WeakMap())).toBe(false)
120 expect(isAsyncFunction(new WeakSet())).toBe(false)
121 expect(isAsyncFunction(new Int8Array())).toBe(false)
122 expect(isAsyncFunction(new Uint8Array())).toBe(false)
123 expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false)
124 expect(isAsyncFunction(new Int16Array())).toBe(false)
125 expect(isAsyncFunction(new Uint16Array())).toBe(false)
126 expect(isAsyncFunction(new Int32Array())).toBe(false)
127 expect(isAsyncFunction(new Uint32Array())).toBe(false)
128 expect(isAsyncFunction(new Float32Array())).toBe(false)
129 expect(isAsyncFunction(new Float64Array())).toBe(false)
130 expect(isAsyncFunction(new BigInt64Array())).toBe(false)
131 expect(isAsyncFunction(new BigUint64Array())).toBe(false)
132 expect(isAsyncFunction(new Promise(() => {}))).toBe(false)
133 expect(isAsyncFunction(new WeakRef({}))).toBe(false)
134 expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false)
135 expect(isAsyncFunction(new ArrayBuffer())).toBe(false)
136 expect(isAsyncFunction(new SharedArrayBuffer())).toBe(false)
137 expect(isAsyncFunction(new DataView(new ArrayBuffer()))).toBe(false)
138 expect(isAsyncFunction({})).toBe(false)
139 expect(isAsyncFunction({ a: 1 })).toBe(false)
140 expect(isAsyncFunction(() => {})).toBe(false)
141 expect(isAsyncFunction(function () {})).toBe(false)
142 expect(isAsyncFunction(function named () {})).toBe(false)
143 expect(isAsyncFunction(async () => {})).toBe(true)
144 expect(isAsyncFunction(async function () {})).toBe(true)
145 expect(isAsyncFunction(async function named () {})).toBe(true)
146 })
147
148 it('Verify updateMeasurementStatistics() behavior', () => {
149 const measurementStatistics = {
150 history: new CircularArray()
151 }
152 updateMeasurementStatistics(
153 measurementStatistics,
154 { aggregate: true, average: false, median: false },
155 0.01
156 )
157 expect(measurementStatistics).toStrictEqual({
158 aggregate: 0.01,
159 maximum: 0.01,
160 minimum: 0.01,
161 history: new CircularArray()
162 })
163 updateMeasurementStatistics(
164 measurementStatistics,
165 { aggregate: true, average: false, median: false },
166 0.02
167 )
168 expect(measurementStatistics).toStrictEqual({
169 aggregate: 0.03,
170 maximum: 0.02,
171 minimum: 0.01,
172 history: new CircularArray()
173 })
174 updateMeasurementStatistics(
175 measurementStatistics,
176 { aggregate: true, average: true, median: false },
177 0.001
178 )
179 expect(measurementStatistics).toStrictEqual({
180 aggregate: 0.031,
181 maximum: 0.02,
182 minimum: 0.001,
183 average: 0.001,
184 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001)
185 })
186 updateMeasurementStatistics(
187 measurementStatistics,
188 { aggregate: true, average: true, median: false },
189 0.003
190 )
191 expect(measurementStatistics).toStrictEqual({
192 aggregate: 0.034,
193 maximum: 0.02,
194 minimum: 0.001,
195 average: 0.002,
196 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003)
197 })
198 })
199 })