Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/benchmark/test_zlib_benchmark.c
4130 views
1
/*
2
* Copyright 2013 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include "zlib.h"
9
#include <stdio.h>
10
#include <string.h>
11
#include <stdlib.h>
12
#include <assert.h>
13
14
15
// don't inline, to be friendly to js engine osr
16
void __attribute__ ((noinline)) doit(unsigned char *buffer, int size, int i) {
17
static unsigned char *buffer2 = NULL;
18
static unsigned char *buffer3 = NULL;
19
20
unsigned long maxCompressedSize = compressBound(size);
21
22
if (!buffer2) buffer2 = (unsigned char*)malloc(maxCompressedSize);
23
if (!buffer3) buffer3 = (unsigned char*)malloc(size);
24
25
unsigned long compressedSize = maxCompressedSize;
26
compress(buffer2, &compressedSize, buffer, size);
27
if (i == 0) printf("sizes: %d,%d\n", size, (int)compressedSize);
28
29
unsigned long decompressedSize = size;
30
uncompress(buffer3, &decompressedSize, buffer2, (int)compressedSize);
31
assert(decompressedSize == size);
32
if (i == 0) {
33
if (strcmp((char*)buffer, (char*)buffer3) != 0) {
34
puts("incorrect output!");
35
abort();
36
}
37
puts("output looks good");
38
}
39
}
40
41
int main(int argc, char **argv) {
42
int size, iters;
43
int arg = argc > 1 ? argv[1][0] - '0' : 3;
44
switch(arg) {
45
case 0: return 0; break;
46
case 1: size = 100000; iters = 60; break;
47
case 2: size = 100000; iters = 250; break;
48
case 3: size = 100000; iters = 500; break;
49
case 4: size = 100000; iters = 5*500; break;
50
case 5: size = 100000; iters = 10*500; break;
51
default: printf("error: %d\\n", arg); return -1;
52
}
53
54
unsigned char *buffer = (unsigned char*)malloc(size);
55
56
int i = 0;
57
int run = 0;
58
char runChar = 17;
59
while (i < size) {
60
if (run > 0) {
61
run--;
62
} else {
63
if ((i & 7) == 0) {
64
runChar = i & 7;
65
run = i & 31;
66
} else {
67
runChar = (i*i) % 6714;
68
}
69
}
70
buffer[i] = runChar;
71
i++;
72
}
73
74
for (i = 0; i < iters; i++) {
75
doit(buffer, size, i);
76
}
77
78
printf("ok.\n");
79
80
return 0;
81
}
82
83
84