Debian Codegraph @

debian/tests/test_modules/llparse-test-fixture/src/helper.js

raw symbols SCIP
1 #!/usr/bin/env node
2 import { Buffer } from 'buffer';
3 import * as fs from 'fs';
4 import * as path from 'path';
5
6 import * as yargs from 'yargs';
7
8 import DEFAULT_BINDING from '../src/binding';
9
10 const argv = yargs
11 .option('parser', {
12 alias: 'p',
13 describe: 'Parser to use',
14 type: 'string',
15 })
16 .option('binding', {
17 alias: 'b',
18 describe: 'Binding to use',
19 type: 'string',
20 })
21 .option('init', {
22 alias: 'i',
23 describe: 'The name of function from the binding to be used at init',
24 type: 'string',
25 })
26 .demandOption([ 'parser' ])
27 .string('_')
28 .command('bench <input>', 'benchmark input', (yargs) => {
29 return yargs.positional('input', {
30 type: 'string',
31 coerce(opt) {
32 return opt;
33 },
34 });
35 })
36 .command('* <range> <input>', 'scan input', (yargs) => {
37 return yargs
38 .positional('range', { type: 'string' })
39 .positional('input', { type: 'string' });
40 })
41 .help('h')
42 .alias('help', 'h')
43 .argv;
44
45 const IS_BENCH = argv._[0] === 'bench';
46
47 const PARSER_FILE = path.resolve(argv.parser);
48 const EXTRA_BINDINGS = argv.binding ?
49 Array.isArray(argv.binding) ? argv.binding : [ argv.binding ]
50 :
51 [];
52
53 function runOne(binding, p, buf, globalOff = 0) {
54 let paused = false;
55 let code;
56
57 binding.setGlobalOff(globalOff);
58
59 let off = 0;
60 for (;;) {
61 code = p.execute(off === 0 ? buf : buf.slice(off));
62 if (code !== binding.LLPARSE__ERROR_PAUSE) {
63 break;
64 }
65
66 if (paused && p.errorOff === 0) {
67 binding.llparse__debug(p, buf, off, 'Can\'t make progress after pause');
68 return -1;
69 }
70
71 off += p.errorOff;
72 binding.setGlobalOff(globalOff + off);
73
74 binding.llparse__print(0, 'pause');
75
76 // Resume
77 p.error = 0;
78 paused = true;
79 }
80
81 if (code !== 0) {
82 if (code !== p.error) {
83 binding.llparse__print(p.errorOff,
84 'error code mismatch got=%d expected=%d', code, p.error);
85 return -1;
86 }
87
88 binding.llparse__print(p.errorOff, "error code=%d reason=\"%s\"", code,
89 p.reason);
90 }
91
92 return code;
93 }
94
95 function benchmark(binding, Parser, input) {
96 const buf = Buffer.from(input);
97
98 // JS is slower than C, use less bytes
99 const TOTAL = 4 * 1024 * 1024 * 1024;
100 const ITERATIONS = (TOTAL / buf.length) >>> 0;
101
102 const p = new Parser();
103 if (argv.init) {
104 binding[argv.init](p);
105 }
106
107 const start = Date.now();
108 for (let i = 0; i < ITERATIONS; i++) {
109 const code = runOne(binding, p, buf);
110 if (code !== 0) {
111 console.error('got error code %d', code);
112 }
113 }
114 const end = Date.now();
115
116 const secs = (end - start) / 1000;
117
118 const size = (TOTAL / 1024 / 1024).toFixed(2);
119 const bandwidth = (TOTAL / secs / 1024 / 1024).toFixed(2);
120 const ops = (ITERATIONS / secs).toFixed(2);
121
122 console.log('%s mb | %s mb/s | %s ops/sec | %s s',
123 size, bandwidth, ops, secs.toFixed(2));
124 }
125
126 function scan(binding, Parser, range, input) {
127 const buf = Buffer.from(input);
128
129 const [ from, to ] = range.split(':', 2).map((x) => parseInt(x, 10) | 0);
130 if (from < 1 || to < from) {
131 throw new Error('Invalid range');
132 }
133
134 for (let scan = from; scan < to; scan++) {
135 console.log('===== SCAN %d START =====', scan);
136
137 const p = new Parser();
138 if (argv.init) {
139 binding[argv.init](p);
140 }
141 for (let off = 0; off < buf.length; off += scan) {
142 if (runOne(binding, p, buf.slice(off, off + scan), off) !== 0) {
143 break;
144 }
145 }
146 }
147 }
148
149 async function main() {
150 // Load bindings
151 const bindings = [ DEFAULT_BINDING ];
152 for (const extra of EXTRA_BINDINGS) {
153 const m = (await import(path.resolve(extra))).default;
154 bindings.push(m.default || m);
155 }
156
157 // Apply bindings
158 const binding = {};
159 for (const apply of bindings) {
160 apply(binding, IS_BENCH);
161 }
162
163 // Import parser
164 const init = (await import(PARSER_FILE)).default;
165 const Parser = init(binding);
166
167 if (IS_BENCH) {
168 benchmark(binding, Parser, argv.input);
169 } else {
170 scan(binding, Parser, argv.range, argv.input);
171 }
172 }
173
174 main().then(() => {
175 }).catch((e) => {
176 console.error(e.stack);
177 process.exit(1);
178 });