unit_test.cc 12 KB

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