Skip to content

Commit a526b51

Browse files
chschollbeardhatcode
authored andcommitted
benchmarks
1 parent d9cdd14 commit a526b51

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

benchmarks/benchmarks.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
__ __ .___ .__
3+
/ \ / \_____ _______ __| _/_ __|__| ____ ____
4+
\ \/\/ /\__ \\_ __ \/ __ | | \ |/ \ / _ \
5+
\ / / __ \| | \/ /_/ | | / | | ( <_> )
6+
\__/\ / (____ /__| \____ |____/|__|___| /\____/
7+
\/ \/ \/ \/
8+
9+
WARDuino (c) by Christophe Scholliers & Robbert Gurdeep Singh
10+
11+
WARDuino is licensed under a
12+
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
13+
14+
You should have received a copy of the license along with this
15+
work. If not, see <http://creativecommons.org/licenses/by-nc-sa/4.0/>.
16+
*/
17+
18+
#include "../WARDuino.h"
19+
#include <iostream>
20+
#include <string.h>
21+
22+
#define MAIN "_main"
23+
#define MAX_PATH 100
24+
#define MAX_BYTE_CODE_SIZE 10000
25+
#define BENCHMARK_PATH "./"
26+
#define append(buf, a, b) snprintf(buf, sizeof(buf), "%s%s", a, b)
27+
#define array_size(x) (sizeof(x) / sizeof((x)[0]))
28+
29+
#define FOR_EACH(type, item, array) \
30+
{ \
31+
for (size_t i = 0; i < array_size(array); i++) \
32+
{ \
33+
type item = array[i];
34+
#define END \
35+
} \
36+
}
37+
38+
typedef char *string;
39+
40+
void set_path(string path, string name)
41+
{
42+
strncpy(path, BENCHMARK_PATH, array_size(path));
43+
strncat(path, name, array_size(path));
44+
strncat(path, "/wast/", array_size(path));
45+
strncat(path, name, array_size(path));
46+
strncat(path, ".wasm", array_size(path));
47+
}
48+
49+
unsigned int read_file_to_buf(unsigned char *bytes, string path)
50+
{
51+
FILE *file = fopen(path, "r");
52+
if (file == NULL)
53+
{
54+
fprintf(stderr, "Cannot open file: %s", path);
55+
exit(1);
56+
}
57+
fseek(file, 0L, SEEK_END);
58+
long num_bytes = ftell(file);
59+
if (num_bytes < MAX_BYTE_CODE_SIZE)
60+
{
61+
fseek(file, 0L, SEEK_SET);
62+
long result = fread(bytes, sizeof(char), num_bytes, file);
63+
if (result != num_bytes)
64+
{
65+
fprintf(stderr, "reading error while loading file %s", path);
66+
exit(1);
67+
}
68+
fclose(file);
69+
return (unsigned int)num_bytes;
70+
}
71+
else
72+
{
73+
fprintf(stderr, "File < %s > is too big for buffer", path);
74+
exit(1);
75+
}
76+
}
77+
78+
void run_benchmarks(string benchmarks[])
79+
{
80+
char path[MAX_PATH];
81+
unsigned char bytes[MAX_BYTE_CODE_SIZE];
82+
unsigned int bytes_length;
83+
WARDuino *w = new WARDuino();
84+
FOR_EACH(char *, name, benchmarks)
85+
set_path(path, name);
86+
printf("Starting test %s \n", path);
87+
bytes_length = read_file_to_buf(bytes, path);
88+
Options opt;
89+
Module* m = w->load_module(bytes, bytes_length,opt);
90+
int fidx = w->get_export_fidx(m,MAIN);
91+
w->invoke(m,fidx);
92+
END
93+
}
94+
95+
int main(int argc, const char *argv[])
96+
{
97+
string benchmarks[] = {"tak"};
98+
run_benchmarks(benchmarks);
99+
return 0;
100+
}

benchmarks/makefile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# __ __ .___ .__
2+
#/ \ / \_____ _______ __| _/_ __|__| ____ ____
3+
#\ \/\/ /\__ \\_ __ \/ __ | | \ |/ \ / _ \
4+
# \ / / __ \| | \/ /_/ | | / | | ( <_> )
5+
# \__/\ / (____ /__| \____ |____/|__|___| /\____/
6+
# \/ \/ \/ \/
7+
CXX = g++
8+
CC = gcc
9+
CFLAGS = -Wall -c
10+
CXXFLAGS = -g -v -std=c++11 -Wall
11+
12+
OUTPUTDIR = ./bin/
13+
MKDIR = mkdir -p $(OUTPUTDIR)
14+
TARGET = warduino_benchmark
15+
16+
COBJECTS= \
17+
../mem.o\
18+
../util.o
19+
20+
CXXSOURCES = \
21+
../WARDuino.cpp \
22+
../primitives.cpp \
23+
benchmarks.cpp
24+
25+
all: warduino
26+
27+
../%.o : ../%.c
28+
$(CC) $(CFLAGS) -c $< -o $@
29+
30+
../wa_sources/%.c: ../wa_sources/%.wast
31+
$(MAKE) -C ../wa_sources
32+
33+
warduino: $(COBJECTS) $(CXXSOURCES) ../wa_sources/*.c
34+
$(MKDIR)
35+
$(CXX) $(CXXFLAGS) $(COBJECTS) $(CXXSOURCES) -o $(OUTPUTDIR)$(TARGET)
36+
37+
38+
run:
39+
./bin/$(TARGET)
40+
41+
.PHONY: clean
42+
clean:
43+
$(MAKE) -C ../wa_sources clean
44+
$(RM) -rf $(OUTPUTDIR)
45+
$(RM) $(COBJECTS)

0 commit comments

Comments
 (0)