[wip] #1 nodejs addon bench (memory)

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-26 22:28:27 +02:00
parent 9150d3faf6
commit 18a0a1e9a1
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
5 changed files with 93 additions and 18 deletions

3
.gitignore vendored
View file

@ -3,6 +3,9 @@
*.a *.a
*.dylib *.dylib
build/ build/
database.sqlite
database.sqlite-shm
database.sqlite-wal
# Logs # Logs
logs logs

View file

@ -0,0 +1,80 @@
const fs = require("fs");
const dal = require("../../build/Release/dal.node");
class Stats {
constructor() {
this.calls = 0;
this.avg_rss = 0;
this.avg_heapTotal = 0;
this.avg_heapUsed = 0;
this.avg_external = 0;
}
add(mem) {
this.calls++;
this.avg_rss += mem.rss;
this.avg_heapTotal += mem.heapTotal;
this.avg_heapUsed += mem.heapUsed;
this.avg_external += mem.external;
}
avg() {
const n = this.calls;
this.avg_rss /= n;
this.avg_heapTotal /= n;
this.avg_heapUsed /= n;
this.avg_external /= n;
}
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`);
}
}
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}]`);
}
console.time("Time to end");
MEM("START");
const buf = fs.readFileSync("./pkg/__test__/proto_test.msgpack");
const iter = dal.RowIterator(buf);
MEM("AFTER INIT");
let dataTransferedBytes = 0;
for (let i = 0; i < 10000000000; i++) {
const b = iter.next();
if (b.length === 0) {
break;
}
dataTransferedBytes += b.length;
if (i % 1000000 === 0) {
MEM(`ITERATION ${i}`);
}
}
MEM("AFTER ITERATION");
iter.free();
MEM("AFTER CLEANUP");
console.log("\nData transfered: ", mb(dataTransferedBytes), "Mb");
console.timeEnd("Time to end");
setTimeout(() => {
MEM("AFTER SOME TIME");
stats.avg();
stats.print();
}, 30000);

View file

@ -1,15 +0,0 @@
const fs = require("fs");
const dal = require("../../build/Release/dal.node");
//dal.InitSQLite(Buffer.from([]));
const buf = fs.readFileSync("./pkg/__test__/proto_test.msgpack");
//console.log(dal.Handle(buf));
const iter = dal.RowIterator(buf);
console.log(iter);
for (let i = 0; i < 10; i++) {
const b = iter.next();
if (b.length === 0) {
break;
}
console.log(b.toString());
}
iter.free();

View file

@ -28,6 +28,7 @@ func InitSQLite(pragmas []string) {
type RowsIter struct { type RowsIter struct {
Result []byte Result []byte
Columns []string
rows *sql.Rows rows *sql.Rows
} }
@ -83,7 +84,13 @@ func (r *RowsIter) Close() {
func (r *RowsIter) Next() []byte { func (r *RowsIter) Next() []byte {
columns, _ := r.rows.Columns() columns, _ := r.rows.Columns()
types, _ := r.rows.ColumnTypes() types, _ := r.rows.ColumnTypes()
if r.Columns == nil {
r.Columns = columns
cols, _ := proto.MarshalRow(columns)
return cols
}
data := make([]interface{}, len(columns)) data := make([]interface{}, len(columns))
r.rows.Next()
for i := range data { for i := range data {
typ := reflect.New(types[i].ScanType()).Interface() typ := reflect.New(types[i].ScanType()).Interface()
data[i] = &typ data[i] = &typ