Skip to content

Commit

Permalink
Merge pull request #11 from dcousens/perfup
Browse files Browse the repository at this point in the history
benchmark: adds performance testing suite
  • Loading branch information
jprichardson committed Jun 12, 2014
2 parents 885fd36 + d50b770 commit 5e01326
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 0 deletions.
67 changes: 67 additions & 0 deletions benchmark/binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var assert = require('assert')
var benchmark = require('benchmark')
benchmark.options.minTime = 1

var LocalBi = require('..')
var NpmBi = require('bigi')

var fixtures = require('./fixtures/ops')

var suites = []
fixtures.forEach(function(f) {
var flist = f.binary.results

for (var name in flist) {
(function(name) {
var suite = new benchmark.Suite()
suite.__name = name

var localBi = new LocalBi(f.dec)
var npmBi = new NpmBi(f.dec)

var func1 = localBi[name]
var func2 = npmBi[name]
var term1 = new LocalBi(f.binary.term)
var term2 = new NpmBi(f.binary.term)

var args1 = [term1]
var args2 = [term2]
var expected = f.binary.results[name]

suite.add('local#' + name, function() {
var actual = func1.apply(localBi, args1)
assert.equal(actual, expected)
})

suite.add('npm#' + name, function() {
var actual = func2.apply(npmBi, args2)
assert.equal(actual, expected)
})

// after each cycle
suite.on('cycle', function (event) {
console.log('*', String(event.target))
})

// other handling
suite.on('complete', function() {
console.log('')
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})

suite.on('error', function(event) {
throw event.target.error
})

suites.push(suite)
})(name)
}
})

// run tests after set up, less chance of error
suites.forEach(function(suite) {
console.log('--------------------------------------------------')
console.log('Benchmarking: ' + suite.__name);

suite.run()
})
82 changes: 82 additions & 0 deletions benchmark/fixtures/gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var BigInteger = require('../..')

function randomNum(bits) {
var num = BigInteger.ZERO

for (var i = 0; i < bits; i += 8) {
var rand = BigInteger.valueOf((Math.random() * 255) & 255)

num = num.add(rand.shiftLeft(i))
}

return num
}

var results = [128, 160, 256].map(function(bits) {
var a, b

while (true) {
a = randomNum(bits)
b = randomNum(bits)

// a = Math.max(a, b), b = Math.min(a, b)
if (b.compareTo(a) >= 0) {
var t = a
a = b
b = t
}

var r1 = a.mod(b).signum() !== 0
var r2 = a.modInverse(b).signum() !== 0

if (r1 && r2) break
}

var results = {
dec: a.toString(10),
hex: a.toString(16),
unary: {
bitLength: {
result: a.bitLength()
},
pow: {
args: [3],
result: a.pow(3).toString()
},
shiftLeft: {
args: [3],
result: a.shiftLeft(3).toString()
},
shiftRight: {
args: [3],
result: a.shiftRight(3).toString()
},
signum: {
result: a.signum().toString()
},
square: {
result: a.square().toString()
},
testBit: {
args: [64],
result: a.testBit(64)
}
},
binary: {
term: b.toString(),
results: {
add: a.add(b).toString(),
compareTo: a.compareTo(b).toString(),
divide: a.divide(b).toString(),
mod: a.mod(b).toString(),
modInverse: a.modInverse(b).toString(),
multiply: a.multiply(b).toString(),
subtract: a.subtract(b).toString()
}
}
}

return results
})

