1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.IsoBench = void 0;
|
4 | const Test_1 = require("./Test");
|
5 | const Messager_1 = require("./Messager");
|
6 | const WorkerSetup_1 = require("./WorkerSetup");
|
7 | const processors_1 = require("./processors");
|
8 | let IDs = 0;
|
9 | function getUniqueName(name, map) {
|
10 | let newName = name;
|
11 | while (map.has(newName)) {
|
12 | newName = `${name}_${IDs++}`;
|
13 | }
|
14 | return newName;
|
15 | }
|
16 | const BENCHES = new Map();
|
17 | class IsoBench {
|
18 | name;
|
19 | processors = [];
|
20 | tests = [];
|
21 | currentTests = [];
|
22 | options;
|
23 | running = false;
|
24 | constructor(name = "IsoBench", options) {
|
25 | this.name = name;
|
26 | this.options = { ...{
|
27 | parallel: 1,
|
28 | samples: 50,
|
29 | time: 100
|
30 | }, ...options };
|
31 | this.name = getUniqueName(this.name, BENCHES);
|
32 | BENCHES.set(this.name, this);
|
33 | }
|
34 | static IfMaster(cb) {
|
35 | if (!WorkerSetup_1.WorkerSetup) {
|
36 | cb();
|
37 | }
|
38 | }
|
39 | add(name, callback, setup) {
|
40 | if (this.running) {
|
41 | throw new Error("Can't add tests to a running bench");
|
42 | }
|
43 | const test = new Test_1.Test(name, this.tests.length, callback, setup);
|
44 | this.tests.push(test);
|
45 | this.currentTests.push(test);
|
46 | return this;
|
47 | }
|
48 | addProcessor(processorCallback) {
|
49 | if (WorkerSetup_1.WorkerSetup) {
|
50 | return this;
|
51 | }
|
52 | if (this.running) {
|
53 | throw new Error("Can't add processors to a running bench");
|
54 | }
|
55 | this.processors.push(processorCallback());
|
56 | return this;
|
57 | }
|
58 | consoleLog() {
|
59 | if (WorkerSetup_1.WorkerSetup) {
|
60 | return this;
|
61 | }
|
62 | return this.addProcessor(() => new processors_1.ConsoleLog());
|
63 | }
|
64 | streamLog(streamCallback) {
|
65 | if (WorkerSetup_1.WorkerSetup) {
|
66 | return this;
|
67 | }
|
68 | return this.addProcessor(() => new processors_1.StreamLog(streamCallback()));
|
69 | }
|
70 | endGroup(name) {
|
71 | for (const test of this.currentTests.splice(0)) {
|
72 | test.group = name;
|
73 | }
|
74 | return this;
|
75 | }
|
76 | async run() {
|
77 | if (this.running) {
|
78 | throw new Error("Already running");
|
79 | }
|
80 | this.running = true;
|
81 | this.endGroup("");
|
82 | if (WorkerSetup_1.WorkerSetup) {
|
83 | this._start(WorkerSetup_1.WorkerSetup);
|
84 | }
|
85 | else {
|
86 | if (this.processors.length === 0) {
|
87 | this.consoleLog();
|
88 | }
|
89 | let i = 0;
|
90 | const tests = this.tests.slice();
|
91 | for (const processor of this.processors) {
|
92 | processor.initialize && processor.initialize(this, tests);
|
93 | }
|
94 | await Promise.all(new Array(this.options.parallel).fill(0).map(async () => {
|
95 | while (i < tests.length) {
|
96 | const test = tests[i++];
|
97 | for (const processor of this.processors) {
|
98 | processor.start && processor.start(test);
|
99 | }
|
100 | await test.fork(this.name, this.processors, this.options);
|
101 | for (const processor of this.processors) {
|
102 | processor.end && processor.end(test);
|
103 | }
|
104 | }
|
105 | }));
|
106 | for (const processor of this.processors) {
|
107 | processor.completed && processor.completed(tests);
|
108 | }
|
109 | }
|
110 | }
|
111 | async _start(setup) {
|
112 | if (this.name === setup.benchName) {
|
113 | try {
|
114 | const test = this.tests[setup.testIndex];
|
115 | if (!test) {
|
116 | throw new Error("Test index " + setup.testIndex + " not found");
|
117 | }
|
118 | await test.run(setup);
|
119 | await Messager_1.Messager.send({
|
120 | done: true
|
121 | });
|
122 | }
|
123 | catch (e) {
|
124 | await Messager_1.Messager.send({
|
125 | error: String(e)
|
126 | });
|
127 | }
|
128 | setTimeout(() => process.exit(), 100);
|
129 | }
|
130 | }
|
131 | }
|
132 | exports.IsoBench = IsoBench;
|