feat: use SMA and SMM for worker tasks usage
[poolifier.git] / tests / utils.test.js
CommitLineData
aba955e1 1const { expect } = require('expect')
dc021bcc
JB
2const {
3 CircularArray,
4 DEFAULT_CIRCULAR_ARRAY_SIZE
5} = require('../lib/circular-array')
afe0d5bf
JB
6const {
7 availableParallelism,
dc021bcc 8 average,
78ac1a90 9 isAsyncFunction,
59317253 10 isKillBehavior,
afe0d5bf
JB
11 isPlainObject,
12 median,
a91f7b35
JB
13 round,
14 updateMeasurementStatistics
afe0d5bf 15} = require('../lib/utils')
59317253 16const { KillBehaviors } = require('../lib/worker/worker-options')
aba955e1
JB
17
18describe('Utils test suite', () => {
afe0d5bf
JB
19 it('Verify availableParallelism() behavior', () => {
20 expect(typeof availableParallelism() === 'number').toBe(true)
51c90525
JB
21 expect(availableParallelism()).toBeGreaterThan(0)
22 expect(Number.isSafeInteger(availableParallelism())).toBe(true)
afe0d5bf
JB
23 })
24
dc021bcc
JB
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
bb615bd0 36 it('Verify median() computation', () => {
4a45e8d2 37 expect(median([])).toBe(0)
76845835
JB
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)
aba955e1
JB
41 })
42
afe0d5bf
JB
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
aba955e1
JB
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 })
47aacbaa
JB
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)
78ac1a90 102 expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false)
47aacbaa
JB
103 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
104 })
78ac1a90
JB
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 })
a91f7b35
JB
147
148 it('Verify updateMeasurementStatistics() behavior', () => {
997bbcba
JB
149 const measurementStatistics = {
150 history: new CircularArray()
151 }
a91f7b35
JB
152 updateMeasurementStatistics(
153 measurementStatistics,
154 { aggregate: true, average: false, median: false },
dc021bcc 155 0.01
a91f7b35 156 )
997bbcba 157 expect(measurementStatistics).toStrictEqual({
a91f7b35
JB
158 aggregate: 0.01,
159 maximum: 0.01,
997bbcba 160 minimum: 0.01,
dc021bcc 161 history: new CircularArray()
a91f7b35
JB
162 })
163 updateMeasurementStatistics(
164 measurementStatistics,
165 { aggregate: true, average: false, median: false },
dc021bcc 166 0.02
a91f7b35 167 )
997bbcba 168 expect(measurementStatistics).toStrictEqual({
a91f7b35
JB
169 aggregate: 0.03,
170 maximum: 0.02,
997bbcba 171 minimum: 0.01,
dc021bcc 172 history: new CircularArray()
a91f7b35
JB
173 })
174 updateMeasurementStatistics(
175 measurementStatistics,
176 { aggregate: true, average: true, median: false },
dc021bcc 177 0.001
a91f7b35 178 )
997bbcba 179 expect(measurementStatistics).toStrictEqual({
a91f7b35
JB
180 aggregate: 0.031,
181 maximum: 0.02,
182 minimum: 0.001,
dc021bcc
JB
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)
a91f7b35
JB
197 })
198 })
aba955e1 199})