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