|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import 'regenerator-runtime/runtime.js'; |
| 4 | +import {ArgumentParser} from 'argparse'; |
| 5 | + |
| 6 | +import {range} from '@iterable-iterator/range'; |
| 7 | + |
| 8 | +import Benchtable from 'benchtable'; |
| 9 | +import { |
| 10 | + FAST_COUNTER as COUNTER, |
| 11 | + measure, |
| 12 | + measureToString, |
| 13 | +} from '../test/src/_fixtures.js'; |
| 14 | + |
| 15 | +import {dist, dependency, object} from './_fixtures.js'; |
| 16 | + |
| 17 | +const parser = new ArgumentParser(); |
| 18 | +parser.add_argument('filter'); |
| 19 | +const args = parser.parse_args(); |
| 20 | +const filter = new RegExp(args.filter, 'i'); |
| 21 | + |
| 22 | +const cjs = dist('cjs'); |
| 23 | +const module = dist('module.js'); |
| 24 | +const modern = dist('modern.js'); |
| 25 | +const list = dependency('list'); |
| 26 | +const array = object('Array', Array); |
| 27 | + |
| 28 | +const suite = new Benchtable('Tree Construction', {isTransposed: false}); |
| 29 | + |
| 30 | +const add = async (module, fn) => { |
| 31 | + if (!filter.test(module.name)) return; |
| 32 | + if (!filter.test(fn.name)) return; |
| 33 | + const {title, build} = await fn.compile(module); |
| 34 | + suite.addFunction( |
| 35 | + title, |
| 36 | + (measure, n) => { |
| 37 | + build(measure, n); |
| 38 | + }, |
| 39 | + { |
| 40 | + maxTime: 5, |
| 41 | + }, |
| 42 | + ); |
| 43 | + if (!filter.test('measure')) return; |
| 44 | + suite.addFunction( |
| 45 | + `${title}.measure`, |
| 46 | + (measure, n, expected) => { |
| 47 | + const result = build(measure, n).measure(); |
| 48 | + if (result !== expected) { |
| 49 | + throw new Error('wrong measure'); |
| 50 | + } |
| 51 | + }, |
| 52 | + { |
| 53 | + maxTime: 5, |
| 54 | + }, |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +const makeFn = ({name: fnName, build}) => ({ |
| 59 | + name: fnName, |
| 60 | + compile: async (module) => { |
| 61 | + const {name, version, exports} = await module.load(); |
| 62 | + return { |
| 63 | + title: `${name} ${version} #${fnName}`, |
| 64 | + build: build({exports}), |
| 65 | + }; |
| 66 | + }, |
| 67 | +}); |
| 68 | + |
| 69 | +const treePush = makeFn({ |
| 70 | + name: 'push', |
| 71 | + build: ({exports}) => { |
| 72 | + const empty = exports.empty; |
| 73 | + return (measure, n) => { |
| 74 | + let l = empty(measure); |
| 75 | + for (let i = 0; i < n; ++i) l = l.push(i); |
| 76 | + return l; |
| 77 | + }; |
| 78 | + }, |
| 79 | +}); |
| 80 | + |
| 81 | +const listAppend = makeFn({ |
| 82 | + name: 'push', |
| 83 | + build: ({exports}) => { |
| 84 | + const {empty, append} = exports; |
| 85 | + return (_measure, n) => { |
| 86 | + let l = empty(); |
| 87 | + for (let i = 0; i < n; ++i) l = append(i, l); |
| 88 | + return l; |
| 89 | + }; |
| 90 | + }, |
| 91 | +}); |
| 92 | + |
| 93 | +const arrayPush = makeFn({ |
| 94 | + name: 'push', |
| 95 | + build: ({exports}) => { |
| 96 | + assert(exports === Array); |
| 97 | + return (_measure, n) => { |
| 98 | + const l = []; |
| 99 | + for (let i = 0; i < n; ++i) l.push(i); |
| 100 | + return l; |
| 101 | + }; |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +await add(cjs, treePush); |
| 106 | +await add(module, treePush); |
| 107 | +await add(modern, treePush); |
| 108 | + |
| 109 | +await add(list, listAppend); |
| 110 | +await add(array, arrayPush); |
| 111 | + |
| 112 | +const addTitle = (input) => { |
| 113 | + const {measure, n} = input; |
| 114 | + const title = `(${measureToString(measure)}, ${n})`; |
| 115 | + return { |
| 116 | + ...input, |
| 117 | + title, |
| 118 | + }; |
| 119 | +}; |
| 120 | + |
| 121 | +const addExpected = (input) => { |
| 122 | + const expected = measure(input.measure, range(input.n)); |
| 123 | + return { |
| 124 | + ...input, |
| 125 | + expected, |
| 126 | + }; |
| 127 | +}; |
| 128 | + |
| 129 | +const benchmarkInputs = [ |
| 130 | + { |
| 131 | + measure: COUNTER, |
| 132 | + n: 1000, |
| 133 | + }, |
| 134 | + { |
| 135 | + measure: COUNTER, |
| 136 | + n: 10_000, |
| 137 | + }, |
| 138 | + { |
| 139 | + measure: COUNTER, |
| 140 | + n: 100_000, |
| 141 | + }, |
| 142 | +] |
| 143 | + .map(addTitle) |
| 144 | + .map(addExpected); |
| 145 | + |
| 146 | +for (const {title, measure, n, expected} of benchmarkInputs) { |
| 147 | + suite.addInput(title, [measure, n, expected]); |
| 148 | +} |
| 149 | + |
| 150 | +suite.on('cycle', (evt) => { |
| 151 | + console.log(evt.target.name); |
| 152 | +}); |
| 153 | + |
| 154 | +suite.on('complete', () => { |
| 155 | + console.log(suite.table.toString()); |
| 156 | +}); |
| 157 | + |
| 158 | +suite.run(); |
0 commit comments