Recursive traversal; compression ratio; grand total; options to disable some features
This commit is contained in:
parent
cbb62ea555
commit
f45f47c9f0
285
qoibench.c
285
qoibench.c
@ -195,8 +195,12 @@ void png_decode_callback(png_structp png, png_bytep data, png_size_t length) {
|
||||
read_data->pos += length;
|
||||
}
|
||||
|
||||
void png_warning_callback(png_structp png_ptr, png_const_charp warning_msg) {
|
||||
// Ingore warnings about sRGB profiles and such.
|
||||
}
|
||||
|
||||
void *libpng_decode(void *data, int size, int *out_w, int *out_h) {
|
||||
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, png_warning_callback);
|
||||
if (!png) {
|
||||
ERROR("png_create_read_struct");
|
||||
}
|
||||
@ -308,6 +312,16 @@ void *fload(const char *path, int *out_size) {
|
||||
// -----------------------------------------------------------------------------
|
||||
// benchmark runner
|
||||
|
||||
|
||||
int opt_runs = 1;
|
||||
int opt_nopng = 0;
|
||||
int opt_nowarmup = 0;
|
||||
int opt_noverify = 0;
|
||||
int opt_nodecode = 0;
|
||||
int opt_noencode = 0;
|
||||
int opt_norecurse = 0;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t size;
|
||||
uint64_t encode_time;
|
||||
@ -315,6 +329,8 @@ typedef struct {
|
||||
} benchmark_lib_result_t;
|
||||
|
||||
typedef struct {
|
||||
int count;
|
||||
uint64_t raw_size;
|
||||
uint64_t px;
|
||||
int w;
|
||||
int h;
|
||||
@ -324,12 +340,59 @@ typedef struct {
|
||||
} benchmark_result_t;
|
||||
|
||||
|
||||
void benchmark_print_result(benchmark_result_t res) {
|
||||
res.px /= res.count;
|
||||
res.raw_size /= res.count;
|
||||
res.libpng.encode_time /= res.count;
|
||||
res.libpng.decode_time /= res.count;
|
||||
res.libpng.size /= res.count;
|
||||
res.stbi.encode_time /= res.count;
|
||||
res.stbi.decode_time /= res.count;
|
||||
res.stbi.size /= res.count;
|
||||
res.qoi.encode_time /= res.count;
|
||||
res.qoi.decode_time /= res.count;
|
||||
res.qoi.size /= res.count;
|
||||
|
||||
double px = res.px;
|
||||
printf(" decode ms encode ms decode mpps encode mpps size kb rate\n");
|
||||
if (!opt_nopng) {
|
||||
printf(
|
||||
"libpng: %8.1f %8.1f %8.2f %8.2f %8d %4.1f%%\n",
|
||||
(double)res.libpng.decode_time/1000000.0,
|
||||
(double)res.libpng.encode_time/1000000.0,
|
||||
(res.libpng.decode_time > 0 ? px / ((double)res.libpng.decode_time/1000.0) : 0),
|
||||
(res.libpng.encode_time > 0 ? px / ((double)res.libpng.encode_time/1000.0) : 0),
|
||||
res.libpng.size/1024,
|
||||
((double)res.libpng.size/(double)res.raw_size) * 100.0
|
||||
);
|
||||
printf(
|
||||
"stbi: %8.1f %8.1f %8.2f %8.2f %8d %4.1f%%\n",
|
||||
(double)res.stbi.decode_time/1000000.0,
|
||||
(double)res.stbi.encode_time/1000000.0,
|
||||
(res.stbi.decode_time > 0 ? px / ((double)res.stbi.decode_time/1000.0) : 0),
|
||||
(res.stbi.encode_time > 0 ? px / ((double)res.stbi.encode_time/1000.0) : 0),
|
||||
res.stbi.size/1024,
|
||||
((double)res.stbi.size/(double)res.raw_size) * 100.0
|
||||
);
|
||||
}
|
||||
printf(
|
||||
"qoi: %8.1f %8.1f %8.2f %8.2f %8d %4.1f%%\n",
|
||||
(double)res.qoi.decode_time/1000000.0,
|
||||
(double)res.qoi.encode_time/1000000.0,
|
||||
(res.qoi.decode_time > 0 ? px / ((double)res.qoi.decode_time/1000.0) : 0),
|
||||
(res.qoi.encode_time > 0 ? px / ((double)res.qoi.encode_time/1000.0) : 0),
|
||||
res.qoi.size/1024,
|
||||
((double)res.qoi.size/(double)res.raw_size) * 100.0
|
||||
);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Run __VA_ARGS__ a number of times and meassure the time taken. The first
|
||||
// run is ignored.
|
||||
#define BENCHMARK_FN(RUNS, AVG_TIME, ...) \
|
||||
#define BENCHMARK_FN(NOWARMUP, RUNS, AVG_TIME, ...) \
|
||||
do { \
|
||||
uint64_t time = 0; \
|
||||
for (int i = 0; i <= RUNS; i++) { \
|
||||
for (int i = NOWARMUP; i <= RUNS; i++) { \
|
||||
uint64_t time_start = ns(); \
|
||||
__VA_ARGS__ \
|
||||
uint64_t time_end = ns(); \
|
||||
@ -341,7 +404,7 @@ typedef struct {
|
||||
} while (0)
|
||||
|
||||
|
||||
benchmark_result_t benchmark_image(const char *path, int runs) {
|
||||
benchmark_result_t benchmark_image(const char *path) {
|
||||
int encoded_png_size;
|
||||
int encoded_qoi_size;
|
||||
int w;
|
||||
@ -358,10 +421,22 @@ benchmark_result_t benchmark_image(const char *path, int runs) {
|
||||
}, &encoded_qoi_size);
|
||||
|
||||
if (!pixels || !encoded_qoi || !encoded_png) {
|
||||
ERROR("Error decoding %s\n", path);
|
||||
ERROR("Error decoding %s", path);
|
||||
}
|
||||
|
||||
// Verify QOI Output
|
||||
if (!opt_noverify) {
|
||||
qoi_desc dc;
|
||||
void *pixels_qoi = qoi_decode(encoded_qoi, encoded_qoi_size, &dc, 4);
|
||||
if (memcmp(pixels, pixels_qoi, w * h * 4) != 0) {
|
||||
ERROR("QOI roundtrip pixel missmatch for %s", path);
|
||||
}
|
||||
free(pixels_qoi);
|
||||
}
|
||||
|
||||
benchmark_result_t res = {0};
|
||||
res.count = 1;
|
||||
res.raw_size = w * h * 4;
|
||||
res.px = w * h;
|
||||
res.w = w;
|
||||
res.h = h;
|
||||
@ -369,41 +444,48 @@ benchmark_result_t benchmark_image(const char *path, int runs) {
|
||||
|
||||
// Decoding
|
||||
|
||||
BENCHMARK_FN(runs, res.libpng.decode_time, {
|
||||
if (!opt_nodecode) {
|
||||
if (!opt_nopng) {
|
||||
BENCHMARK_FN(opt_nowarmup, opt_runs, res.libpng.decode_time, {
|
||||
int dec_w, dec_h;
|
||||
void *dec_p = libpng_decode(encoded_png, encoded_png_size, &dec_w, &dec_h);
|
||||
free(dec_p);
|
||||
});
|
||||
|
||||
BENCHMARK_FN(runs, res.stbi.decode_time, {
|
||||
BENCHMARK_FN(opt_nowarmup, opt_runs, res.stbi.decode_time, {
|
||||
int dec_w, dec_h, dec_channels;
|
||||
void *dec_p = stbi_load_from_memory(encoded_png, encoded_png_size, &dec_w, &dec_h, &dec_channels, 4);
|
||||
free(dec_p);
|
||||
});
|
||||
}
|
||||
|
||||
BENCHMARK_FN(runs, res.qoi.decode_time, {
|
||||
BENCHMARK_FN(opt_nowarmup, opt_runs, res.qoi.decode_time, {
|
||||
qoi_desc desc;
|
||||
void *dec_p = qoi_decode(encoded_qoi, encoded_qoi_size, &desc, 4);
|
||||
free(dec_p);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Encoding
|
||||
|
||||
BENCHMARK_FN(runs, res.libpng.encode_time, {
|
||||
if (!opt_noencode) {
|
||||
if (!opt_nopng) {
|
||||
BENCHMARK_FN(opt_nowarmup, opt_runs, res.libpng.encode_time, {
|
||||
int enc_size;
|
||||
void *enc_p = libpng_encode(pixels, w, h, &enc_size);
|
||||
res.libpng.size = enc_size;
|
||||
free(enc_p);
|
||||
});
|
||||
|
||||
BENCHMARK_FN(runs, res.stbi.encode_time, {
|
||||
BENCHMARK_FN(opt_nowarmup, opt_runs, res.stbi.encode_time, {
|
||||
int enc_size = 0;
|
||||
stbi_write_png_to_func(stbi_write_callback, &enc_size, w, h, 4, pixels, 0);
|
||||
res.stbi.size = enc_size;
|
||||
});
|
||||
}
|
||||
|
||||
BENCHMARK_FN(runs, res.qoi.encode_time, {
|
||||
BENCHMARK_FN(opt_nowarmup, opt_runs, res.qoi.encode_time, {
|
||||
int enc_size;
|
||||
void *enc_p = qoi_encode(pixels, &(qoi_desc){
|
||||
.width = w,
|
||||
@ -414,6 +496,7 @@ benchmark_result_t benchmark_image(const char *path, int runs) {
|
||||
res.qoi.size = enc_size;
|
||||
free(enc_p);
|
||||
});
|
||||
}
|
||||
|
||||
free(pixels);
|
||||
free(encoded_png);
|
||||
@ -422,102 +505,130 @@ benchmark_result_t benchmark_image(const char *path, int runs) {
|
||||
return res;
|
||||
}
|
||||
|
||||
void benchmark_print_result(const char *head, benchmark_result_t res) {
|
||||
double px = res.px;
|
||||
printf("## %s size: %dx%d\n", head, res.w, res.h);
|
||||
printf(" decode ms encode ms decode mpps encode mpps size kb\n");
|
||||
printf(
|
||||
"libpng: %8.1f %8.1f %8.2f %8.2f %8d\n",
|
||||
(double)res.libpng.decode_time/1000000.0,
|
||||
(double)res.libpng.encode_time/1000000.0,
|
||||
(res.libpng.decode_time > 0 ? px / ((double)res.libpng.decode_time/1000.0) : 0),
|
||||
(res.libpng.encode_time > 0 ? px / ((double)res.libpng.encode_time/1000.0) : 0),
|
||||
res.libpng.size/1024
|
||||
);
|
||||
printf(
|
||||
"stbi: %8.1f %8.1f %8.2f %8.2f %8d\n",
|
||||
(double)res.stbi.decode_time/1000000.0,
|
||||
(double)res.stbi.encode_time/1000000.0,
|
||||
(res.stbi.decode_time > 0 ? px / ((double)res.stbi.decode_time/1000.0) : 0),
|
||||
(res.stbi.encode_time > 0 ? px / ((double)res.stbi.encode_time/1000.0) : 0),
|
||||
res.stbi.size/1024
|
||||
);
|
||||
printf(
|
||||
"qoi: %8.1f %8.1f %8.2f %8.2f %8d\n",
|
||||
(double)res.qoi.decode_time/1000000.0,
|
||||
(double)res.qoi.encode_time/1000000.0,
|
||||
(res.qoi.decode_time > 0 ? px / ((double)res.qoi.decode_time/1000.0) : 0),
|
||||
(res.qoi.encode_time > 0 ? px / ((double)res.qoi.encode_time/1000.0) : 0),
|
||||
res.qoi.size/1024
|
||||
);
|
||||
printf("\n");
|
||||
}
|
||||
void benchmark_directory(const char *path, benchmark_result_t *grand_total) {
|
||||
DIR *dir = opendir(path);
|
||||
if (!dir) {
|
||||
ERROR("Couldn't open directory %s", path);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 3) {
|
||||
printf("Usage: qoibench <iterations> <directory>\n");
|
||||
printf("Example: qoibench 10 images/textures/\n");
|
||||
exit(1);
|
||||
struct dirent *file;
|
||||
|
||||
if (!opt_norecurse) {
|
||||
for (int i = 0; (file = readdir(dir)) != NULL; i++) {
|
||||
if (
|
||||
file->d_type & DT_DIR &&
|
||||
strcmp(file->d_name, ".") != 0 &&
|
||||
strcmp(file->d_name, "..") != 0
|
||||
) {
|
||||
char subpath[1024];
|
||||
snprintf(subpath, 1024, "%s/%s", path, file->d_name);
|
||||
benchmark_directory(subpath, grand_total);
|
||||
}
|
||||
}
|
||||
rewinddir(dir);
|
||||
}
|
||||
|
||||
float total_percentage = 0;
|
||||
int total_size = 0;
|
||||
|
||||
benchmark_result_t totals = {0};
|
||||
benchmark_result_t dir_total = {0};
|
||||
|
||||
int runs = atoi(argv[1]);
|
||||
DIR *dir = opendir(argv[2]);
|
||||
if (runs <=0) {
|
||||
runs = 1;
|
||||
}
|
||||
|
||||
if (!dir) {
|
||||
ERROR("Couldn't open directory %s", argv[2]);
|
||||
}
|
||||
|
||||
printf("## Benchmarking %s/*.png -- %d runs\n\n", argv[2], runs);
|
||||
struct dirent *file;
|
||||
int count = 0;
|
||||
for (int i = 0; dir && (file = readdir(dir)) != NULL; i++) {
|
||||
int has_shown_heaad = 0;
|
||||
for (int i = 0; (file = readdir(dir)) != NULL; i++) {
|
||||
if (strcmp(file->d_name + strlen(file->d_name) - 4, ".png") != 0) {
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
|
||||
char *file_path = malloc(strlen(file->d_name) + strlen(argv[2])+8);
|
||||
sprintf(file_path, "%s/%s", argv[2], file->d_name);
|
||||
if (!has_shown_heaad) {
|
||||
has_shown_heaad = 1;
|
||||
printf("## Benchmarking %s/*.png -- %d runs\n\n", path, opt_runs);
|
||||
}
|
||||
|
||||
benchmark_result_t res = benchmark_image(file_path, runs);
|
||||
benchmark_print_result(file_path, res);
|
||||
char *file_path = malloc(strlen(file->d_name) + strlen(path)+8);
|
||||
sprintf(file_path, "%s/%s", path, file->d_name);
|
||||
|
||||
benchmark_result_t res = benchmark_image(file_path);
|
||||
|
||||
printf("## %s size: %dx%d\n", file_path, res.w, res.h);
|
||||
benchmark_print_result(res);
|
||||
|
||||
free(file_path);
|
||||
|
||||
dir_total.count++;
|
||||
dir_total.raw_size += res.raw_size;
|
||||
dir_total.px += res.px;
|
||||
dir_total.libpng.encode_time += res.libpng.encode_time;
|
||||
dir_total.libpng.decode_time += res.libpng.decode_time;
|
||||
dir_total.libpng.size += res.libpng.size;
|
||||
dir_total.stbi.encode_time += res.stbi.encode_time;
|
||||
dir_total.stbi.decode_time += res.stbi.decode_time;
|
||||
dir_total.stbi.size += res.stbi.size;
|
||||
dir_total.qoi.encode_time += res.qoi.encode_time;
|
||||
dir_total.qoi.decode_time += res.qoi.decode_time;
|
||||
dir_total.qoi.size += res.qoi.size;
|
||||
|
||||
totals.px += res.px;
|
||||
totals.libpng.encode_time += res.libpng.encode_time;
|
||||
totals.libpng.decode_time += res.libpng.decode_time;
|
||||
totals.libpng.size += res.libpng.size;
|
||||
totals.stbi.encode_time += res.stbi.encode_time;
|
||||
totals.stbi.decode_time += res.stbi.decode_time;
|
||||
totals.stbi.size += res.stbi.size;
|
||||
totals.qoi.encode_time += res.qoi.encode_time;
|
||||
totals.qoi.decode_time += res.qoi.decode_time;
|
||||
totals.qoi.size += res.qoi.size;
|
||||
grand_total->count++;
|
||||
grand_total->raw_size += res.raw_size;
|
||||
grand_total->px += res.px;
|
||||
grand_total->libpng.encode_time += res.libpng.encode_time;
|
||||
grand_total->libpng.decode_time += res.libpng.decode_time;
|
||||
grand_total->libpng.size += res.libpng.size;
|
||||
grand_total->stbi.encode_time += res.stbi.encode_time;
|
||||
grand_total->stbi.decode_time += res.stbi.decode_time;
|
||||
grand_total->stbi.size += res.stbi.size;
|
||||
grand_total->qoi.encode_time += res.qoi.encode_time;
|
||||
grand_total->qoi.decode_time += res.qoi.decode_time;
|
||||
grand_total->qoi.size += res.qoi.size;
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
totals.px /= count;
|
||||
totals.libpng.encode_time /= count;
|
||||
totals.libpng.decode_time /= count;
|
||||
totals.libpng.size /= count;
|
||||
totals.stbi.encode_time /= count;
|
||||
totals.stbi.decode_time /= count;
|
||||
totals.stbi.size /= count;
|
||||
totals.qoi.encode_time /= count;
|
||||
totals.qoi.decode_time /= count;
|
||||
totals.qoi.size /= count;
|
||||
if (dir_total.count > 0) {
|
||||
printf("## Total for %s\n", path);
|
||||
benchmark_print_result(dir_total);
|
||||
}
|
||||
}
|
||||
|
||||
benchmark_print_result("Totals (AVG)", totals);
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 3) {
|
||||
printf("Usage: qoibench <iterations> <directory> [options]\n");
|
||||
printf("Options:\n");
|
||||
printf(" --nowarmup ... don't perform a warmup run\n");
|
||||
printf(" --nopng ...... don't run png encode/decode\n");
|
||||
printf(" --noverify ... don't verify qoi roundtrip\n");
|
||||
printf(" --noencode ... don't run encoders\n");
|
||||
printf(" --nodecode ... don't run decoders\n");
|
||||
printf(" --norecurse .. don't descend into directories\n");
|
||||
printf("Examples\n");
|
||||
printf(" qoibench 10 images/textures/\n");
|
||||
printf(" qoibench 1 images/textures/ --nopng --nowarmup\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 3; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--nowarmup") == 0) { opt_nowarmup = 1; }
|
||||
else if (strcmp(argv[i], "--nopng") == 0) { opt_nopng = 1; }
|
||||
else if (strcmp(argv[i], "--noverify") == 0) { opt_noverify = 1; }
|
||||
else if (strcmp(argv[i], "--noencode") == 0) { opt_noencode = 1; }
|
||||
else if (strcmp(argv[i], "--nodecode") == 0) { opt_nodecode = 1; }
|
||||
else if (strcmp(argv[i], "--norecurse") == 0) { opt_norecurse = 1; }
|
||||
else { ERROR("Unknown option %s", argv[i]); }
|
||||
}
|
||||
|
||||
opt_runs = atoi(argv[1]);
|
||||
if (opt_runs <=0) {
|
||||
ERROR("Invalid number of runs %d", opt_runs);
|
||||
}
|
||||
|
||||
benchmark_result_t grand_total = {0};
|
||||
benchmark_directory(argv[2], &grand_total);
|
||||
|
||||
if (grand_total.count > 0) {
|
||||
printf("# Grand total for %s\n", argv[2]);
|
||||
benchmark_print_result(grand_total);
|
||||
}
|
||||
else {
|
||||
printf("No images found in %s\n", argv[2]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user