aboutsummaryrefslogtreecommitdiff
path: root/tests/zlib/benchmark.c
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-01-25 14:27:16 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-01-25 14:27:16 -0800
commit639ed811678379e8af7913356ca98a93a0a3db51 (patch)
tree32b32a4c912ec812d5d668b56189b94bf98e51a9 /tests/zlib/benchmark.c
parent2dbcd8d08d76134a81b232e7c964869dc6942a55 (diff)
improve benchmark infrastructure in preparation for zlib benchmark
Diffstat (limited to 'tests/zlib/benchmark.c')
-rw-r--r--tests/zlib/benchmark.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/zlib/benchmark.c b/tests/zlib/benchmark.c
new file mode 100644
index 00000000..f8ab6205
--- /dev/null
+++ b/tests/zlib/benchmark.c
@@ -0,0 +1,63 @@
+#include "zlib.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+
+
+// don't inline, to be friendly to js engine osr
+void __attribute__ ((noinline)) doit(char *buffer, int size, int i) {
+ static char *buffer2 = NULL;
+ static char *buffer3 = NULL;
+
+ int maxCompressedSize = compressBound(size);
+
+ if (!buffer2) buffer2 = (char*)malloc(maxCompressedSize);
+ if (!buffer3) buffer3 = (char*)malloc(size);
+
+ int compressedSize = maxCompressedSize;
+ compress(buffer2, &compressedSize, buffer, size);
+ if (i == 0) printf("sizes: %d,%d\n", size, compressedSize);
+
+ int decompressedSize = size;
+ uncompress(buffer3, &decompressedSize, buffer2, compressedSize);
+ assert(decompressedSize == size);
+ if (i == 0) assert(strcmp(buffer, buffer3) == 0);
+}
+
+int main(int argc, char **argv) {
+ int size = atoi(argv[1]);
+ int iters = atoi(argv[2]);
+ char *buffer = malloc(size);
+
+ int i = 0;
+ int run = 0;
+ char runChar = 17;
+ int sum = 0;
+ while (i < size) {
+ if (run > 0) {
+ run--;
+ } else {
+ if ((i & 7) == 0) {
+ runChar = i & 7;
+ run = i & 31;
+ } else {
+ runChar = (i*i) % 6714;
+ }
+ }
+ buffer[i] = runChar;
+ sum += buffer[i];
+ if (argc == 100) printf("%d: %d\n", i, buffer[i]); // confuse llvm optimizer, work around possible bug, this is not speed-relevant anyhow
+ i++;
+ }
+ printf("sum: %d\n", sum);
+
+ for (i = 0; i < iters; i++) {
+ doit(buffer, size, i);
+ }
+
+ printf("ok.\n");
+
+ return 0;
+}
+