Last active
January 9, 2020 03:01
-
-
Save freemandealer/948f7750522fba843a9e734990579fd4 to your computer and use it in GitHub Desktop.
test lz4 compress/decompress performance to different buffer size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> // For printf() | |
#include <string.h> // For memcmp() | |
#include <stdlib.h> // For exit() | |
#include <assert.h> | |
#include <time.h> /* for clock_gettime() */ | |
#include <string.h> | |
#include <linux/types.h> | |
#include <stdint.h> | |
#include "lz4.h" | |
#define BILLION 1000000000L | |
#define NUM_ITER_BASE 7000000000 | |
enum {SIZE_4K, SIZE_8K, SIZE_16K, SIZE_32K, SIZE_64K, SIZE_128K, SIZE_256K, SIZE_512K, SIZE_1M}; | |
FILE *g_fp; | |
char *g_src_data; | |
char *g_compressed_data; | |
char *g_decompressed_data; | |
int g_compressed_data_size; | |
void run_screaming(const char* message, const int code) { | |
printf("%s \n", message); | |
exit(code); | |
} | |
void compression_test(size_t size) | |
{ | |
struct timespec start, end; | |
uint64_t time_taken = 0; | |
int i; | |
int num_itr = NUM_ITER_BASE / size; | |
int max_dst_size = LZ4_compressBound(size); | |
g_compressed_data = malloc((size_t)max_dst_size); | |
assert(NULL != g_compressed_data); | |
// warm up | |
for (i = 0; i < 100; i++) { | |
LZ4_compress_default(g_src_data, g_compressed_data, size, max_dst_size); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &start); | |
for (i = 0; i < num_itr; i++) { | |
g_compressed_data_size = LZ4_compress_default(g_src_data, g_compressed_data, size, max_dst_size); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &end); | |
time_taken = BILLION *(end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; | |
printf("avg compress time (size %d) is %lld us\n", size, time_taken / num_itr / 1000); | |
} | |
void decompression_test(size_t size) | |
{ | |
struct timespec start, end; | |
uint64_t time_taken = 0; | |
int decompressed_size; | |
int i; | |
int num_itr = NUM_ITER_BASE / size; | |
char *regen_buffer = malloc(size); | |
assert(NULL != regen_buffer); | |
// warm up | |
for (i = 0; i < 100; i++) { | |
decompressed_size = LZ4_decompress_safe(g_compressed_data, regen_buffer, g_compressed_data_size, size); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &start); | |
for (i = 0; i < num_itr; i++) { | |
decompressed_size = LZ4_decompress_safe(g_compressed_data, regen_buffer, g_compressed_data_size, size); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &end); | |
time_taken = BILLION *(end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; | |
printf("avg decompress time (size %d) is %lld us\n", size, time_taken / num_itr / 1000); | |
assert(decompressed_size == size); | |
assert(memcmp(g_src_data, regen_buffer, size) == 0); | |
} | |
void test(int s) { | |
size_t expect_size; | |
size_t read_size; | |
switch (s) { | |
case SIZE_4K: expect_size = 1 << 12; break; | |
case SIZE_8K: expect_size = 1 << 13; break; | |
case SIZE_16K: expect_size = 1 << 14; break; | |
case SIZE_32K: expect_size = 1 << 15; break; | |
case SIZE_64K: expect_size = 1 << 16; break; | |
case SIZE_128K: expect_size = 1 << 17; break; | |
case SIZE_256K: expect_size = 1 << 18; break; | |
case SIZE_512K: expect_size = 1 << 19; break; | |
case SIZE_1M: expect_size = 1 << 20; break; | |
default: assert(0); | |
} | |
g_src_data = malloc(expect_size); | |
assert(NULL != g_src_data); | |
read_size = fread(g_src_data, 1, expect_size, g_fp); | |
if (read_size != expect_size) { | |
printf("expect size is %d, but read size is %d\n", expect_size, read_size); | |
} | |
compression_test(read_size); | |
decompression_test(read_size); | |
} | |
int main(int argc, char * argv[]) { | |
printf("open file %s for test\n", argv[1]); | |
g_fp = fopen(argv[1], "r"); | |
test(SIZE_4K); | |
test(SIZE_8K); | |
test(SIZE_16K); | |
test(SIZE_32K); | |
test(SIZE_64K); | |
test(SIZE_128K); | |
test(SIZE_256K); | |
test(SIZE_512K); | |
test(SIZE_1M); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment