Path: blob/main/test/benchmark/test_zlib_benchmark.c
4130 views
/*1* Copyright 2013 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include "zlib.h"8#include <stdio.h>9#include <string.h>10#include <stdlib.h>11#include <assert.h>121314// don't inline, to be friendly to js engine osr15void __attribute__ ((noinline)) doit(unsigned char *buffer, int size, int i) {16static unsigned char *buffer2 = NULL;17static unsigned char *buffer3 = NULL;1819unsigned long maxCompressedSize = compressBound(size);2021if (!buffer2) buffer2 = (unsigned char*)malloc(maxCompressedSize);22if (!buffer3) buffer3 = (unsigned char*)malloc(size);2324unsigned long compressedSize = maxCompressedSize;25compress(buffer2, &compressedSize, buffer, size);26if (i == 0) printf("sizes: %d,%d\n", size, (int)compressedSize);2728unsigned long decompressedSize = size;29uncompress(buffer3, &decompressedSize, buffer2, (int)compressedSize);30assert(decompressedSize == size);31if (i == 0) {32if (strcmp((char*)buffer, (char*)buffer3) != 0) {33puts("incorrect output!");34abort();35}36puts("output looks good");37}38}3940int main(int argc, char **argv) {41int size, iters;42int arg = argc > 1 ? argv[1][0] - '0' : 3;43switch(arg) {44case 0: return 0; break;45case 1: size = 100000; iters = 60; break;46case 2: size = 100000; iters = 250; break;47case 3: size = 100000; iters = 500; break;48case 4: size = 100000; iters = 5*500; break;49case 5: size = 100000; iters = 10*500; break;50default: printf("error: %d\\n", arg); return -1;51}5253unsigned char *buffer = (unsigned char*)malloc(size);5455int i = 0;56int run = 0;57char runChar = 17;58while (i < size) {59if (run > 0) {60run--;61} else {62if ((i & 7) == 0) {63runChar = i & 7;64run = i & 31;65} else {66runChar = (i*i) % 6714;67}68}69buffer[i] = runChar;70i++;71}7273for (i = 0; i < iters; i++) {74doit(buffer, size, i);75}7677printf("ok.\n");7879return 0;80}81828384