Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding benchmark for worst case #40

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
demo
coverage
.clinic
doctor-patient.js
doctor-patient.js
benchmark.js
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ The cache key is composed based on the following criteria, allowing multiple fil
- `instanceIdentifier`: A uniqid generated for each instance of the class. It uses the instance properties to generate the id. (JSON.stringify)
- The arguments passed to the method. (JSON.stringify)

## Benchmarks

An overhead benchmark is available launching the `npm run benchmark` command.
The benchmark will run:
- A simple function without cache-candidate.
- A cache-candidate wrap of the same function of the first test with an unreachable requestsThreshold.
- A cache-candidate wrap of the same function of the first test with a candidateFunction that always returns false.

The results on a MacBook Pro M1 2021 with 16GB of RAM are:

```
cacheCandidate OFF - normalFunction x 47.48 ops/sec ±0.48% (74 runs sampled)
cacheCandidate ON - Request Threshold unreachable x 46.86 ops/sec ±0.76% (73 runs sampled)
cacheCandidate ON - Candidate Function set to false x 46.75 ops/sec ±0.47% (72 runs sampled)
```

The results show that the library has a very low overhead, even when the cache is not used.
Our suggestion therefore is to use the library on every function that could benefit from an hypothetical caching.

# Contributing

Please, refer to the [CONTRIBUTING.md](https://github.com/JointlyTech/cache-candidate/blob/main/CONTRIBUTING.md) file for more information.
41 changes: 41 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const {cacheCandidate} = require('./dist');
const Benchmark = require('benchmark');

let suite = new Benchmark.Suite();

const normalFunction = function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(true);
}, 20);
});
};

const wrappedNFWithReqThreshold = cacheCandidate(normalFunction, {requestsThreshold: 1_000_000_000});
const wrappedNFWithCandidateFn = cacheCandidate(normalFunction, {candidateFunction: () => false});

suite.add('cacheCandidate OFF - normalFunction', {
defer: true,
fn: function (deferred) {
normalFunction().then(function() {
deferred.resolve();
});
}
}).add('cacheCandidate ON - Request Threshold unreachable', {
defer: true,
fn: function (deferred) {
wrappedNFWithReqThreshold().then(function() {
deferred.resolve();
});
}
}).add('cacheCandidate ON - Candidate Function set to false', {
defer: true,
fn: function (deferred) {
wrappedNFWithCandidateFn().then(function() {
deferred.resolve();
});
}
}).on('cycle', function(event) {
console.log(String(event.target));
})
.run({ 'async': true });
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dev": "jest --watch",
"test": "jest --no-cache",
"coverage": "jest --coverage",
"benchmark": "node benchmark.js",
"release:common": "npm run build && git push --follow-tags origin main && npm publish --access public",
"release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'docs: updated changelog' && npm version patch && npm run release:common",
"release:minor": "changelog -m && git add CHANGELOG.md && git commit -m 'docs: updated changelog' && npm version minor && npm run release:common",
Expand All @@ -40,6 +41,7 @@
"@types/redis": "^4.0.11",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"benchmark": "^2.1.4",
"esbuild": "^0.16.4",
"eslint": "^8.29.0",
"generate-changelog": "^1.8.0",
Expand Down