Skip to content

Instantly share code, notes, and snippets.

@ryanhs
Last active May 5, 2018 17:29
Show Gist options
  • Select an option

  • Save ryanhs/34c7397ce20e3f2778038c2d94942d2a to your computer and use it in GitHub Desktop.

Select an option

Save ryanhs/34c7397ce20e3f2778038c2d94942d2a to your computer and use it in GitHub Desktop.
Loki JS for big data, benchmark
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
const express = require('express');
const loki = require('lokijs');
var app = express();
var db = new loki('test');
// make big DB for simulation
var items = db.addCollection('items', {unique: ['id']});
const maxItems = 1000 * 1000;
for (var i = 0; i < maxItems; i++) {
items.insert({ id: i, value: Math.random()});
}
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) cluster.fork();
cluster.on('exit', (worker, code, signal) => console.log(`worker ${worker.process.pid} died`));
} else {
// This is Workers can share any TCP connection
// It will be initialized using express
console.log(`Worker ${process.pid} started`);
app.get('/', (req, res) => {
let worker = cluster.worker.id;
let id = parseInt(Math.random() * maxItems, 10);
var tyrfing = items.by('id', id);
// var tyrfing = items.findOne({'id': id});
// var tyrfing = items.get(id);
res.send(`worker #${worker} \n ${id}:${JSON.stringify(tyrfing)}`);
});
app.listen(8888, function() {
console.log('Your node is running on port 8888');
});
}
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
const jayson = require('jayson');
const loki = require('lokijs');
var db = new loki('test');
// make big DB for simulation
var items = db.addCollection('items', {unique: ['id']});
const maxItems = 1000 * 1000;
for (var i = 0; i < maxItems; i++) {
items.insert({id: i, value: Math.random()});
}
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++)
cluster.fork();
cluster.on('exit', (worker, code, signal) => console.log(`worker ${worker.process.pid} died`));
} else {
var server = jayson.server({
// curl -d '{"jsonrpc":"2.0", "id":9, "method": "test", "params": [1]}' -H "Content-Type: application/json" -X POST http://localhost:8888/
// ab -p /tmp/request.json -T application/json -c 8 -n 10000 http://127.0.0.1:8888/
test: function(args, callback) {
let worker = cluster.worker.id;
let id = parseInt(Math.random() * maxItems, 10);
var tyrfing = items.by('id', id);
// var tyrfing = items.findOne({'id': id});
// var tyrfing = items.get(id);
callback(null, {worker, id, value: tyrfing});
}
});
server.http().listen(8888);
console.log(`Worker ${process.pid} started on port 8888`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment