-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbun-callback.js
62 lines (57 loc) · 1.38 KB
/
bun-callback.js
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
import { dlopen, JSCallback, ptr, CString } from "bun:ffi";
const {
symbols: { register_callback, start_callbacks, stop_callbacks, callback_once },
close,
} = dlopen("./callback.so", {
register_callback: {
returns: 'void',
args: ['callback']
},
start_callbacks: {
returns: 'void',
args: []
},
stop_callbacks: {
returns: 'void',
args: []
},
callback_once: {
returns: 'void',
args: []
},
});
function mem () {
return process.memoryUsage().rss
}
const on_callback = new JSCallback(
(counter) => {
counter += 1
if (counter === runs) {
stop_callbacks()
const elapsed = Date.now() - then
const rate = Math.floor(runs / (elapsed / 1000))
const ns_iter = Math.floor((elapsed * 1000000) / runs)
console.log(`${runs} ${AG}in${AD} ${elapsed} ${AG}ns${AD} = ${rate} ${AY}per/sec${AD} ${ns_iter} ${AY}ns/iter${AD} ${mem()} ${AC}rss${AD}`)
then = Date.now()
if (--total) {
process.nextTick(start_callbacks())
counter = 0
}
}
return counter
},
{
returns: "int",
args: ["int"],
},
);
const AD = '\u001b[0m' // ANSI Default
const AG = '\u001b[32m' // ANSI Green
const AY = '\u001b[33m' // ANSI Yellow
const AC = '\u001b[36m' // ANSI Cyan
let total = 20
const runs = 30000000
register_callback(on_callback)
let then = Date.now()
start_callbacks()
// await Bun.sleep(1000)