Path: blob/main/test/benchmark/test_lzma_benchmark.c
4130 views
// From the very useful lzbench project, https://github.com/inikep/lzbench12#include <stdint.h>3#include <string.h>4#include <stdio.h>5#include <stdlib.h>6#include <assert.h>78#include "Alloc.h"9#include "LzmaDec.h"10#include "LzmaEnc.h"1112static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }13static void SzFree(void *p, void *address) { p = p; MyFree(address); }14static ISzAlloc g_Alloc = { SzAlloc, SzFree };1516int64_t lzbench_lzma_compress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t level, size_t x, char* y)17{18CLzmaEncProps props;19int res;20size_t headerSize = LZMA_PROPS_SIZE;21SizeT out_len = outsize - LZMA_PROPS_SIZE;2223LzmaEncProps_Init(&props);24props.level = level;25LzmaEncProps_Normalize(&props);26/*27p->level = 5;28p->dictSize = p->mc = 0;29p->reduceSize = (UInt64)(Int64)-1;30p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;31p->writeEndMark = 0;32*/3334res = 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);35if (res != SZ_OK) return 0;3637// printf("out_len=%u LZMA_PROPS_SIZE=%d headerSize=%d\n", (int)(out_len + LZMA_PROPS_SIZE), LZMA_PROPS_SIZE, (int)headerSize);38return LZMA_PROPS_SIZE + out_len;39}4041int64_t lzbench_lzma_decompress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t x, size_t y, char* z)42{43int res;44SizeT out_len = outsize;45SizeT src_len = insize - LZMA_PROPS_SIZE;46ELzmaStatus status;4748// SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)49res = 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);50if (res != SZ_OK) return 0;5152// printf("out_len=%u\n", (int)(out_len + LZMA_PROPS_SIZE));53return out_len;54}5556// main part5758// don't inline, to be friendly to js engine osr59void __attribute__ ((noinline)) doit(char *buffer, int size, int i) {60static char *buffer2 = NULL;61static char *buffer3 = NULL;6263unsigned long maxCompressedSize = size * 2 + 10000; // whatever6465if (!buffer2) buffer2 = (char*)malloc(maxCompressedSize);66if (!buffer3) buffer3 = (char*)malloc(size);6768int64_t compressedSize = lzbench_lzma_compress(buffer, size, buffer2, maxCompressedSize, 4 /*level*/, 0, NULL);6970if (i == 0) printf("sizes: %d,%d\n", size, (int32_t)compressedSize);7172int64_t roundTrip = lzbench_lzma_decompress(buffer2, compressedSize, buffer3, size, 0, 0, NULL);7374assert(roundTrip == size);75if (i == 0) assert(strcmp(buffer, buffer3) == 0);76}7778int main(int argc, char **argv) {79int size, iters;80int arg = argc > 1 ? argv[1][0] - '0' : 3;81switch(arg) {82case 0: return 0; break;83case 1: size = 100000; iters = 4*1; break;84case 2: size = 100000; iters = 4*10; break;85case 3: size = 100000; iters = 4*22; break;86case 4: size = 100000; iters = 4*125; break;87case 5: size = 100000; iters = 4*225; break;88default: printf("error: %d\\n", arg); return -1;89}9091char *buffer = (char*)malloc(size);9293int i = 0;94int run = 0;95char runChar = 17;96while (i < size) {97if (run > 0) {98run--;99} else {100if ((i & 7) == 0) {101runChar = i & 7;102run = i & 31;103} else {104runChar = (i*i) % 6714;105}106}107buffer[i] = runChar;108i++;109}110111for (i = 0; i < iters; i++) {112doit(buffer, size, i);113}114115printf("ok.\n");116117return 0;118}119120121122