Skip to content

Instantly share code, notes, and snippets.

@nitram509
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitram509/35b3570edfd9144f37e5 to your computer and use it in GitHub Desktop.
Save nitram509/35b3570edfd9144f37e5 to your computer and use it in GitHub Desktop.
macaroons.js benchmark
// requires
// $> npm install benchmark
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var MacaroonsBuilder = require('../index.js').MacaroonsBuilder;
var MacaroonsVerifier = require('../index.js').MacaroonsVerifier;
var serialized1 = (function prepare_a_macaroon_for_deserialization_with_string() {
var location = "http://www.example.org";
var secretKey = "this is our super secret key; only we should know it";
var identifier = "we used our secret key";
var macaroon = MacaroonsBuilder.create(location, secretKey, identifier);
return macaroon.serialize();
})();
var serialized2 = (function prepare_a_macaroon_for_deserialization_with_Buffer() {
var location = "http://www.example.org";
var secretKey = new Buffer("this is our super secret key; only we should know it", 'ascii');
var identifier = "we used our secret key";
var macaroon = MacaroonsBuilder.create(location, secretKey, identifier);
return macaroon.serialize();
})();
function benchmark_Serialize_with_key_string() {
var location = "http://www.example.org";
var secretKey = "this is our super secret key; only we should know it";
var identifier = "we used our secret key";
var macaroon = MacaroonsBuilder.create(location, secretKey, identifier);
macaroon.serialize();
}
function benchmark_Serialize_with_key_Buffer() {
var location = "http://www.example.org";
var secretKey = new Buffer("this is our super secret key; only we should know it", 'ascii');
var identifier = "we used our secret key";
var macaroon = MacaroonsBuilder.create(location, secretKey, identifier);
macaroon.serialize();
}
function benchmark_DeSerialize_and_verify_with_key_string() {
var secretKey = "this is our super secret key; only we should know it";
var macaroon = MacaroonsBuilder.deserialize(serialized1);
new MacaroonsVerifier(macaroon).assertIsValid(secretKey);
}
function benchmark_DeSerialize_and_verify_with_key_Buffer() {
var secretKey = new Buffer("this is our super secret key; only we should know it",'ascii');
var macaroon = MacaroonsBuilder.deserialize(serialized2);
new MacaroonsVerifier(macaroon).assertIsValid(secretKey);
}
// add tests
suite.add('macaroons.js benchmark')
.add('serialize with secret string', benchmark_Serialize_with_key_string)
.add('serialize with secret Buffer', benchmark_Serialize_with_key_Buffer)
.add('de-serialize and verify with secret string', benchmark_DeSerialize_and_verify_with_key_string)
.add('de-serialize and verify with secret Buffer', benchmark_DeSerialize_and_verify_with_key_Buffer)
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run({'async': true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment