[feat] prep for cffi
Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
parent
ab5de2ae51
commit
c0c9b7c9b4
5
binding/makefile
Normal file
5
binding/makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
build-lib:
|
||||
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -o ../lib/libdal-arm64.dylib -buildmode=c-shared ./dal.go
|
||||
for arch in amd64 arm64; do\
|
||||
CC=x86_64-unknown-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=$${arch} go build -o ../lib/libdal-$${arch}.so -buildmode=c-shared ./dal.go;\
|
||||
done
|
|
@ -1,6 +1,8 @@
|
|||
const fs = require("fs");
|
||||
const dal = require("../../build/Release/dal.node");
|
||||
|
||||
const Mb = (num) => Math.round(num / 1024 / 1024);
|
||||
|
||||
class Stats {
|
||||
constructor() {
|
||||
this.calls = 0;
|
||||
|
@ -26,37 +28,42 @@ class Stats {
|
|||
print() {
|
||||
console.log(`
|
||||
AVERAGE:
|
||||
rss: ${mb(this.avg_rss)} Mb
|
||||
external: ${mb(this.avg_external)} Mb
|
||||
buffers: ${mb(this.avg_heapUsed)} Mb
|
||||
total: ${mb(this.avg_heapTotal)} Mb`);
|
||||
rss: ${Mb(this.avg_rss)} Mb
|
||||
external: ${Mb(this.avg_external)} Mb
|
||||
buffers: ${Mb(this.avg_heapUsed)} Mb
|
||||
total: ${Mb(this.avg_heapTotal)} Mb`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const stats = new Stats();
|
||||
let prevMem = process.memoryUsage();
|
||||
stats.add(prevMem);
|
||||
const mb = (num) => Math.round(num / 1024 / 1024);
|
||||
|
||||
|
||||
function MEM(when = "") {
|
||||
const mem = process.memoryUsage();
|
||||
stats.add(mem);
|
||||
console.log(`
|
||||
${when}
|
||||
rss: ${mb(mem.rss)} Mb [delta> ${mem.rss - prevMem.rss}]
|
||||
external: ${mb(mem.external)} Mb [delta> ${mem.external - prevMem.external}]
|
||||
buffers: ${mb(mem.heapUsed)} Mb [delta> ${mem.heapUsed - prevMem.heapUsed}]
|
||||
total: ${mb(mem.heapTotal)} Mb [delta> ${mem.heapTotal - prevMem.heapTotal}]`);
|
||||
rss: ${Mb(mem.rss)} Mb [delta> ${mem.rss - prevMem.rss}]
|
||||
external: ${Mb(mem.external)} Mb [delta> ${mem.external - prevMem.external}]
|
||||
buffers: ${Mb(mem.heapUsed)} Mb [delta> ${mem.heapUsed - prevMem.heapUsed}]
|
||||
total: ${Mb(mem.heapTotal)} Mb [delta> ${mem.heapTotal - prevMem.heapTotal}]`);
|
||||
}
|
||||
|
||||
|
||||
console.time("Time to end");
|
||||
MEM("START");
|
||||
|
||||
const buf = fs.readFileSync("./pkg/__test__/proto_test.msgpack");
|
||||
|
||||
const iter = dal.RowIterator(buf);
|
||||
const Iterator = dal.RowIterator(buf);
|
||||
MEM("AFTER INIT");
|
||||
|
||||
let dataTransferedBytes = 0;
|
||||
for (let i = 0; i < 100000000; i++) {
|
||||
const b = iter.next();
|
||||
const b = Iterator.next();
|
||||
if (b.length === 0) {
|
||||
break;
|
||||
}
|
||||
|
@ -68,11 +75,12 @@ for (let i = 0; i < 100000000; i++) {
|
|||
|
||||
MEM("AFTER ITERATION");
|
||||
|
||||
iter.free();
|
||||
Iterator.free();
|
||||
MEM("AFTER CLEANUP");
|
||||
|
||||
console.log("\nData transfered: ", mb(dataTransferedBytes), "Mb");
|
||||
console.log("\nData transfered: ", Mb(dataTransferedBytes), "Mb");
|
||||
console.timeEnd("Time to end");
|
||||
|
||||
setTimeout(() => {
|
||||
MEM("AFTER SOME TIME");
|
||||
stats.avg();
|
||||
|
|
41
dal/bun.ffi.ts
Normal file
41
dal/bun.ffi.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { dlopen, FFIType, suffix, ptr } from "bun:ffi";
|
||||
import { join } from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
// TODO: build a shared library compatible with cffi
|
||||
const libname = `libdal-arm64.${suffix}`;
|
||||
const libpath = join(__dirname, '..', 'lib', libname);
|
||||
|
||||
const {
|
||||
symbols: {
|
||||
InitSQLite,
|
||||
CreateRowIterator,
|
||||
NextRow,
|
||||
GetLen,
|
||||
FreeIter,
|
||||
},
|
||||
} = dlopen(
|
||||
libpath,
|
||||
{
|
||||
InitSQLite: {
|
||||
args: [ FFIType.cstring ],
|
||||
returns: FFIType.void,
|
||||
},
|
||||
CreateRowIterator: {
|
||||
args: [ FFIType.cstring ],
|
||||
returns: FFIType.i32,
|
||||
},
|
||||
NextRow: {
|
||||
args: [FFIType.i32],
|
||||
returns: FFIType.cstring,
|
||||
},
|
||||
GetLen: {
|
||||
args: [FFIType.i32],
|
||||
returns: FFIType.i32,
|
||||
},
|
||||
FreeIter : {
|
||||
args: [FFIType.i32],
|
||||
returns: FFIType.void,
|
||||
},
|
||||
},
|
||||
);
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"files": ["dal/index.ts", "dal/Binding.ts"],
|
||||
"files": ["dal/index.ts", "dal/Binding.ts", "dal/node.napi.ts"],
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
|
|
Loading…
Reference in a new issue