@ohos-ports/llparse-test-fixture 0.0.0-stage → 5.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/fixture.ts ADDED
@@ -0,0 +1,224 @@
1
+ import { spawn } from 'node:child_process';
2
+ import * as crypto from 'node:crypto';
3
+ import * as fs from 'node:fs';
4
+ import * as os from 'node:os';
5
+ import * as path from 'node:path';
6
+
7
+ import { FixtureResult, IFixtureResultOptions } from './result';
8
+
9
+ export { FixtureResult, IFixtureResultOptions };
10
+
11
+ const CLANG = process.env.CLANG || 'clang';
12
+ const CFLAGS = process.env.CFLAGS || '';
13
+ const WASM = process.env.WASM;
14
+ const WASM_CFLAGS = process.env.WASM_CFLAGS || '';
15
+
16
+ const NATIVE_DIR = path.join(__dirname, '..', 'src', 'native');
17
+ const FIXTURE = path.join(NATIVE_DIR, 'fixture.c');
18
+
19
+ export interface IFixtureOptions {
20
+ readonly buildDir: string;
21
+ readonly clang?: string;
22
+ readonly wasm?: string;
23
+ readonly extra?: ReadonlyArray<string>;
24
+ readonly maxParallel?: number;
25
+ }
26
+
27
+ export interface IFixtureBuildOptions {
28
+ readonly extra?: ReadonlyArray<string>;
29
+ }
30
+
31
+ export interface IFixtureArtifacts {
32
+ readonly header: string;
33
+ readonly c: string;
34
+ }
35
+
36
+ interface IFixtureInternalOptions {
37
+ readonly buildDir: string;
38
+ readonly clang: string;
39
+ readonly wasm: string | undefined;
40
+ readonly extra: ReadonlyArray<string>;
41
+ readonly maxParallel: number;
42
+ }
43
+
44
+ // Just a random value, really
45
+ export const ERROR_PAUSE = 0x7fa73caa;
46
+
47
+ export class Fixture {
48
+ private readonly options: IFixtureInternalOptions;
49
+
50
+ constructor(options: IFixtureOptions) {
51
+ this.options = {
52
+ buildDir: options.buildDir,
53
+ clang: options.clang === undefined ? CLANG : options.clang,
54
+ wasm: options.wasm === undefined ? WASM : options.wasm,
55
+ extra: options.extra || [],
56
+ maxParallel: options.maxParallel === undefined ?
57
+ os.cpus().length :
58
+ options.maxParallel,
59
+ };
60
+
61
+ try {
62
+ fs.mkdirSync(this.options.buildDir, { recursive: true });
63
+ } catch (e) {
64
+ // no-op
65
+ }
66
+ }
67
+
68
+ public async build(artifacts: IFixtureArtifacts, name: string,
69
+ options: IFixtureBuildOptions = {}): Promise<FixtureResult> {
70
+ const BUILD_DIR = this.options.buildDir;
71
+
72
+ const hash = crypto.createHash('sha256');
73
+
74
+ const c = path.join(BUILD_DIR, name + '.c');
75
+ const header = path.join(BUILD_DIR, name + '.h');
76
+
77
+ hash.update('header');
78
+ hash.update(artifacts.header);
79
+ await fs.promises.writeFile(header, artifacts.header);
80
+
81
+ const commonArgs = [
82
+ '-g3', '-Os', '-fvisibility=hidden',
83
+ '-I', NATIVE_DIR,
84
+ '-I', BUILD_DIR,
85
+ '-include', header,
86
+ FIXTURE,
87
+ ];
88
+
89
+ const args = {
90
+ c: [ '-msse4.2' ],
91
+ wasm: [
92
+ '-msimd128',
93
+ '-fno-exceptions',
94
+ '-mexec-model=reactor',
95
+ '-Wl,-error-limit=0',
96
+ '-Wl,--allow-undefined',
97
+ '-Wl,--export-dynamic',
98
+ '-Wl,--export-table',
99
+ '-Wl,--export=malloc',
100
+ '-Wl,--export=free',
101
+ '-Wl,--no-entry',
102
+ ],
103
+ };
104
+
105
+ // This is rather lame, but should work
106
+ if (CFLAGS) {
107
+ for (const flag of CFLAGS.split(/\s+/g)) {
108
+ args.c.push(flag);
109
+ }
110
+ }
111
+ if (WASM_CFLAGS) {
112
+ for (const flag of WASM_CFLAGS.split(/\s+/g)) {
113
+ args.wasm.push(flag);
114
+ }
115
+ }
116
+
117
+ hash.update('c');
118
+ hash.update(artifacts.c);
119
+ await fs.promises.writeFile(c, artifacts.c);
120
+ args.c.push(c);
121
+ args.wasm.push(c);
122
+
123
+ for (const extra of this.options.extra) {
124
+ commonArgs.push(extra);
125
+ }
126
+ if (options.extra) {
127
+ for (const extra of options.extra) {
128
+ commonArgs.push(extra);
129
+ }
130
+ }
131
+ hash.update('common-args');
132
+ hash.update(commonArgs.join(' '));
133
+
134
+ const digest = hash.digest('hex');
135
+
136
+ const executables: string[] = [];
137
+
138
+ {
139
+ const out = path.join(BUILD_DIR, name + '-c.' + digest);
140
+ const link = path.join(BUILD_DIR, name + '-c');
141
+ if (!fs.existsSync(out)) {
142
+ await this.clang(
143
+ this.options.clang, commonArgs.concat(args.c, '-o', out));
144
+ }
145
+ try {
146
+ await fs.promises.unlink(link);
147
+ } catch (e) {
148
+ // no-op
149
+ }
150
+ try {
151
+ await fs.promises.link(out, link);
152
+ } catch (e) {
153
+ // Some file systems (e.g. hmdfs on HarmonyOS) do not support
154
+ // hard links and return EPERM. Fall back to copying the file.
155
+ // copyFile preserves the source file's mode (executable bit).
156
+ await fs.promises.copyFile(out, link);
157
+ try {
158
+ fs.accessSync(link, fs.constants.X_OK);
159
+ } catch (e2) {
160
+ await fs.promises.chmod(link, 0o755);
161
+ }
162
+ }
163
+ executables.push(out);
164
+ }
165
+
166
+ if (this.options.wasm) {
167
+ const out = path.join(BUILD_DIR, name + '-' + digest + '.wasm');
168
+ const link = path.join(BUILD_DIR, name + '.wasm');
169
+ if (!fs.existsSync(out)) {
170
+ await this.clang(
171
+ this.options.wasm, commonArgs.concat(args.wasm, '-o', out));
172
+ }
173
+ try {
174
+ await fs.promises.unlink(link);
175
+ } catch (e) {
176
+ // no-op
177
+ }
178
+ try {
179
+ await fs.promises.link(out, link);
180
+ } catch (e) {
181
+ // Some file systems (e.g. hmdfs on HarmonyOS) do not support
182
+ // hard links and return EPERM. Fall back to copying the file.
183
+ // copyFile preserves the source file's mode (executable bit).
184
+ await fs.promises.copyFile(out, link);
185
+ try {
186
+ fs.accessSync(link, fs.constants.X_OK);
187
+ } catch (e2) {
188
+ await fs.promises.chmod(link, 0o755);
189
+ }
190
+ }
191
+ executables.push(out);
192
+ }
193
+
194
+ return new FixtureResult(executables, this.options.maxParallel);
195
+ }
196
+
197
+ private async clang(bin: string, args: ReadonlyArray<string>): Promise<void> {
198
+ const proc = spawn(bin, args, {
199
+ stdio: [ null, 'pipe', 'pipe' ],
200
+ });
201
+
202
+ const stdout: Buffer[] = [];
203
+ proc.stdout.on('data', (chunk: Buffer) => stdout.push(chunk));
204
+ const stderr: Buffer[] = [];
205
+ proc.stderr.on('data', (chunk: Buffer) => stderr.push(chunk));
206
+
207
+ const code = await (new Promise((resolve) => {
208
+ proc.once('exit', exitCode => resolve(exitCode!));
209
+ }) as Promise<number>);
210
+
211
+ if (code !== 0) {
212
+ if (stdout.length > 0) {
213
+ process.stderr.write(Buffer.concat(stdout).toString());
214
+ }
215
+ if (stderr.length > 0) {
216
+ process.stderr.write(Buffer.concat(stderr).toString());
217
+ }
218
+
219
+ const escapedArgs = args.map(arg => JSON.stringify(arg));
220
+ throw new Error('clang exit code: ' + code +
221
+ `\narguments: ${escapedArgs.join(' ')}`);
222
+ }
223
+ }
224
+ }
@@ -0,0 +1,460 @@
1
+ #include <assert.h>
2
+ #include <inttypes.h>
3
+ #include <stdarg.h>
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "fixture.h"
9
+
10
+ #if defined(__wasm__)
11
+ extern double wasm_get_time();
12
+ extern void wasm_print(int stream, const char* str);
13
+
14
+ static int wasm__stdout = 0;
15
+ static int wasm__stderr = 1;
16
+
17
+ static double get_time() {
18
+ return wasm_get_time();
19
+ }
20
+
21
+ static void wasm__printf(int stream, const char* fmt, ...) {
22
+ va_list ap;
23
+ char buf[16384];
24
+
25
+ if (llparse__in_bench)
26
+ return;
27
+
28
+ va_start(ap, fmt);
29
+ vsnprintf(buf, sizeof(buf), fmt, ap);
30
+ va_end(ap);
31
+
32
+ wasm_print(stream, buf);
33
+ }
34
+
35
+ #define fprintf(stream, ...) wasm__printf(wasm__##stream, __VA_ARGS__)
36
+
37
+ #elif defined(_MSC_VER)
38
+ #if defined(_MSC_EXTENSIONS)
39
+ #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
40
+ #else
41
+ #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
42
+ #endif
43
+
44
+ #include <windows.h>
45
+
46
+ static double get_time() {
47
+ FILETIME ft;
48
+ unsigned __int64 tmpres = 0;
49
+
50
+ GetSystemTimeAsFileTime(&ft);
51
+
52
+ tmpres |= ft.dwHighDateTime;
53
+ tmpres <<= 32;
54
+ tmpres |= ft.dwLowDateTime;
55
+
56
+ tmpres /= 10; /*convert into microseconds*/
57
+ /*converting file time to unix epoch*/
58
+ tmpres -= DELTA_EPOCH_IN_MICROSECS;
59
+
60
+ return (double) tmpres / 1e6;
61
+ }
62
+ #else
63
+ #include <sys/time.h>
64
+
65
+ static double get_time() {
66
+ struct timeval tv;
67
+ gettimeofday(&tv, NULL);
68
+
69
+ return (double) tv.tv_sec + tv.tv_usec * 1e-6;
70
+ }
71
+ #endif /* defined(_MSC_VER) */
72
+
73
+ #ifdef LLPARSE__TEST_INIT
74
+ void LLPARSE__TEST_INIT(llparse_t* p);
75
+ #endif /* LLPARSE__TEST_INIT */
76
+
77
+ #ifdef LLPARSE__TEST_FINISH
78
+ void LLPARSE__TEST_FINISH(llparse_t* p);
79
+ #endif /* LLPARSE__TEST_FINISH */
80
+
81
+ /* NOTE: include of parser is inserted through `-include` clang argument */
82
+
83
+ /* 8 gb */
84
+ static const int64_t kBytes = 8LL << 30;
85
+
86
+ int llparse__in_bench = 0;
87
+ static int llparse__in_loop;
88
+ static const char* start;
89
+
90
+ void llparse__print(const char* p, const char* endp,
91
+ const char* fmt, ...) {
92
+ va_list ap;
93
+ char buf[16384];
94
+ int len;
95
+
96
+ if (llparse__in_bench)
97
+ return;
98
+
99
+ va_start(ap, fmt);
100
+ len = vsnprintf(buf, sizeof(buf), fmt, ap);
101
+ va_end(ap);
102
+
103
+ if (p == NULL) {
104
+ if (len == 0)
105
+ fprintf(stdout, "off=NULL\n");
106
+ else
107
+ fprintf(stdout, "off=NULL %s\n", buf);
108
+ } else {
109
+ if (len == 0)
110
+ fprintf(stdout, "off=%d\n", (int) (p - start));
111
+ else
112
+ fprintf(stdout, "off=%d %s\n", (int) (p - start), buf);
113
+ }
114
+ return;
115
+ }
116
+
117
+
118
+ int llparse__print_span(const char* name, const char* p, const char* endp) {
119
+ if (llparse__in_bench)
120
+ return 0;
121
+
122
+ for (;;) {
123
+ const char* cr;
124
+ const char* lf;
125
+ int len;
126
+
127
+ cr = memchr(p, '\r', endp - p);
128
+ lf = memchr(p, '\n', endp - p);
129
+ if (cr != NULL && lf != NULL) {
130
+ if (cr < lf) {
131
+ lf = NULL;
132
+ } else {
133
+ cr = NULL;
134
+ }
135
+ }
136
+
137
+ if (lf != NULL) {
138
+ len = (int) (lf - p);
139
+ } else if (cr != NULL) {
140
+ len = (int) (cr - p);
141
+ } else {
142
+ len = (int) (endp - p);
143
+ }
144
+ if (len != 0 || (lf == NULL && cr == NULL)) {
145
+ llparse__print(p, endp, "len=%d span[%s]=\"%.*s\"",
146
+ len, name, len, p);
147
+ p += len;
148
+ }
149
+
150
+ if (lf != NULL) {
151
+ llparse__print(p, endp, "len=1 span[%s]=lf", name);
152
+ assert(p != endp);
153
+ p++;
154
+ } else if (cr != NULL) {
155
+ llparse__print(p, endp, "len=1 span[%s]=cr", name);
156
+ assert(p != endp);
157
+ p++;
158
+ }
159
+
160
+ if (p == endp)
161
+ break;
162
+ }
163
+ return 0;
164
+ }
165
+
166
+
167
+ void llparse__debug(llparse_t* s, const char* p, const char* endp,
168
+ const char* msg) {
169
+ if (p == endp) {
170
+ fprintf(stderr, "off=%-3d next=null debug=%s\n", (int) (p - start), msg);
171
+ } else {
172
+ fprintf(stderr, "off=%-3d next=%02x debug=%s\n", (int) (p - start), *p,
173
+ msg);
174
+ }
175
+ }
176
+
177
+
178
+ static int llparse__run_one(llparse_t* s, const char* input, int len) {
179
+ int code;
180
+ const char* endp;
181
+ unsigned int paused;
182
+
183
+ endp = input + len;
184
+
185
+ paused = 0;
186
+ for (;;) {
187
+ code = llparse_execute(s, input, endp);
188
+
189
+ if (code != LLPARSE__ERROR_PAUSE)
190
+ break;
191
+
192
+ if (paused && input == s->error_pos) {
193
+ llparse__debug(s, input, endp, "Can\'t make progress after pause");
194
+ return -1;
195
+ }
196
+
197
+ llparse__print(s->error_pos, endp, "pause");
198
+
199
+ /* Resume */
200
+ s->error = 0;
201
+ input = s->error_pos;
202
+ paused = 1;
203
+ }
204
+
205
+ if (code != 0) {
206
+ if (code != s->error) {
207
+ llparse__print(s->error_pos, endp,
208
+ "error code mismatch got=%d expected=%d", code, s->error);
209
+ return -1;
210
+ }
211
+
212
+ llparse__print(s->error_pos, endp, "error code=%d reason=\"%s\"", code,
213
+ s->reason);
214
+ }
215
+
216
+ return code;
217
+ }
218
+
219
+
220
+ static int llparse__run_loop(const char* input, int len) {
221
+ llparse_t s;
222
+
223
+ llparse_init(&s);
224
+ #ifdef LLPARSE__TEST_INIT
225
+ LLPARSE__TEST_INIT(&s);
226
+ #endif /* LLPARSE__TEST_INIT */
227
+
228
+ for (;;) {
229
+ int code;
230
+
231
+ code = llparse__run_one(&s, input, len);
232
+ if (code != 0)
233
+ return code;
234
+ }
235
+
236
+ #ifdef LLPARSE__TEST_FINISH
237
+ LLPARSE__TEST_FINISH(&s);
238
+ #endif /* LLPARSE__TEST_FINISH */
239
+
240
+ return 0;
241
+ }
242
+
243
+
244
+ static int llparse__run_bench(const char* input, int len) {
245
+ llparse_t s;
246
+ int64_t i;
247
+ double start;
248
+ double end;
249
+ double bw;
250
+ double time;
251
+ double total;
252
+ int64_t iterations;
253
+
254
+ llparse_init(&s);
255
+ #ifdef LLPARSE__TEST_INIT
256
+ LLPARSE__TEST_INIT(&s);
257
+ #endif /* LLPARSE__TEST_INIT */
258
+
259
+ iterations = kBytes / (int64_t) len;
260
+
261
+ start = get_time();
262
+ for (i = 0; i < iterations; i++) {
263
+ int code;
264
+
265
+ code = llparse__run_one(&s, input, len);
266
+ if (code != 0)
267
+ return code;
268
+ }
269
+ end = get_time();
270
+
271
+ #ifdef LLPARSE__TEST_FINISH
272
+ LLPARSE__TEST_FINISH(&s);
273
+ #endif /* LLPARSE__TEST_FINISH */
274
+
275
+ time = end - start;
276
+ total = (double) iterations * len;
277
+ bw = (double) total / time;
278
+
279
+ fprintf(stdout, "%.2f mb | %.2f mb/s | %.2f ops/sec | %.2f s\n",
280
+ (double) total / (1024 * 1024),
281
+ bw / (1024 * 1024),
282
+ (double) iterations / time,
283
+ time);
284
+
285
+ return 0;
286
+ }
287
+
288
+
289
+ static int llparse__run_scan(int scan, const char* input, int len) {
290
+ llparse_t s;
291
+ llparse_init(&s);
292
+ #ifdef LLPARSE__TEST_INIT
293
+ LLPARSE__TEST_INIT(&s);
294
+ #endif /* LLPARSE__TEST_INIT */
295
+
296
+ if (scan <= 0) {
297
+ fprintf(stderr, "Invalid scan value\n");
298
+ return -1;
299
+ }
300
+
301
+ while (len > 0) {
302
+ int max;
303
+ int code;
304
+
305
+ max = len > scan ? scan : len;
306
+
307
+ code = llparse__run_one(&s, input, max);
308
+
309
+ /* Continue with next scan */
310
+ if (code != 0)
311
+ return 0;
312
+
313
+ input += max;
314
+ len -= max;
315
+ }
316
+
317
+ #ifdef LLPARSE__TEST_FINISH
318
+ LLPARSE__TEST_FINISH(&s);
319
+ #endif /* LLPARSE__TEST_FINISH */
320
+
321
+ return 0;
322
+ }
323
+
324
+
325
+ #if !defined(__wasm__)
326
+ static int llparse__run_stdin() {
327
+ llparse_t s;
328
+
329
+ llparse_init(&s);
330
+ #ifdef LLPARSE__TEST_INIT
331
+ LLPARSE__TEST_INIT(&s);
332
+ #endif /* LLPARSE__TEST_INIT */
333
+
334
+ for (;;) {
335
+ char buf[16384];
336
+ const char* input;
337
+ int code;
338
+
339
+ input = fgets(buf, sizeof(buf), stdin);
340
+ if (input == NULL)
341
+ break;
342
+
343
+ start = input;
344
+ code = llparse__run_one(&s, input, strlen(input));
345
+ if (code != 0) {
346
+ fprintf(stderr, "code=%d error=%d reason=%s\n", code, s.error, s.reason);
347
+ return -1;
348
+ }
349
+ }
350
+
351
+ #ifdef LLPARSE__TEST_FINISH
352
+ LLPARSE__TEST_FINISH(&s);
353
+ #endif /* LLPARSE__TEST_FINISH */
354
+
355
+ return 0;
356
+ }
357
+ #endif /* !defined(__wasm__) */
358
+
359
+
360
+ static int llparse__print_usage(const char* exec) {
361
+ fprintf(stderr, "Usage:\n");
362
+ fprintf(stderr, " %s <from>:to [input]\n", exec);
363
+ fprintf(stderr, " %s bench [input]\n", exec);
364
+ #if !defined(__wasm__)
365
+ fprintf(stderr, " %s -\n", exec);
366
+ #endif /* !defined(__wasm__) */
367
+ return -1;
368
+ }
369
+
370
+
371
+ #if defined(__wasm__)
372
+ __attribute__((visibility("default")))
373
+ #endif /* !defined(__wasm__) */
374
+ int run(const char* exec, const char* spec, const char* input) {
375
+ int len;
376
+ struct {
377
+ int from;
378
+ int to;
379
+ } scan;
380
+ int i;
381
+
382
+ if (strcmp(spec, "bench") == 0) {
383
+ llparse__in_bench = 1;
384
+ } else if (strcmp(spec, "loop") == 0) {
385
+ llparse__in_bench = 1;
386
+ llparse__in_loop = 1;
387
+ } else {
388
+ const char* colon;
389
+ const char* p;
390
+
391
+ p = spec;
392
+ colon = strchr(p, ':');
393
+ if (colon == NULL)
394
+ return llparse__print_usage(exec);
395
+
396
+ scan.from = 0;
397
+ scan.to = 0;
398
+
399
+ while (p < colon) {
400
+ char ch = *p;
401
+ if (ch < '0' || ch > '9') {
402
+ return llparse__print_usage(exec);
403
+ }
404
+ scan.from *= 10;
405
+ scan.from += ch - '0';
406
+ p++;
407
+ }
408
+ p++;
409
+ while (1) {
410
+ char ch = *p;
411
+ if (ch == 0) {
412
+ break;
413
+ }
414
+ if (ch < '0' || ch > '9') {
415
+ return llparse__print_usage(exec);
416
+ }
417
+ scan.to *= 10;
418
+ scan.to += ch - '0';
419
+ p++;
420
+ }
421
+ }
422
+
423
+ len = strlen(input);
424
+
425
+ if (llparse__in_bench && len == 0) {
426
+ fprintf(stderr, "Input can\'t be empty for benchmark");
427
+ return -1;
428
+ }
429
+
430
+ start = input;
431
+
432
+ if (llparse__in_loop)
433
+ return llparse__run_loop(input, len);
434
+
435
+ if (llparse__in_bench)
436
+ return llparse__run_bench(input, len);
437
+
438
+ for (i = scan.from; i < scan.to; i++) {
439
+ int err;
440
+
441
+ fprintf(stdout, "===== SCAN %d START =====\n", i);
442
+ err = llparse__run_scan(i, input, len);
443
+ if (err != 0)
444
+ return err;
445
+ }
446
+
447
+ return 0;
448
+ }
449
+
450
+ #if !defined(__wasm__)
451
+ int main(int argc, char** argv) {
452
+ if (argc >= 2 && strcmp(argv[1], "-") == 0)
453
+ return llparse__run_stdin();
454
+
455
+ if (argc < 3)
456
+ return llparse__print_usage(argv[0]);
457
+
458
+ return run(argv[0], argv[1], argv[2]);
459
+ }
460
+ #endif /* !defined(__wasm__) */
@@ -0,0 +1,13 @@
1
+ #ifndef SRC_LLPARSE_FIXTURE_H_
2
+ #define SRC_LLPARSE_FIXTURE_H_
3
+
4
+ #ifndef LLPARSE__ERROR_PAUSE
5
+ # define LLPARSE__ERROR_PAUSE 0x7fa73caa
6
+ #endif /* LLPARSE__ERROR_PAUSE */
7
+
8
+ extern int llparse__in_bench;
9
+
10
+ void llparse__print(const char* p, const char* endp, const char* fmt, ...);
11
+ int llparse__print_span(const char* name, const char* p, const char* endp);
12
+
13
+ #endif /* SRC_LLPARSE_FIXTURE_H_ */