Skip to content

Commit 50e9ac0

Browse files
🚀 perf(benchmark): Improve UX.
1 parent c84c632 commit 50e9ac0

File tree

1 file changed

+57
-48
lines changed

1 file changed

+57
-48
lines changed

_benchmark/index.js

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
/* global BigInt */
22
/* eslint-disable new-cap, no-new, no-unused-expressions */
33

4-
const { ZZ } = require('..') ;
4+
const ArgumentParser = require('argparse').ArgumentParser;
55
var benchmark = require('benchmark');
66
var crypto = require('crypto');
7-
var bn = require('bn.js');
8-
var bignum;
7+
var XorShift128Plus = require('xorshift.js').XorShift128Plus;
8+
9+
const { ZZ , DEFAULT_DISPLAY_BASE , DEFAULT_REPRESENTATION_BASE } = require('..');
10+
const bn = require('bn.js');
11+
let bignum;
912
try {
1013
bignum = require('bignum');
1114
} catch (err) {
1215
console.log('Load bignum error: ' + err.message.split('\n')[0]);
1316
}
14-
var sjcl = require('eccjs').sjcl.bn;
15-
var bigi = require('bigi');
16-
var BigInteger = require('js-big-integer').BigInteger;
17-
var JSBI = require('jsbi');
18-
var SilentMattBigInteger = require('biginteger').BigInteger;
19-
var XorShift128Plus = require('xorshift.js').XorShift128Plus;
20-
var benchmarks = [];
21-
22-
var selfOnly = process.env.SELF_ONLY;
23-
var seed = process.env.SEED || crypto.randomBytes(16).toString('hex');
24-
console.log('Seed: ' + seed);
25-
var prng = new XorShift128Plus(seed);
17+
const sjcl = require('eccjs').sjcl.bn;
18+
const bigi = require('bigi');
19+
const BigInteger = require('js-big-integer').BigInteger;
20+
const JSBI = require('jsbi');
21+
const SilentMattBigInteger = require('biginteger').BigInteger;
22+
23+
const parser = new ArgumentParser();
24+
parser.addArgument(['-s', '--seed'], {defaultValue: process.env.SEED || crypto.randomBytes(16).toString('hex')});
25+
parser.addArgument(['-l', '--libs'], {defaultValue: '.'});
26+
parser.addArgument(['-b', '--benches'], {defaultValue: '.'});
27+
parser.addArgument(['-f', '--fast'], {action: 'storeTrue'});
28+
const args = parser.parseArgs();
29+
const seed = args.seed;
30+
const filter = new RegExp(args.libs, 'i');
31+
const re = new RegExp(args.benches, 'i');
32+
const fast = args.fast;
33+
34+
const benchmarks = [];
35+
36+
console.log('args:', args);
37+
console.log('DEFAULT_DISPLAY_BASE:', DEFAULT_DISPLAY_BASE);
38+
console.log('DEFAULT_REPRESENTATION_BASE:', DEFAULT_REPRESENTATION_BASE);
39+
40+
const prng = new XorShift128Plus(seed);
2641

2742
const NFIXTURES = 25;
2843
var fixtures = [];
@@ -31,13 +46,12 @@ function findexRefresh () {
3146
if (++findex === fixtures.length) findex = 0;
3247
}
3348

34-
const filter = process.argv[3] ? new RegExp(process.argv[3], 'i') : /./;
35-
3649
const k256 = 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f' ;
3750

3851
function add (op, ...args) {
52+
const key = op + '-' + args.join('-');
3953
benchmarks.push({
40-
name: op,
54+
name: key,
4155
start: function start () {
4256
var suite = new benchmark.Suite();
4357

@@ -48,35 +62,31 @@ function add (op, ...args) {
4862
if (name === 'BigInt' && typeof BigInt === 'undefined') return ;
4963
if (name === 'bignum' && bignum === undefined) return ;
5064

51-
if (!selfOnly || name === '@aureooms/js-integer') {
52-
const opFn = fns[name][op];
53-
if (!opFn) return;
54-
if (!(opFn instanceof Function)) throw new Error(`opFN is not a function: ${opFN}`) ;
55-
const fixture = fixtures[findex][name];
56-
57-
if (args.length === 1) {
58-
const key = op + '-' + args[0];
59-
const x = fixture.args[args[0]];
60-
const outs = fixture.outs;
61-
const testFn = () => outs[key] = opFn(x) ;
62-
suite.add(name + '#' + key, testFn, {
63-
onStart: findexRefresh,
64-
onCycle: findexRefresh
65-
});
66-
}
67-
else if (args.length === 2) {
68-
const key = op + '-' + args[0] + '-' + args[1];
69-
const a = fixture.args[args[0]];
70-
const b = fixture.args[args[1]];
71-
const outs = fixture.outs;
72-
const testFn = () => outs[key] = opFn(a, b) ;
73-
suite.add(name + '#' + key, testFn, {
74-
onStart: findexRefresh,
75-
onCycle: findexRefresh
76-
});
77-
}
78-
else throw new Error('Too many args.') ;
65+
const opFn = fns[name][op];
66+
if (!opFn) return;
67+
if (!(opFn instanceof Function)) throw new Error(`opFN is not a function: ${opFN}`) ;
68+
const fixture = fixtures[findex][name];
69+
70+
if (args.length === 1) {
71+
const x = fixture.args[args[0]];
72+
const outs = fixture.outs;
73+
const testFn = () => outs[key] = opFn(x) ;
74+
suite.add(name + '#' + key, testFn, {
75+
onStart: findexRefresh,
76+
onCycle: findexRefresh
77+
});
78+
}
79+
else if (args.length === 2) {
80+
const a = fixture.args[args[0]];
81+
const b = fixture.args[args[1]];
82+
const outs = fixture.outs;
83+
const testFn = () => outs[key] = opFn(a, b) ;
84+
suite.add(name + '#' + key, testFn, {
85+
onStart: findexRefresh,
86+
onCycle: findexRefresh
87+
});
7988
}
89+
else throw new Error('Too many args.') ;
8090
});
8191

8292
suite
@@ -95,7 +105,6 @@ function add (op, ...args) {
95105
}
96106

97107
function start () {
98-
var re = process.argv[2] ? new RegExp(process.argv[2], 'i') : /./;
99108

100109
benchmarks
101110
.filter(function (b) {
@@ -106,7 +115,7 @@ function start () {
106115
});
107116
}
108117

109-
if (/fast/i.test(process.argv[4])) {
118+
if (fast) {
110119
console.log('Running in fast mode...');
111120
benchmark.options.minTime = 0.3;
112121
benchmark.options.maxTime = 1;

0 commit comments

Comments
 (0)