Skip to content

Instantly share code, notes, and snippets.

@kriszyp
Last active June 22, 2021 02:11
Show Gist options
  • Save kriszyp/0344b597acb4a483cfe414e17797fb6b to your computer and use it in GitHub Desktop.
Save kriszyp/0344b597acb4a483cfe414e17797fb6b to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const path = require('path');
const lmdb = require('lmdb');
const cluster = require('cluster');
const ENV_NUM = 1;
const FORK_NUM = 3;
const TEST_RUNS = 1000;
const CONCURRENT_TXN_REQUESTS = 100;
function openEnv(name, create = true) {
let environment_path = path.join(__dirname, 'data', name);
if(create) {
try {
fs.rmdirSync(environment_path, {recursive: true})
fs.mkdirSync(environment_path);
} catch (e) {
console.error(e);
}
}
let env_init = {
"path": environment_path,
"mapSize": 1024 * 1024 * 1024,
"maxReaders": 100,
"maxDbs":20,
sharedStructuresKey: Symbol.for('structures')
};
let env = lmdb.open(env_init);
env.dbis = {};
env.dbis['id'] = env.openDB('id', {
create: create,
dupSort: false,
useVersions: true
});
env.dbis['str'] = env.openDB('str', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str2'] = env.openDB('str2', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str3'] = env.openDB('str3', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str4'] = env.openDB('str4', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str5'] = env.openDB('str5', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str5'] = env.openDB('str5', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str6'] = env.openDB('str6', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str7'] = env.openDB('str7', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str8'] = env.openDB('str8', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str9'] = env.openDB('str9', {
create: create,
dupSort: true,
useVersions: false
});
global.envs[name] = env;
}
if(cluster.isMaster){
global.envs = {};
for(let x = 0; x < ENV_NUM; x++){
openEnv(`env${x}`);
}
for(let x = 0; x < FORK_NUM; x++){
cluster.fork();
}
} else {
global.envs = {};
for(let x = 0; x < ENV_NUM; x++){
openEnv(`env${x}`, false);
}
async function testTxn(j) {
let num = 0;
let env = global.envs[`env${num}`];
const id = '501174f4-d2e3-11eb-' + Math.random().toString().slice(4,8)
try {
await env.dbis['id'].transactionAsync(() => {
env.dbis['id'].put(id, {id: id, str: 'hello'});
env.dbis['str'].put('hello', id);
env.dbis['str2'].put('hello2', id);
env.dbis['str3'].put('hello3', id);
env.dbis['str4'].put('hello4', id);
env.dbis['str5'].put('hello5', id);
env.dbis['str6'].put('hello6', id);
env.dbis['str7'].put('hello7', id);
env.dbis['str8'].put('hello8', id);
env.dbis['str9'].put('hello9', id);
});
//reply.send({success: true});
}catch(e){
console.error(e)
//reply.send({success: false});
}
}
async function runTest() {
let start = Date.now()
for (let i = 0; i < TEST_RUNS; i++) {
let result
for (let j = 0; j < CONCURRENT_TXN_REQUESTS; j++) {
result = testTxn()
}
await result
}
let time = Date.now() - start
console.log('finished in ' + time + ', estimated txn/sec: ' + Math.round(FORK_NUM * TEST_RUNS * CONCURRENT_TXN_REQUESTS / (time / 1000)))
}
runTest()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment