diff --git a/test/wasi/README.md b/test/wasi/README.md index 8f37d45..deb9237 100644 --- a/test/wasi/README.md +++ b/test/wasi/README.md @@ -2,6 +2,8 @@ ```sh wasicc -O3 wasi_printf.c -o wasi_printf.wasm +wasm-opt -O3 wasi_printf.wasm -o wasi_printf.wasm +wasm-strip wasi_printf.wasm ``` ## Run diff --git a/test/wasi/wasi_printf.c b/test/wasi/wasi_printf.c index fccd1cc..5bc173b 100644 --- a/test/wasi/wasi_printf.c +++ b/test/wasi/wasi_printf.c @@ -1,9 +1,15 @@ #include #include +#include #include +/* + * Helpers + */ + #define WASM_EXPORT __attribute__((used)) __attribute__((visibility ("default"))) +static inline struct timespec timespec_diff(struct timespec start, struct timespec end) { struct timespec temp; @@ -17,6 +23,11 @@ struct timespec timespec_diff(struct timespec start, struct timespec end) return temp; } +static inline +int rand_range(int min, int max){ + return min + rand() / (RAND_MAX / (max - min + 1) + 1); +} + WASM_EXPORT uint32_t fib(uint32_t n) { @@ -26,25 +37,80 @@ uint32_t fib(uint32_t n) return fib(n-1) + fib(n-2); } -int main() -{ - struct timespec start, finish; +/* + * Tests + */ - // Chew up some CPU time - volatile int n=38, result; +static char gString[16]; - printf("Calculating fib(%d)...\n", n); +__attribute__((constructor)) +void test_init_some_global() { + static const char data[] = "Constructor OK\n"; + memcpy(gString, data, sizeof(data)); +} +void test_constructor() { + fwrite(gString, 1, sizeof(gString), stdout); +} + +void test_write() { + fwrite("Hello world\n", 1, 12, stdout); +} + +void test_printf() { + printf("Hello %s!\n", "printf"); +} + +void test_random() { + unsigned entropy; + getentropy(&entropy, sizeof(entropy)); + srand(entropy); + int x = rand_range(0, 10); + int y = rand_range(0, 10); + printf("%d + %d = %d\n", x, y, x+y); +} + +void test_gettime() { + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + printf("Now: %lld sec, %ld ns\n", now.tv_sec, now.tv_nsec); +} + +void test_fib10() { + volatile uint32_t n = 10, result; + result = fib(n); + printf("fib(%d) = %d\n", n, result); +} + +void test_perf_fib38() { + struct timespec start, finish; + volatile uint32_t n = 38, result; + + printf("fib(%d) = ", n); + fflush(stdout); + + // Chew up some CPU time clock_gettime(CLOCK_REALTIME, &start); result = fib(n); clock_gettime(CLOCK_REALTIME, &finish); struct timespec delta = timespec_diff(start, finish); + unsigned ms = (delta.tv_sec*1000) + (delta.tv_nsec/1000000); + printf("%d [%u ms]\n", result, ms); +} - printf("Finished in: %lu ms\n", (delta.tv_sec*1000) + (delta.tv_nsec/1000000)); - - // TODO: this fails for some reason - //printf("Finished in: %lf ms\n", delta.tv_sec*1000.0 + delta.tv_nsec/1000000.0); +/* + * Main + */ +int main() +{ + test_write(); + test_constructor(); + test_printf(); + test_gettime(); + //test_random(); + test_fib10(); + test_perf_fib38(); return 0; } diff --git a/test/wasi/wasi_printf.wasm b/test/wasi/wasi_printf.wasm index df44dfe..2cde244 100755 Binary files a/test/wasi/wasi_printf.wasm and b/test/wasi/wasi_printf.wasm differ