Commit 760bfda1 authored by nagayama15's avatar nagayama15

Write a simple benchmark

parent 38bd57e8
#pragma once
int now(void);
void print(int);
void start_warm(void);
void start_bench(void);
......@@ -6,7 +6,7 @@
#include "import.h"
#define RAW_DATA_SIZE ((uInt)2 * 1024 * 1024)
#define RAW_DATA_SIZE ((uInt)20 * 1024 * 1024)
static unsigned char *make_random_bytes(uInt size) {
unsigned char *buffer = malloc(size);
......@@ -18,6 +18,42 @@ static unsigned char *make_random_bytes(uInt size) {
return buffer;
}
export void run_benchmark(void) {
// Make a random byte sequence
srand(0);
unsigned char *raw_bytes = make_random_bytes(RAW_DATA_SIZE);
int level = 9;
int n = 10;
uLongf deflated_size = compressBound(RAW_DATA_SIZE);
unsigned char *deflated_bytes = malloc(deflated_size);
// Warming up
start_warm();
for (int i = 0; i < 5; i++) {
int start_time = now();
compress2(deflated_bytes, &deflated_size, raw_bytes, RAW_DATA_SIZE, level);
int end_time = now();
int duration = end_time - start_time;
print(duration);
}
// Benchmark
start_bench();
for (int i = 0; i < n; i++) {
int start_time = now();
compress2(deflated_bytes, &deflated_size, raw_bytes, RAW_DATA_SIZE, level);
int end_time = now();
int duration = end_time - start_time;
print(duration);
}
}
export int run_test(void) {
// Make a random byte sequence
srand(0);
......
......@@ -4,9 +4,15 @@ const wasm = require('webassembly');
(async () => {
const zlib = await wasm.load(`${__dirname}/build/zlib.wasm`, {
imports: {
now: Date.now,
print: console.log,
start_warm() { console.log('warming up'); },
start_bench() { console.log('benchmark'); },
},
});
assert.strictEqual(zlib.exports.run_test(), 0);
zlib.exports.run_benchmark();
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment