Skip to content

Instantly share code, notes, and snippets.

@Prtfw
Created June 27, 2017 20:57
Show Gist options
  • Save Prtfw/89276f8e05ef3bec18feebdec1f68d5c to your computer and use it in GitHub Desktop.
Save Prtfw/89276f8e05ef3bec18feebdec1f68d5c to your computer and use it in GitHub Desktop.
not sure if callback time stamp generated on line 78 is to be trusted.
var AES = require("crypto-js/aes");
const SHA256 = require('crypto-js/sha256')
const Chance = require('chance')
// const testsetL = require('./testData')
// const testsetS = require('./testDatas')
const fs = require('fs');
const l = 10000;
const testing = 'redis'
//setup fake data
const chance = new Chance();
chance.mixin({
test: () => ({
first: chance.first(),
last: chance.last(),
guid: chance.guid()
})
});
/*
notes:
1- make giant array with rand var
2- stick in db
3- either call back or log if bgn/mid/end
4- run index test
5- retreive bgn/mid/end
*/
// data munging and storing
let seeder = Array.from({length: l})
const mkr = () => { const str = chance.test();
const keyHash = SHA256(AES.encrypt(JSON.stringify(str), 'secret key blah blah').ciphertext.words.join("")).words.join("");
return {[keyHash]: str}
}
let testSet = testData = seeder.map(x=>mkr())
let mid = Object.keys(testSet[Math.floor(l/2)])[0]
let last = Object.keys(testSet[l-1])[0]
fs.writeFile("testJSON.txt", JSON.stringify(testData), function(err) {
if(err) {
return console.log(err);
}
});
// REDIS
if (testing === 'redis'){
const Redis = require('redis');
const redis = Redis.createClient();
redis.flushdb((err, dt)=>console.log(dt)) //clear everything
// const resMid = acc.by({hash: `${mid}`});
// console.log("end get mid", `${mid}`, resMid, new Date())
// console.log("start get last", new Date())
// const resEnd = acc.by({hash: `${last}`});
// console.log("end get last", `${last}`, resEnd, new Date())
console.log("start write", new Date())
let ind = 0;
testSet[l-1].cb = (err, dt)=>{ console.log('end write', err, dt, new Date())}
testSet.forEach((x, i) => { ind = i;
// console.log(78, Object.keys(x)[0], x[Object.keys(x)[0]])
res = redis.hmset(Object.keys(x)[0], x[Object.keys(x)[0]], x.cb && x.cb())
// (err, dt)=> console.log(ind, err, dt, new Date())) // simple testing cb
// (ind == (l-1)) ?
// (err, dt)=> !err && console.log('end write', ind, res, new Date())
// :
// null)
})
console.log("start get mid", new Date())
// console.log(83, mid)
redis.hgetall(mid, function (err, obj) {
console.log(90, obj);
});
console.log("start get last", new Date())
redis.hgetall(last, function (err, obj) {
console.log(93, obj);
});
}
if (testing === 'loki'){
const loki = require('lokijs')
const db = new loki('testLoki.json');
const acc = db.addCollection('acc', {indices: ['hash']});
console.log("start write", new Date())
testSet.forEach(x => resultObj = acc.insert({hash: Object.keys(x), val: x[Object.keys(x)[0]]}));
console.log('end write', new Date())
console.log("start get mid", new Date())
const resMid = acc.by({hash: `${mid}`});
console.log("end get mid", `${mid}`, resMid, new Date())
console.log("start get last", new Date())
const resEnd = acc.by({hash: `${last}`});
console.log("end get last", `${last}`, resEnd, new Date())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment