Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/ffi/add-f64.js → benchmark/ffi/add-64.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const add = functions.add_f64;
function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(1.5, 2.5);
add(20.5, 21.5);
bench.end(n);

lib.close();
Expand Down
28 changes: 28 additions & 0 deletions benchmark/ffi/add-f32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_f32: { result: 'f32', parameters: ['f32', 'f32'] },
});

const add = functions.add_f32;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20.5, 21.5);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/add-i16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_i16: { result: 'i16', parameters: ['i16', 'i16'] },
});

const add = functions.add_i16;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20, 22);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/add-i64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_i64: { result: 'i64', parameters: ['i64', 'i64'] },
});

const add = functions.add_i64;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20n, 22n);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/add-i8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_i8: { result: 'i8', parameters: ['i8', 'i8'] },
});

const add = functions.add_i8;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20, 22);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/add-u16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_u16: { result: 'u16', parameters: ['u16', 'u16'] },
});

const add = functions.add_u16;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20, 22);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/add-u64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_u64: { result: 'u64', parameters: ['u64', 'u64'] },
});

const add = functions.add_u64;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20n, 22n);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/add-u8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
add_u8: { result: 'u8', parameters: ['u8', 'u8'] },
});

const add = functions.add_u8;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
add(20, 22);
bench.end(n);

lib.close();
}
30 changes: 30 additions & 0 deletions benchmark/ffi/buffer-first-byte-direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
first_byte: { result: 'u8', parameters: ['pointer'] },
});

const fn = functions.first_byte;
const bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
const pointer = ffi.getRawPointer(bytes);

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
fn(pointer);
bench.end(n);

lib.close();
}
29 changes: 29 additions & 0 deletions benchmark/ffi/buffer-first-byte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
first_byte: { result: 'u8', parameters: ['buffer'] },
});

const fn = functions.first_byte;
const bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
fn(bytes);
bench.end(n);

lib.close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
size: [64, 1024, 16384],
n: [1e6],
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});
Expand All @@ -17,16 +16,15 @@ const { lib, functions } = ffi.dlopen(libraryPath, {
sum_buffer: { result: 'u64', parameters: ['pointer', 'u64'] },
});

function main({ n, size }) {
const buf = Buffer.alloc(size, 0x42);
const ptr = ffi.getRawPointer(buf);
const len = BigInt(size);

const sum = functions.sum_buffer;
const fn = functions.sum_buffer;
const bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
const pointer = ffi.getRawPointer(bytes);
const length = BigInt(bytes.length);

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
sum(ptr, len);
fn(pointer, length);
bench.end(n);

lib.close();
Expand Down
30 changes: 30 additions & 0 deletions benchmark/ffi/buffer-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
sum_buffer: { result: 'u64', parameters: ['buffer', 'u64'] },
});

const fn = functions.sum_buffer;
const bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
const length = BigInt(bytes.length);

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
fn(bytes, length);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/identity-i32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
identity_i32: { result: 'i32', parameters: ['i32'] },
});

const fn = functions.identity_i32;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
fn(42);
bench.end(n);

lib.close();
}
28 changes: 28 additions & 0 deletions benchmark/ffi/noop-void.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');
const { libraryPath, ensureFixtureLibrary } = require('./common.js');

const bench = common.createBenchmark(main, {
n: [1e7],
}, {
flags: ['--experimental-ffi'],
});

ensureFixtureLibrary();

const { lib, functions } = ffi.dlopen(libraryPath, {
noop_void: { result: 'void', parameters: [] },
});

const fn = functions.noop_void;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
fn();
bench.end(n);

lib.close();
}
Loading