Skip to content

Commit 5b88d45

Browse files
committed
benchmark: add benchmarks for ffi module
Adds microbenchmarks covering the common FFI call shapes so future changes to the invoker can be evaluated: - add-i32.js: 2-arg integer - add-f64.js: 2-arg float - many-args.js: 6-arg integer - pointer-bigint.js: 1-arg pointer (BigInt) - sum-buffer.js: pointer + length (Buffer) A `common.js` helper resolves the fixture-library path from `test/ffi/fixture_library` without pulling in the test harness, and throws a clear message if the fixture hasn't been built yet. Also adds `sum_6_i32` to the fixture library for the many-args case.
1 parent a5b3d76 commit 5b88d45

7 files changed

Lines changed: 173 additions & 0 deletions

File tree

benchmark/ffi/add-f64.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const ffi = require('node:ffi');
5+
const { libraryPath, ensureFixtureLibrary } = require('./common.js');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e7],
9+
}, {
10+
flags: ['--experimental-ffi'],
11+
});
12+
13+
ensureFixtureLibrary();
14+
15+
const { lib, functions } = ffi.dlopen(libraryPath, {
16+
add_f64: { result: 'f64', parameters: ['f64', 'f64'] },
17+
});
18+
19+
const add = functions.add_f64;
20+
21+
function main({ n }) {
22+
bench.start();
23+
for (let i = 0; i < n; ++i)
24+
add(1.5, 2.5);
25+
bench.end(n);
26+
27+
lib.close();
28+
}

benchmark/ffi/add-i32.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const ffi = require('node:ffi');
5+
const { libraryPath, ensureFixtureLibrary } = require('./common.js');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e7],
9+
}, {
10+
flags: ['--experimental-ffi'],
11+
});
12+
13+
ensureFixtureLibrary();
14+
15+
const { lib, functions } = ffi.dlopen(libraryPath, {
16+
add_i32: { result: 'i32', parameters: ['i32', 'i32'] },
17+
});
18+
19+
const add = functions.add_i32;
20+
21+
function main({ n }) {
22+
bench.start();
23+
for (let i = 0; i < n; ++i)
24+
add(20, 22);
25+
bench.end(n);
26+
27+
lib.close();
28+
}

benchmark/ffi/common.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const fs = require('node:fs');
5+
const path = require('node:path');
6+
7+
// Cannot use test/ffi/ffi-test-common.js because it requires test/common
8+
// (the test harness module). Construct the path directly.
9+
const libraryPath = path.join(__dirname, '..', '..', 'test', 'ffi',
10+
'fixture_library', 'build', common.buildType,
11+
process.platform === 'win32' ? 'ffi_test_library.dll' :
12+
process.platform === 'darwin' ? 'ffi_test_library.dylib' :
13+
'ffi_test_library.so');
14+
15+
function ensureFixtureLibrary() {
16+
if (!fs.existsSync(libraryPath)) {
17+
throw new Error(
18+
`Missing FFI fixture library: ${libraryPath}. ` +
19+
'Build it with `tools/test.py test/ffi/test-ffi-calls.js` first.',
20+
);
21+
}
22+
}
23+
24+
module.exports = { libraryPath, ensureFixtureLibrary };

benchmark/ffi/many-args.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const ffi = require('node:ffi');
5+
const { libraryPath, ensureFixtureLibrary } = require('./common.js');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e7],
9+
}, {
10+
flags: ['--experimental-ffi'],
11+
});
12+
13+
ensureFixtureLibrary();
14+
15+
const { lib, functions } = ffi.dlopen(libraryPath, {
16+
sum_6_i32: { result: 'i32', parameters: ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'] },
17+
});
18+
19+
const fn = functions.sum_6_i32;
20+
21+
function main({ n }) {
22+
bench.start();
23+
for (let i = 0; i < n; ++i)
24+
fn(1, 2, 3, 4, 5, 6);
25+
bench.end(n);
26+
27+
lib.close();
28+
}

benchmark/ffi/pointer-bigint.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const ffi = require('node:ffi');
5+
const { libraryPath, ensureFixtureLibrary } = require('./common.js');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e7],
9+
}, {
10+
flags: ['--experimental-ffi'],
11+
});
12+
13+
ensureFixtureLibrary();
14+
15+
const { lib, functions } = ffi.dlopen(libraryPath, {
16+
pointer_to_usize: { result: 'u64', parameters: ['pointer'] },
17+
});
18+
19+
const fn = functions.pointer_to_usize;
20+
21+
function main({ n }) {
22+
bench.start();
23+
for (let i = 0; i < n; ++i)
24+
fn(0xdeadbeefn);
25+
bench.end(n);
26+
27+
lib.close();
28+
}

benchmark/ffi/sum-buffer.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const ffi = require('node:ffi');
5+
const { libraryPath, ensureFixtureLibrary } = require('./common.js');
6+
7+
const bench = common.createBenchmark(main, {
8+
size: [64, 1024, 16384],
9+
n: [1e6],
10+
}, {
11+
flags: ['--experimental-ffi'],
12+
});
13+
14+
ensureFixtureLibrary();
15+
16+
const { lib, functions } = ffi.dlopen(libraryPath, {
17+
sum_buffer: { result: 'u64', parameters: ['pointer', 'u64'] },
18+
});
19+
20+
function main({ n, size }) {
21+
const buf = Buffer.alloc(size, 0x42);
22+
const ptr = ffi.getRawPointer(buf);
23+
const len = BigInt(size);
24+
25+
const sum = functions.sum_buffer;
26+
27+
bench.start();
28+
for (let i = 0; i < n; ++i)
29+
sum(ptr, len);
30+
bench.end(n);
31+
32+
lib.close();
33+
}

test/ffi/fixture_library/ffi_test_library.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ FFI_EXPORT double sum_five_f64(
331331
return a + b + c + d + e;
332332
}
333333

334+
FFI_EXPORT int32_t sum_6_i32(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e, int32_t f) {
335+
return a + b + c + d + e + f;
336+
}
337+
334338
// Mixed parameter types.
335339

336340
FFI_EXPORT double mixed_operation(int32_t i, float f, double d, uint32_t u) {

0 commit comments

Comments
 (0)