-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
sequential.test.ts
129 lines (112 loc) 路 3.42 KB
/
sequential.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { setTimeout } from 'timers/promises';
import { test, expect } from 'vitest';
import { Bench } from '../src';
test('sequential', async () => {
let isFirstTaskDefined = true;
const sequentialBench = new Bench({
time: 0,
iterations: 100,
});
isFirstTaskDefined = false;
sequentialBench
.add('sample 1', async () => {
await setTimeout(0);
for (let i = 0; i < 1e7; i++);
})
.add('sample 2', async () => {
// sample 1 should be defined always
if (typeof sequentialBench.tasks[0]?.result === 'undefined') { isFirstTaskDefined = false; } else isFirstTaskDefined = true;
await setTimeout(0);
for (let i = 0; i < 1e7; i++);
});
await sequentialBench.warmup();
await sequentialBench.run();
expect(isFirstTaskDefined).toBe(true);
});
test.each(['warmup', 'run'])('%s concurrent (bench level)', async (mode) => {
const concurrentBench = new Bench({
time: 0,
iterations: 100,
throws: true,
});
let shouldBeDefined1: true;
let shouldBeDefined2: true;
let shouldNotBeDefinedFirst1: true;
let shouldNotBeDefinedFirst2: true;
concurrentBench
.add('sample 1', async () => {
shouldBeDefined1 = true;
await setTimeout(100);
shouldNotBeDefinedFirst1 = true;
})
.add('sample 2', async () => {
shouldBeDefined2 = true;
await setTimeout(100);
shouldNotBeDefinedFirst2 = true;
});
if (mode === 'warmup') {
concurrentBench.warmupConcurrently();
} else {
concurrentBench.runConcurrently();
}
await setTimeout(0);
expect(shouldBeDefined1!).toBeDefined();
expect(shouldBeDefined2!).toBeDefined();
expect(shouldNotBeDefinedFirst1!).toBeUndefined();
expect(shouldNotBeDefinedFirst2!).toBeUndefined();
await setTimeout(100);
expect(shouldNotBeDefinedFirst1!).toBeDefined();
expect(shouldNotBeDefinedFirst2!).toBeDefined();
});
test.each(['warmup', 'run'])('%s concurrent (task level)', async (mode) => {
const iterations = 10;
const concurrentBench = new Bench({
time: 0,
warmupTime: 0,
iterations,
warmupIterations: iterations,
});
const key = 'sample 1';
const runs = { value: 0 };
concurrentBench
.add(key, async () => {
runs.value++;
await setTimeout(10);
// all task function should be here after 10ms
expect(runs.value).toEqual(iterations);
await setTimeout(10);
});
if (mode === 'warmup') {
await concurrentBench.warmup();
} else {
await concurrentBench.run();
for (const result of concurrentBench.results) {
expect(result?.error).toMatch(/AssertionError/);
}
}
expect(concurrentBench.getTask(key)!.runs).toEqual(0);
concurrentBench.reset();
runs.value = 0;
if (mode === 'warmup') {
await concurrentBench.warmupConcurrently();
} else {
await concurrentBench.runConcurrently();
for (const result of concurrentBench.results) {
expect(result?.error).toMatch(/AssertionError/);
}
}
expect(concurrentBench.getTask(key)!.runs).toEqual(0);
concurrentBench.reset();
runs.value = 0;
if (mode === 'warmup') {
await concurrentBench.warmupConcurrently(Infinity, 'task');
expect(runs.value).toEqual(10);
} else {
await concurrentBench.runConcurrently(Infinity, 'task');
for (const result of concurrentBench.results) {
expect(result?.error).toBeUndefined();
}
expect(runs.value).toEqual(10);
expect(concurrentBench.getTask(key)!.runs).toEqual(10);
}
});