Skip to content

Commit

Permalink
improve perf getCacheKey method
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Apr 24, 2017
1 parent 8f8eed2 commit bdf4260
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
15 changes: 14 additions & 1 deletion benchmark/perf-runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

let ServiceBroker = require("../src/service-broker");
let Cacher = require("../src/cachers/memory-map");
let Transporters = require("../src/transporters");
let Serializer = require("../src/serializers/json");

Expand All @@ -17,6 +18,7 @@ function createBrokers(Transporter, opts) {

let b2 = new ServiceBroker({
transporter: new Transporter(opts),
cacher: new Cacher(),
//requestTimeout: 0,
//logger: console,
//logLevel: "debug",
Expand All @@ -29,6 +31,16 @@ function createBrokers(Transporter, opts) {
actions: {
reply(ctx) {
return ctx.params;
},
get: {
cache: {
keys: ["id"]
},
handler() {
return {
name: "User"
};
}
}
}
});
Expand All @@ -43,7 +55,8 @@ let [b1, b2] = createBrokers(Transporters.Fake);
let count = 0;
function doRequest() {
count++;
return b2.call("echo.reply", { a: count }).then(res => {
//return b2.call("echo.reply", { a: count }).then(res => {
return b2.call("echo.get", { id: 5 }).then(res => {
if (count % 10000) {
// Fast cycle
doRequest();
Expand Down
8 changes: 4 additions & 4 deletions benchmark/suites/cachers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ Suite: Set & get 1k data with cacher
-----------------------------------------------------------------------
Suite: Test getCacheKey
√ Dynamic 534,814 rps
√ Static 1,684,070 rps
√ Dynamic 507,894 rps
√ Static 19,409,900 rps
Dynamic -68.24% (534,814 rps) (avg: 1μs)
Static 0% (1,684,070 rps) (avg: 593ns)
Dynamic -97.38% (507,894 rps) (avg: 1μs)
Static 0% (19,409,900 rps) (avg: 51ns)
-----------------------------------------------------------------------
*/
15 changes: 7 additions & 8 deletions benchmark/suites/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ let bench5 = benchmark.createSuite("Call with statistics & metrics");
});
})();

//benchmark.run([bench1, bench2, bench3, bench4, bench5]);
benchmark.run([bench3]);
benchmark.run([bench1, bench2, bench3, bench4, bench5]);

/*
Expand Down Expand Up @@ -178,13 +177,13 @@ Suite: Call with middlewares
-----------------------------------------------------------------------
Suite: Call with cachers
√ No cacher* 1,093,232 rps
√ Built-in cacher* 221,052 rps
√ Built-in cacher (keys filter)* 445,376 rps
√ No cacher* 1,054,783 rps
√ Built-in cacher* 226,415 rps
√ Built-in cacher (keys filter)* 794,494 rps
No cacher* (#) 0% (1,093,232 rps) (avg: 914ns)
Built-in cacher* -79.78% (221,052 rps) (avg: 4μs)
Built-in cacher (keys filter)* -59.26% (445,376 rps) (avg: 2μs)
No cacher* (#) 0% (1,054,783 rps) (avg: 948ns)
Built-in cacher* -78.53% (226,415 rps) (avg: 4μs)
Built-in cacher (keys filter)* -24.68% (794,494 rps) (avg: 1μs)
-----------------------------------------------------------------------
Suite: Call with param validator
Expand Down
21 changes: 14 additions & 7 deletions src/cachers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,26 @@ class Cacher {
* Get a cache key by name and params.
* Concatenate the name and the hashed params object
*
* @param {any} name
* @param {any} params
* @param {any} keys
* @param {String} name
* @param {Object} params
* @param {Array|null} keys
* @returns
*/
getCacheKey(name, params, keys) {
if (params) {
if (keys && Array.isArray(keys)) {
if (keys.length > 0)
return name + ":" + keys.map(key => params[key]).join("-");
const keyPrefix = name + ":";
if (keys) {
if (keys.length == 1) {
// Quick solution for 'id' only keys
return keyPrefix + params[keys[0]];
}

if (keys.length > 0) {
return keys.reduce((a, key, i) => a + (i ? "|" : "") + params[key], keyPrefix);
}
}
else {
return name + ":" + hash(params);
return keyPrefix + hash(params);
}
}
return name;
Expand Down
3 changes: 3 additions & 0 deletions test/unit/cachers/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ describe("Test BaseCacher", () => {

res = cacher.getCacheKey("user", {a: 5, b: 3, c: 5}, ["a"]);
expect(res).toBe("user:5");

res = cacher.getCacheKey("user", {a: 5, b: 3, c: 5}, ["a", "b", "c"]);
expect(res).toBe("user:5|3|5");

res = cacher.getCacheKey("user", {a: 5, b: { id: 3 }}, ["a", "c", "b"]);
// FIXME
Expand Down

0 comments on commit bdf4260

Please sign in to comment.