Skip to content

Commit

Permalink
Merge pull request #79 from fed135/v1.13.1
Browse files Browse the repository at this point in the history
v1.13.1
  • Loading branch information
fed135 authored Apr 8, 2019
2 parents b7b4fe7 + f41c27b commit 66241f9
Show file tree
Hide file tree
Showing 5 changed files with 127,191 additions and 39 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ha-store",
"version": "1.13.0",
"version": "1.13.1",
"description": "Efficient data fetching",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -42,7 +42,8 @@
"chai": "^4.2.0",
"heapdump": "^0.3.12",
"mocha": "^6.0.0",
"sinon": "^7.2.0"
"sinon": "^7.2.0",
"split2": "^3.1.1"
},
"contributors": [
"frederic charette <[email protected]>",
Expand Down
38 changes: 20 additions & 18 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { tween } = require('./utils.js');
* @param {Object} store A store instance to replace the default in-memory Dictionary
*/
function localStore(config, emitter, store) {
store = store || {};
store = store || {};
let storeSize = 0;
let totalTTL = 0;
const curve = tween(config.cache);
Expand Down Expand Up @@ -57,30 +57,32 @@ function localStore(config, emitter, store) {
for (let i = 0; i < keys.length; i++) {
if (storeSize + 1 > config.storeOptions.recordLimit) {
emitter.emit('cacheSkip', { omitted: { key: recordKey(keys[i]), value: values[keys[i]] }, reason: 'Too many records' });
continue;
return false;
}
const storageEfficiency = ((totalTTL / storeSize || 1) / config.cache.limit); // Generates a 0-1 number indicating current efficiency.
if (Math.random() < storageEfficiency * config.storeOptions.dropFactor) {
emitter.emit('cacheSkip', { omitted: { key: recordKey(keys[i]), value: values[keys[i]] }, reason: 'Efficiency capped' });
continue;
}
let value = {
value: values[keys[i]],
timestamp: null,
step: null,
ttl: 0,
bump: null,
};
totalTTL += stepSize;
const key = recordKey(keys[i]);
if (opts && opts.step !== undefined) {
value.timestamp = now;
value.step = opts.step;
value.ttl = now + stepSize;
else {
let value = {
value: values[keys[i]],
timestamp: null,
step: null,
ttl: 0,
bump: null,
};
totalTTL += stepSize;
const key = recordKey(keys[i]);
if (opts && opts.step !== undefined) {
value.timestamp = now;
value.step = opts.step;
value.ttl = now + stepSize;
}
storeSize++;
store[key] = value;
}
storeSize++;
store[key] = value;
}
return true;
}

/**
Expand Down
29 changes: 11 additions & 18 deletions tests/profiling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,17 @@

/* Requires ------------------------------------------------------------------*/

const crypto = require('crypto');
const settings = require('./settings');
const {fork} = require('child_process');
const path = require('path');
const fs = require('fs');
const split2 = require('split2');

/* Init ----------------------------------------------------------------------*/

// Setup
const app = fork(path.resolve(__dirname, './testApp.js'));
const startTime = Date.now();

async function hitStore() {
if (Date.now() - startTime < settings.test.testDuration) {
process.nextTick(hitStore);

// Simulate normal z-distribution
let sampleRange = (Math.round(Math.random()*3) === 0) ? 1:2;
const id = crypto.randomBytes(sampleRange).toString('hex');
const language = settings.test.languages[Math.floor(Math.random()*settings.test.languages.length)];
app.send({ id, language });
}
else {
app.send('finish');
}
}
const stream = fs.createReadStream(path.resolve(__dirname, 'sample.txt'), 'utf-8').pipe(split2());

app.on('message', async (suite) => {
console.log(`
Expand All @@ -52,4 +38,11 @@ app.on('message', async (suite) => {
process.exit(0);
});

hitStore();
stream.on('data', (chunk) => {
const [id, language] = chunk.split(' ');
app.send({ id, language });
});

stream.on('end', () => {
app.send('finish');
});
Loading

0 comments on commit 66241f9

Please sign in to comment.