Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/benchmark/test_lzma_benchmark.c
4130 views
1
// From the very useful lzbench project, https://github.com/inikep/lzbench
2
3
#include <stdint.h>
4
#include <string.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <assert.h>
8
9
#include "Alloc.h"
10
#include "LzmaDec.h"
11
#include "LzmaEnc.h"
12
13
static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }
14
static void SzFree(void *p, void *address) { p = p; MyFree(address); }
15
static ISzAlloc g_Alloc = { SzAlloc, SzFree };
16
17
int64_t lzbench_lzma_compress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t level, size_t x, char* y)
18
{
19
CLzmaEncProps props;
20
int res;
21
size_t headerSize = LZMA_PROPS_SIZE;
22
SizeT out_len = outsize - LZMA_PROPS_SIZE;
23
24
LzmaEncProps_Init(&props);
25
props.level = level;
26
LzmaEncProps_Normalize(&props);
27
/*
28
p->level = 5;
29
p->dictSize = p->mc = 0;
30
p->reduceSize = (UInt64)(Int64)-1;
31
p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
32
p->writeEndMark = 0;
33
*/
34
35
res = LzmaEncode((uint8_t*)outbuf+LZMA_PROPS_SIZE, &out_len, (uint8_t*)inbuf, insize, &props, (uint8_t*)outbuf, &headerSize, 0/*int writeEndMark*/, NULL, &g_Alloc, &g_Alloc);
36
if (res != SZ_OK) return 0;
37
38
// printf("out_len=%u LZMA_PROPS_SIZE=%d headerSize=%d\n", (int)(out_len + LZMA_PROPS_SIZE), LZMA_PROPS_SIZE, (int)headerSize);
39
return LZMA_PROPS_SIZE + out_len;
40
}
41
42
int64_t lzbench_lzma_decompress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t x, size_t y, char* z)
43
{
44
int res;
45
SizeT out_len = outsize;
46
SizeT src_len = insize - LZMA_PROPS_SIZE;
47
ELzmaStatus status;
48
49
// SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)
50
res = LzmaDecode((uint8_t*)outbuf, &out_len, (uint8_t*)inbuf+LZMA_PROPS_SIZE, &src_len, (uint8_t*)inbuf, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &g_Alloc);
51
if (res != SZ_OK) return 0;
52
53
// printf("out_len=%u\n", (int)(out_len + LZMA_PROPS_SIZE));
54
return out_len;
55
}
56
57
// main part
58
59
// don't inline, to be friendly to js engine osr
60
void __attribute__ ((noinline)) doit(char *buffer, int size, int i) {
61
static char *buffer2 = NULL;
62
static char *buffer3 = NULL;
63
64
unsigned long maxCompressedSize = size * 2 + 10000; // whatever
65
66
if (!buffer2) buffer2 = (char*)malloc(maxCompressedSize);
67
if (!buffer3) buffer3 = (char*)malloc(size);
68
69
int64_t compressedSize = lzbench_lzma_compress(buffer, size, buffer2, maxCompressedSize, 4 /*level*/, 0, NULL);
70
71
if (i == 0) printf("sizes: %d,%d\n", size, (int32_t)compressedSize);
72
73
int64_t roundTrip = lzbench_lzma_decompress(buffer2, compressedSize, buffer3, size, 0, 0, NULL);
74
75
assert(roundTrip == size);
76
if (i == 0) assert(strcmp(buffer, buffer3) == 0);
77
}
78
79
int main(int argc, char **argv) {
80
int size, iters;
81
int arg = argc > 1 ? argv[1][0] - '0' : 3;
82
switch(arg) {
83
case 0: return 0; break;
84
case 1: size = 100000; iters = 4*1; break;
85
case 2: size = 100000; iters = 4*10; break;
86
case 3: size = 100000; iters = 4*22; break;
87
case 4: size = 100000; iters = 4*125; break;
88
case 5: size = 100000; iters = 4*225; break;
89
default: printf("error: %d\\n", arg); return -1;
90
}
91
92
char *buffer = (char*)malloc(size);
93
94
int i = 0;
95
int run = 0;
96
char runChar = 17;
97
while (i < size) {
98
if (run > 0) {
99
run--;
100
} else {
101
if ((i & 7) == 0) {
102
runChar = i & 7;
103
run = i & 31;
104
} else {
105
runChar = (i*i) % 6714;
106
}
107
}
108
buffer[i] = runChar;
109
i++;
110
}
111
112
for (i = 0; i < iters; i++) {
113
doit(buffer, size, i);
114
}
115
116
printf("ok.\n");
117
118
return 0;
119
}
120
121
122