console.log(JSON.stringify(results, null, ' '))
155 changes: 155 additions & 0 deletions benchmark/fixtures/ops.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
[
{
"dec": "229891891649243031202316587505358006929",
"hex": "acf3915bfd831e422c97c6c36a78ea91",
"unary": {
"bitLength": {
"result": 128
},
"pow": {
"args": [
3
],
"result": "12149851267788059177463774748207199757548772786422208190884884358149851976164598026469476248940951704560305702503089"
},
"shiftLeft": {
"args": [
3
],
"result": "1839135133193944249618532700042864055432"
},
"shiftRight": {
"args": [
3
],
"result": "28736486456155378900289573438169750866"
},
"signum": {
"result": "1"
},
"square": {
"result": "52850281846067297744861641329319679717840549375219107203381977813251212011041"
},
"testBit": {
"args": [
64
],
"result": false
}
},
"binary": {
"term": "27007850920266570088725416397760483561",
"results": {
"add": "256899742569509601291042003903118490490",
"compareTo": "10002956",
"divide": "8",
"mod": "13829084287110470492513256323274138441",
"modInv": "5136022155839969873468946829700334052",
"multiply": "6208885937440831019515267281863369302780507589343211993248978978502228594169",
"subtract": "202884040728976461113591171107597523368"
}
}
},
{
"dec": "490701241078624462195409378484496932915350407101",
"hex": "55f3cdfe12d24849c75bdb7fc2704c382af907bd",
"unary": {
"bitLength": {
"result": 159
},
"pow": {
"args": [
3
],
"result": "118154827150164837275616720199701340234590049094867179596937943008626162969467633115265015413316093538497152025452098759905628367182503153451301"
},
"shiftLeft": {
"args": [
3
],
"result": "3925609928628995697563275027875975463322803256808"
},
"shiftRight": {
"args": [
3
],
"result": "61337655134828057774426172310562116614418800887"
},
"signum": {
"result": "1"
},
"square": {
"result": "240787707996102323350671739106280047028893104010568232124124186735687939084637396043966431224201"
},
"testBit": {
"args": [
64
],
"result": true
}
},
"binary": {
"term": "387552020168729810004207236368971589233930434041",
"results": {
"add": "878253261247354272199616614853468522149280841142",
"compareTo": "1",
"divide": "1",
"mod": "103149220909894652191202142115525343681419973060",
"modInverse": "235152901383068743144338660413685036680112108930",
"multiply": "190172257279323816321269536033960416766727056148592941565164984865333639342274033733742978525141",
"subtract": "103149220909894652191202142115525343681419973060"
}
}
},
{
"dec": "101697921761909654222690701334482309860413762802427666847567436346204679706732",
"hex": "e0d6fbe11d315db02500a52d7ef42c15e6e07788c443036981dfd25f54e2186c",
"unary": {
"bitLength": {
"result": 256
},
"pow": {
"args": [
3
],
"result": "1051807429353853597832844242990869492652824533236122313348376594672384268850673765073239982768135199616971893722541919235383066318419560256035949922775874173590415861238057283639850867626928875572630294385257263370212305886211455168"
},
"shiftLeft": {
"args": [
3
],
"result": "813583374095277233781525610675858478883310102419421334780539490769637437653856"
},
"shiftRight": {
"args": [
3
],
"result": "12712240220238706777836337666810288732551720350303458355945929543275584963341"
},
"signum": {
"result": "1"
},
"square": {
"result": "10342467290691497229059351901237659503300418164168932791535853969730985829237713599982870867065893741188006353472411036746768035353826609144891897526119824"
},
"testBit": {
"args": [
64
],
"result": true
}
},
"binary": {
"term": "65942635406350954468277743244650357992528241658403170087048934261572991853681",
"results": {
"add": "167640557168260608690968444579132667852942004460830836934616370607777671560413",
"compareTo": "1295153",
"divide": "1",
"mod": "35755286355558699754412958089831951867885521144024496760518502084631687853051",
"modInv": "59174217759958506263535767481212987808590692586894030343742197030672530958968",
"multiply": "6706228976329212806775323786532344022737471799348149801121032823544329361747450975162606839440452308498537966660800554617999916099539420239249252134680492",
"subtract": "35755286355558699754412958089831951867885521144024496760518502084631687853051"
}
}
}
]
11 changes: 11 additions & 0 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "bigi-benchmark",
"version": "0.0.0",
"description": "",
"license": "MIT",
"dependencies": {
"benchmark": "^1.0.0",
"bn.js": "*",
"bigi": "*"
}
}
63 changes: 63 additions & 0 deletions benchmark/unary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var assert = require('assert')
var benchmark = require('benchmark')
benchmark.options.minTime = 1

var LocalBi = require('..')
var NpmBi = require('bigi')

var fixtures = require('./fixtures/ops')

var suites = []
fixtures.forEach(function(f) {
var flist = f.unary

for (var name in flist) {
(function(name) {
var suite = new benchmark.Suite()
suite.__name = name

var localBi = new LocalBi(f.dec)
var npmBi = new NpmBi(f.dec)

var func1 = localBi[name]
var func2 = npmBi[name]
var args = f.unary[name].args
var expected = f.unary[name].result

suite.add('local#' + name, function() {
var actual = func1.apply(localBi, args)
assert.equal(actual, expected)
})

suite.add('npm#' + name, function() {
var actual = func2.apply(npmBi, args)
assert.equal(actual, expected)
})

// after each cycle
suite.on('cycle', function (event) {
console.log('*', String(event.target))
})

// other handling
suite.on('complete', function() {
console.log('')
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})

suite.on('error', function(event) {
throw event.target.error
})

suites.push(suite)
})(name)
}
})

// run tests after set up, less chance of error
suites.forEach(function(suite) {
console.log('--------------------------------------------------')
console.log('Benchmarking: ' + suite.__name);

suite.run()
})

0 comments on commit 5e01326

Please sign in to comment.