~jb55/protoverse

ref: e669c2d69908ffea1de08f9da01abe041b28fe62 protoverse/src/bench.c -rw-r--r-- 1.5 KiB
e669c2d6William Casarin hello-c executes to end, but is still broken 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>

#include "io.h"
#include "wasm.h"

static int bench_wasm(unsigned char *wasm, unsigned long len, int times)
{
	struct wasm_parser p;
	struct wasm_interp interp;
	struct timespec t1, t2;
	int i, ops = 0;
	long nanos, ms;

	wasm_parser_init(&p, wasm, len, len*16);

	if (!parse_wasm(&p)) {
		return 0;
	}

	if (!wasm_interp_init(&interp, &p.module)) {
		print_error_backtrace(&interp.errors);
		return 0;
	}
	interp.errors.enabled = 0;

	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
	for (i = 0; i < times; i++) {
		if (!interp_wasm_module(&interp)) {
			//print_error_backtrace(&interp.errors);
		}
		ops += interp.ops;
	}
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);

	nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);
	ms = nanos / 1e6;
	printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\nops\t%d\nns/op\t%ld\n",
		nanos/times, (double)ms/(double)times, nanos, ms, ops, nanos/(ops+1));

	wasm_interp_free(&interp);
	wasm_parser_free(&p);
	return 1;
}

int main(int argc, char *argv[])
{
	unsigned char *wasm_data;
	const char *code_file;
	size_t len;
	int times;

	if (argc >= 2)
		code_file = argv[1];
	else
		code_file = "wasm/hello.wasm";

	if (argc >= 3)
		times = atoi(argv[2]);
	else
		times = 10000;

	if (!map_file(code_file, &wasm_data, &len)) {
		perror("mmap");
		return 1;
	}
	fprintf(stderr, "executing %s %d times\n", code_file, times);
	if (!bench_wasm(wasm_data, len, times)) {
		return 2;
	}

	munmap(wasm_data, len);

	return 0;
}