| 1 | #include <stdio.h> |
| 2 | #include <assert.h> |
| 3 | #include <benchmark/benchmark.h> |
| 4 | #include "benchmark_png_shared.h" |
| 5 | |
| 6 | #define IMWIDTH 1024 |
| 7 | #define IMHEIGHT 1024 |
| 8 | |
| 9 | class png_encode: public benchmark::Fixture { |
| 10 | private: |
| 11 | png_dat outpng; |
| 12 | |
| 13 | /* Backing this on the heap is a more realistic benchmark */ |
| 14 | uint8_t *input_img_buf = NULL; |
| 15 | |
| 16 | public: |
| 17 | /* Let's make the vanilla version have something extremely compressible */ |
| 18 | virtual void init_img(png_bytep img_bytes, size_t width, size_t height) { |
| 19 | init_compressible(img_bytes, width * height); |
| 20 | } |
| 21 | |
| 22 | void SetUp(const ::benchmark::State& state) { |
| 23 | input_img_buf = (uint8_t*)malloc(IMWIDTH * IMHEIGHT * 3); |
| 24 | outpng.buf = (uint8_t*)malloc(IMWIDTH * IMHEIGHT * 3); |
| 25 | /* Using malloc rather than zng_alloc so that we can call realloc. |
| 26 | * IMWIDTH * IMHEIGHT is likely to be more than enough bytes, though, |
| 27 | * given that a simple run length encoding already pretty much can |
| 28 | * reduce to this */ |
| 29 | outpng.len = 0; |
| 30 | outpng.buf_rem = IMWIDTH * IMHEIGHT * 3; |
| 31 | assert(input_img_buf != NULL); |
| 32 | assert(outpng.buf != NULL); |
| 33 | init_img(input_img_buf, IMWIDTH, IMHEIGHT); |
| 34 | } |
| 35 | |
| 36 | /* State in this circumstance will convey the compression level */ |
| 37 | void Bench(benchmark::State &state) { |
| 38 | for (auto _ : state) { |
| 39 | encode_png((png_bytep)input_img_buf, &outpng, state.range(0), IMWIDTH, IMHEIGHT); |
| 40 | outpng.buf_rem = outpng.len; |
| 41 | outpng.len = 0; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void TearDown(const ::benchmark::State &state) { |
| 46 | free(input_img_buf); |
| 47 | free(outpng.buf); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | BENCHMARK_DEFINE_F(png_encode, encode_compressible)(benchmark::State &state) { |
| 52 | Bench(state); |
| 53 | } |
| 54 | BENCHMARK_REGISTER_F(png_encode, encode_compressible)->DenseRange(0, 9, 1)->Unit(benchmark::kMicrosecond); |