1
1
/* global BigInt */
2
2
/* eslint-disable new-cap, no-new, no-unused-expressions */
3
3
4
- const { ZZ } = require ( '..' ) ;
4
+ const ArgumentParser = require ( 'argparse' ) . ArgumentParser ;
5
5
var benchmark = require ( 'benchmark' ) ;
6
6
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 ;
9
12
try {
10
13
bignum = require ( 'bignum' ) ;
11
14
} catch ( err ) {
12
15
console . log ( 'Load bignum error: ' + err . message . split ( '\n' ) [ 0 ] ) ;
13
16
}
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 ) ;
26
41
27
42
const NFIXTURES = 25 ;
28
43
var fixtures = [ ] ;
@@ -31,13 +46,12 @@ function findexRefresh () {
31
46
if ( ++ findex === fixtures . length ) findex = 0 ;
32
47
}
33
48
34
- const filter = process . argv [ 3 ] ? new RegExp ( process . argv [ 3 ] , 'i' ) : / ./ ;
35
-
36
49
const k256 = 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f' ;
37
50
38
51
function add ( op , ...args ) {
52
+ const key = op + '-' + args . join ( '-' ) ;
39
53
benchmarks . push ( {
40
- name : op ,
54
+ name : key ,
41
55
start : function start ( ) {
42
56
var suite = new benchmark . Suite ( ) ;
43
57
@@ -48,35 +62,31 @@ function add (op, ...args) {
48
62
if ( name === 'BigInt' && typeof BigInt === 'undefined' ) return ;
49
63
if ( name === 'bignum' && bignum === undefined ) return ;
50
64
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
+ } ) ;
79
88
}
89
+ else throw new Error ( 'Too many args.' ) ;
80
90
} ) ;
81
91
82
92
suite
@@ -95,7 +105,6 @@ function add (op, ...args) {
95
105
}
96
106
97
107
function start ( ) {
98
- var re = process . argv [ 2 ] ? new RegExp ( process . argv [ 2 ] , 'i' ) : / ./ ;
99
108
100
109
benchmarks
101
110
. filter ( function ( b ) {
@@ -106,7 +115,7 @@ function start () {
106
115
} ) ;
107
116
}
108
117
109
- if ( / f a s t / i . test ( process . argv [ 4 ] ) ) {
118
+ if ( fast ) {
110
119
console . log ( 'Running in fast mode...' ) ;
111
120
benchmark . options . minTime = 0.3 ;
112
121
benchmark . options . maxTime = 1 ;
0 commit comments