Home | History | Annotate | Download | only in unit_test
      1 /*
      2  *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS. All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "../unit_test/unit_test.h"
     12 
     13 #include <stdlib.h>  // For getenv()
     14 
     15 #include <cstring>
     16 
     17 #include "gflags/gflags.h"
     18 
     19 // Change this to 1000 for benchmarking.
     20 // TODO(fbarchard): Add command line parsing to pass this as option.
     21 #define BENCHMARK_ITERATIONS 1
     22 
     23 unsigned int fastrand_seed = 0xfb;
     24 
     25 DEFINE_int32(libyuv_width, 0, "width of test image.");
     26 DEFINE_int32(libyuv_height, 0, "height of test image.");
     27 DEFINE_int32(libyuv_repeat, 0, "number of times to repeat test.");
     28 DEFINE_int32(libyuv_flags, 0, "cpu flags for reference code. 1 = C, -1 = SIMD");
     29 DEFINE_int32(libyuv_cpu_info,
     30              0,
     31              "cpu flags for benchmark code. 1 = C, -1 = SIMD");
     32 
     33 // For quicker unittests, default is 128 x 72.  But when benchmarking,
     34 // default to 720p.  Allow size to specify.
     35 // Set flags to -1 for benchmarking to avoid slower C code.
     36 
     37 LibYUVConvertTest::LibYUVConvertTest()
     38     : benchmark_iterations_(BENCHMARK_ITERATIONS),
     39       benchmark_width_(128),
     40       benchmark_height_(72),
     41       disable_cpu_flags_(1),
     42       benchmark_cpu_info_(-1) {
     43   const char* repeat = getenv("LIBYUV_REPEAT");
     44   if (repeat) {
     45     benchmark_iterations_ = atoi(repeat);  // NOLINT
     46   }
     47   if (FLAGS_libyuv_repeat) {
     48     benchmark_iterations_ = FLAGS_libyuv_repeat;
     49   }
     50   if (benchmark_iterations_ > 1) {
     51     benchmark_width_ = 1280;
     52     benchmark_height_ = 720;
     53   }
     54   const char* width = getenv("LIBYUV_WIDTH");
     55   if (width) {
     56     benchmark_width_ = atoi(width);  // NOLINT
     57   }
     58   if (FLAGS_libyuv_width) {
     59     benchmark_width_ = FLAGS_libyuv_width;
     60   }
     61   const char* height = getenv("LIBYUV_HEIGHT");
     62   if (height) {
     63     benchmark_height_ = atoi(height);  // NOLINT
     64   }
     65   if (FLAGS_libyuv_height) {
     66     benchmark_height_ = FLAGS_libyuv_height;
     67   }
     68   const char* cpu_flags = getenv("LIBYUV_FLAGS");
     69   if (cpu_flags) {
     70     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
     71   }
     72   if (FLAGS_libyuv_flags) {
     73     disable_cpu_flags_ = FLAGS_libyuv_flags;
     74   }
     75   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
     76   if (cpu_info) {
     77     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
     78   }
     79   if (FLAGS_libyuv_cpu_info) {
     80     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
     81   }
     82   benchmark_pixels_div256_ =
     83       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
     84                             static_cast<double>(Abs(benchmark_height_)) *
     85                             static_cast<double>(benchmark_iterations_) +
     86                         255.0) /
     87                        256.0);
     88   benchmark_pixels_div1280_ =
     89       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
     90                             static_cast<double>(Abs(benchmark_height_)) *
     91                             static_cast<double>(benchmark_iterations_) +
     92                         1279.0) /
     93                        1280.0);
     94 }
     95 
     96 LibYUVColorTest::LibYUVColorTest()
     97     : benchmark_iterations_(BENCHMARK_ITERATIONS),
     98       benchmark_width_(128),
     99       benchmark_height_(72),
    100       disable_cpu_flags_(1),
    101       benchmark_cpu_info_(-1) {
    102   const char* repeat = getenv("LIBYUV_REPEAT");
    103   if (repeat) {
    104     benchmark_iterations_ = atoi(repeat);  // NOLINT
    105   }
    106   if (FLAGS_libyuv_repeat) {
    107     benchmark_iterations_ = FLAGS_libyuv_repeat;
    108   }
    109   if (benchmark_iterations_ > 1) {
    110     benchmark_width_ = 1280;
    111     benchmark_height_ = 720;
    112   }
    113   const char* width = getenv("LIBYUV_WIDTH");
    114   if (width) {
    115     benchmark_width_ = atoi(width);  // NOLINT
    116   }
    117   if (FLAGS_libyuv_width) {
    118     benchmark_width_ = FLAGS_libyuv_width;
    119   }
    120   const char* height = getenv("LIBYUV_HEIGHT");
    121   if (height) {
    122     benchmark_height_ = atoi(height);  // NOLINT
    123   }
    124   if (FLAGS_libyuv_height) {
    125     benchmark_height_ = FLAGS_libyuv_height;
    126   }
    127   const char* cpu_flags = getenv("LIBYUV_FLAGS");
    128   if (cpu_flags) {
    129     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
    130   }
    131   if (FLAGS_libyuv_flags) {
    132     disable_cpu_flags_ = FLAGS_libyuv_flags;
    133   }
    134   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
    135   if (cpu_info) {
    136     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
    137   }
    138   if (FLAGS_libyuv_cpu_info) {
    139     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
    140   }
    141   benchmark_pixels_div256_ =
    142       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    143                             static_cast<double>(Abs(benchmark_height_)) *
    144                             static_cast<double>(benchmark_iterations_) +
    145                         255.0) /
    146                        256.0);
    147   benchmark_pixels_div1280_ =
    148       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    149                             static_cast<double>(Abs(benchmark_height_)) *
    150                             static_cast<double>(benchmark_iterations_) +
    151                         1279.0) /
    152                        1280.0);
    153 }
    154 
    155 LibYUVScaleTest::LibYUVScaleTest()
    156     : benchmark_iterations_(BENCHMARK_ITERATIONS),
    157       benchmark_width_(128),
    158       benchmark_height_(72),
    159       disable_cpu_flags_(1),
    160       benchmark_cpu_info_(-1) {
    161   const char* repeat = getenv("LIBYUV_REPEAT");
    162   if (repeat) {
    163     benchmark_iterations_ = atoi(repeat);  // NOLINT
    164   }
    165   if (FLAGS_libyuv_repeat) {
    166     benchmark_iterations_ = FLAGS_libyuv_repeat;
    167   }
    168   if (benchmark_iterations_ > 1) {
    169     benchmark_width_ = 1280;
    170     benchmark_height_ = 720;
    171   }
    172   const char* width = getenv("LIBYUV_WIDTH");
    173   if (width) {
    174     benchmark_width_ = atoi(width);  // NOLINT
    175   }
    176   if (FLAGS_libyuv_width) {
    177     benchmark_width_ = FLAGS_libyuv_width;
    178   }
    179   const char* height = getenv("LIBYUV_HEIGHT");
    180   if (height) {
    181     benchmark_height_ = atoi(height);  // NOLINT
    182   }
    183   if (FLAGS_libyuv_height) {
    184     benchmark_height_ = FLAGS_libyuv_height;
    185   }
    186   const char* cpu_flags = getenv("LIBYUV_FLAGS");
    187   if (cpu_flags) {
    188     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
    189   }
    190   if (FLAGS_libyuv_flags) {
    191     disable_cpu_flags_ = FLAGS_libyuv_flags;
    192   }
    193   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
    194   if (cpu_info) {
    195     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
    196   }
    197   if (FLAGS_libyuv_cpu_info) {
    198     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
    199   }
    200   benchmark_pixels_div256_ =
    201       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    202                             static_cast<double>(Abs(benchmark_height_)) *
    203                             static_cast<double>(benchmark_iterations_) +
    204                         255.0) /
    205                        256.0);
    206   benchmark_pixels_div1280_ =
    207       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    208                             static_cast<double>(Abs(benchmark_height_)) *
    209                             static_cast<double>(benchmark_iterations_) +
    210                         1279.0) /
    211                        1280.0);
    212 }
    213 
    214 LibYUVRotateTest::LibYUVRotateTest()
    215     : benchmark_iterations_(BENCHMARK_ITERATIONS),
    216       benchmark_width_(128),
    217       benchmark_height_(72),
    218       disable_cpu_flags_(1),
    219       benchmark_cpu_info_(-1) {
    220   const char* repeat = getenv("LIBYUV_REPEAT");
    221   if (repeat) {
    222     benchmark_iterations_ = atoi(repeat);  // NOLINT
    223   }
    224   if (FLAGS_libyuv_repeat) {
    225     benchmark_iterations_ = FLAGS_libyuv_repeat;
    226   }
    227   if (benchmark_iterations_ > 1) {
    228     benchmark_width_ = 1280;
    229     benchmark_height_ = 720;
    230   }
    231   const char* width = getenv("LIBYUV_WIDTH");
    232   if (width) {
    233     benchmark_width_ = atoi(width);  // NOLINT
    234   }
    235   if (FLAGS_libyuv_width) {
    236     benchmark_width_ = FLAGS_libyuv_width;
    237   }
    238   const char* height = getenv("LIBYUV_HEIGHT");
    239   if (height) {
    240     benchmark_height_ = atoi(height);  // NOLINT
    241   }
    242   if (FLAGS_libyuv_height) {
    243     benchmark_height_ = FLAGS_libyuv_height;
    244   }
    245   const char* cpu_flags = getenv("LIBYUV_FLAGS");
    246   if (cpu_flags) {
    247     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
    248   }
    249   if (FLAGS_libyuv_flags) {
    250     disable_cpu_flags_ = FLAGS_libyuv_flags;
    251   }
    252   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
    253   if (cpu_info) {
    254     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
    255   }
    256   if (FLAGS_libyuv_cpu_info) {
    257     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
    258   }
    259   benchmark_pixels_div256_ =
    260       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    261                             static_cast<double>(Abs(benchmark_height_)) *
    262                             static_cast<double>(benchmark_iterations_) +
    263                         255.0) /
    264                        256.0);
    265   benchmark_pixels_div1280_ =
    266       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    267                             static_cast<double>(Abs(benchmark_height_)) *
    268                             static_cast<double>(benchmark_iterations_) +
    269                         1279.0) /
    270                        1280.0);
    271 }
    272 
    273 LibYUVPlanarTest::LibYUVPlanarTest()
    274     : benchmark_iterations_(BENCHMARK_ITERATIONS),
    275       benchmark_width_(128),
    276       benchmark_height_(72),
    277       disable_cpu_flags_(1),
    278       benchmark_cpu_info_(-1) {
    279   const char* repeat = getenv("LIBYUV_REPEAT");
    280   if (repeat) {
    281     benchmark_iterations_ = atoi(repeat);  // NOLINT
    282   }
    283   if (FLAGS_libyuv_repeat) {
    284     benchmark_iterations_ = FLAGS_libyuv_repeat;
    285   }
    286   if (benchmark_iterations_ > 1) {
    287     benchmark_width_ = 1280;
    288     benchmark_height_ = 720;
    289   }
    290   const char* width = getenv("LIBYUV_WIDTH");
    291   if (width) {
    292     benchmark_width_ = atoi(width);  // NOLINT
    293   }
    294   if (FLAGS_libyuv_width) {
    295     benchmark_width_ = FLAGS_libyuv_width;
    296   }
    297   const char* height = getenv("LIBYUV_HEIGHT");
    298   if (height) {
    299     benchmark_height_ = atoi(height);  // NOLINT
    300   }
    301   if (FLAGS_libyuv_height) {
    302     benchmark_height_ = FLAGS_libyuv_height;
    303   }
    304   const char* cpu_flags = getenv("LIBYUV_FLAGS");
    305   if (cpu_flags) {
    306     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
    307   }
    308   if (FLAGS_libyuv_flags) {
    309     disable_cpu_flags_ = FLAGS_libyuv_flags;
    310   }
    311   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
    312   if (cpu_info) {
    313     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
    314   }
    315   if (FLAGS_libyuv_cpu_info) {
    316     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
    317   }
    318   benchmark_pixels_div256_ =
    319       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    320                             static_cast<double>(Abs(benchmark_height_)) *
    321                             static_cast<double>(benchmark_iterations_) +
    322                         255.0) /
    323                        256.0);
    324   benchmark_pixels_div1280_ =
    325       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    326                             static_cast<double>(Abs(benchmark_height_)) *
    327                             static_cast<double>(benchmark_iterations_) +
    328                         1279.0) /
    329                        1280.0);
    330 }
    331 
    332 LibYUVBaseTest::LibYUVBaseTest()
    333     : benchmark_iterations_(BENCHMARK_ITERATIONS),
    334       benchmark_width_(128),
    335       benchmark_height_(72),
    336       disable_cpu_flags_(1),
    337       benchmark_cpu_info_(-1) {
    338   const char* repeat = getenv("LIBYUV_REPEAT");
    339   if (repeat) {
    340     benchmark_iterations_ = atoi(repeat);  // NOLINT
    341   }
    342   if (FLAGS_libyuv_repeat) {
    343     benchmark_iterations_ = FLAGS_libyuv_repeat;
    344   }
    345   if (benchmark_iterations_ > 1) {
    346     benchmark_width_ = 1280;
    347     benchmark_height_ = 720;
    348   }
    349   const char* width = getenv("LIBYUV_WIDTH");
    350   if (width) {
    351     benchmark_width_ = atoi(width);  // NOLINT
    352   }
    353   if (FLAGS_libyuv_width) {
    354     benchmark_width_ = FLAGS_libyuv_width;
    355   }
    356   const char* height = getenv("LIBYUV_HEIGHT");
    357   if (height) {
    358     benchmark_height_ = atoi(height);  // NOLINT
    359   }
    360   if (FLAGS_libyuv_height) {
    361     benchmark_height_ = FLAGS_libyuv_height;
    362   }
    363   const char* cpu_flags = getenv("LIBYUV_FLAGS");
    364   if (cpu_flags) {
    365     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
    366   }
    367   if (FLAGS_libyuv_flags) {
    368     disable_cpu_flags_ = FLAGS_libyuv_flags;
    369   }
    370   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
    371   if (cpu_info) {
    372     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
    373   }
    374   if (FLAGS_libyuv_cpu_info) {
    375     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
    376   }
    377   benchmark_pixels_div256_ =
    378       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    379                             static_cast<double>(Abs(benchmark_height_)) *
    380                             static_cast<double>(benchmark_iterations_) +
    381                         255.0) /
    382                        256.0);
    383   benchmark_pixels_div1280_ =
    384       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
    385                             static_cast<double>(Abs(benchmark_height_)) *
    386                             static_cast<double>(benchmark_iterations_) +
    387                         1279.0) /
    388                        1280.0);
    389 }
    390 
    391 int main(int argc, char** argv) {
    392   ::testing::InitGoogleTest(&argc, argv);
    393   // AllowCommandLineParsing allows us to ignore flags passed on to us by
    394   // Chromium build bots without having to explicitly disable them.
    395   google::AllowCommandLineReparsing();
    396   google::ParseCommandLineFlags(&argc, &argv, true);
    397   return RUN_ALL_TESTS();
    398 }
    399