Skip to content

Commit

Permalink
ADD: Disable Processors inside child processes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Llorx committed Aug 31, 2023
1 parent e0336da commit 1e1a7b0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 80 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ Adds a built-in [Processor](#i-processor) that outputs the result in the console

---
```typescript
bench.streamLog(stream:Stream.Writable):this;
bench.streamLog(streamCallback:() => Stream.Writable):this;
```
Adds a built-in [Processor](#i-processor) that outputs the result in a writable stream, like a file or a socket. Returns the IsoBench instance.
Adds a built-in [Processor](#i-processor) that outputs the result in a writable stream, like a file or a socket. The writable stream should be returned inside the callback function and will be only called in the main process. Returns the IsoBench instance.

---
```typescript
bench.addProcessor(processor:Processor):this;
bench.addProcessor(processorCallback:() => Processor):this;
```
Adds a custom [Processor](#i-processor) that must implement the [Processor](#i-processor) interface. Returns the IsoBench instance.
Adds a custom [Processor](#i-processor) that must implement the [Processor](#i-processor) interface. The callback should return a [Processor](#i-processor) instance and will be only called in the main process. Returns the IsoBench instance.

---
```typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iso-bench",
"version": "2.4.4",
"version": "2.4.5",
"description": "Small benchmark library focused in avoiding optimization/deoptimization pollution between tests by isolating them.",
"types": "./lib/_types/index.d.ts",
"main": "./lib/",
Expand Down
10 changes: 5 additions & 5 deletions src/IsoBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ export class IsoBench {
this.currentTests.push(test);
return this;
}
addProcessor(processor:Processor) {
addProcessor(processorCallback:() => Processor) {
if (WorkerSetup) {
return this;
}
if (this.running) {
throw new Error("Can't add processors to a running bench");
}
this.processors.push(processor);
this.processors.push(processorCallback());
return this;
}
consoleLog() {
if (WorkerSetup) {
return this;
}
return this.addProcessor(new ConsoleLog());
return this.addProcessor(() => new ConsoleLog());
}
streamLog(stream:STREAM.Writable) {
streamLog(streamCallback:() => STREAM.Writable) {
if (WorkerSetup) {
return this;
}
return this.addProcessor(new StreamLog(stream));
return this.addProcessor(() => new StreamLog(streamCallback()));
}
endGroup(name:string) {
for (const test of this.currentTests.splice(0)) {
Expand Down
93 changes: 78 additions & 15 deletions src/tests/example.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,81 @@
import FS from "fs";

import { IsoBench } from "..";

const dates = new Array(1000).fill(0).map(() => new Date(Math.floor(Date.now() - (Math.random() * 1000000000))));
const buffers = new Array(1000).fill(0).map(() => Buffer.allocUnsafe(1));
buffers.forEach(buffer => buffer[0] = Math.floor(Math.random() * 0xFF));

const bench = new IsoBench("test");
bench.add("iso", () => {
let res = 0;
for (const date of dates) {
res += Buffer.byteLength(date.toISOString());
}
});
bench.add("calc", () => {
let res = 0;
for (const date of dates) {
res += Buffer.byteLength(`${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, "0")}-${String(date.getUTCDate()).padStart(2, "0")}T${String(date.getUTCHours()).padStart(2, "0")}:${String(date.getUTCMinutes()).padStart(2, "0")}:${String(date.getUTCSeconds()).padStart(2, "0")}.${String(date.getUTCMilliseconds()).padStart(3, "0")}`);
}
});
bench.consoleLog().run();
const bench = new IsoBench("MyBench");
bench
.add("readUint8", () =>{
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("direct", () =>{
for (const buffer of buffers) {
buffer[0];
}
})
.endGroup("yay...")
.add("direct", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("readUint8", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
}).endGroup("yay...2")
.add("direct", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("readUint8", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
}).endGroup("yay...3")
.add("direct", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("readUint8", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
}).endGroup("yay...4")
.add("direct", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("readUint8", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
}).endGroup("yay...5")
.add("direct", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("readUint8", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
}).endGroup("yay...6")
.add("direct", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
})
.add("readUint8", () => {
for (const buffer of buffers) {
buffer.readUint8(0);
}
}).endGroup("yay...7")
.streamLog(() => FS.createWriteStream("test.txt")).run();
55 changes: 0 additions & 55 deletions src/tests/general.ts

This file was deleted.

0 comments on commit 1e1a7b0

Please sign in to comment.