milodb
Version:
A simple mini database with optional encryption to store key-value pairs
60 lines (53 loc) • 1.83 kB
JavaScript
const fs = require('fs').promises;
const path = require('path');
const MinoDB = require('../src/index');
const BENCHMARK_DB_PATH = path.join(__dirname, '..', 'db', 'benchmark-db.json');
const NUM_ROWS = 1_000_000;
const SAMPLE_KEYS = [0, Math.floor(NUM_ROWS / 2), NUM_ROWS - 1];
async function cleanup() {
try {
await fs.unlink(BENCHMARK_DB_PATH);
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
}
async function runBenchmark() {
console.log(`Benchmark: Inserting ${NUM_ROWS.toLocaleString()} rows...`);
await cleanup();
const db = new MinoDB(null, false);
db.dbPath = BENCHMARK_DB_PATH;
const start = Date.now();
for (let i = 0; i < NUM_ROWS; i++) {
await db.set(`key${i}`, `value${i}`);
if (i > 0 && i % 100_000 === 0) {
console.log(` Inserted ${i.toLocaleString()} rows...`);
}
}
const end = Date.now();
const mem = process.memoryUsage();
console.log(`Insert time: ${(end - start) / 1000}s`);
console.log(`Memory usage: ${(mem.rss / 1024 / 1024).toFixed(2)} MB (RSS)`);
// Verify a few values
let allCorrect = true;
for (const idx of SAMPLE_KEYS) {
const key = `key${idx}`;
const expected = `value${idx}`;
const actual = await db.get(key);
if (actual !== expected) {
console.error(` Value mismatch for ${key}: expected ${expected}, got ${actual}`);
allCorrect = false;
}
}
if (allCorrect) {
console.log('Sample value verification: PASSED');
} else {
console.log('Sample value verification: FAILED');
}
// Clean up
await cleanup();
console.log('Benchmark database cleaned up.');
}
runBenchmark().catch(err => {
console.error('Benchmark failed:', err);
process.exit(1);
});