Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1027)

Side by Side Diff: third_party/WebKit/Source/platform/testing/ImageDecodeBench.cpp

Issue 2880953002: Measure frame decodes more accurately from ImageDecodeBench
Patch Set: Remove constexpr from variables whos usage is close in proximity Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Provides a minimal wrapping of the Blink image decoders. Used to perform 5 // Provides a minimal wrapping of the Blink image decoders. Used to perform
6 // a non-threaded, memory-to-memory image decode using micro second accuracy 6 // a non-threaded, memory-to-memory image decode using micro second accuracy
7 // clocks to measure image decode time. Optionally applies color correction 7 // clocks to measure image decode time. Optionally applies color correction
8 // during image decoding on supported platforms (default off). Usage: 8 // during image decoding on supported platforms (default off). Usage:
9 // 9 //
10 // % ninja -C out/Release image_decode_bench && 10 // % ninja -C out/Release image_decode_bench &&
11 // ./out/Release/image_decode_bench file [iterations] 11 // ./out/Release/image_decode_bench file [iterations]
12 // 12 //
13 // If --raw-output is specified, the output is formatted for use in a .csv file
scroggo_chromium 2017/06/13 20:55:57 - The description seems to imply that this is the
cblume 2017/06/14 21:18:37 You are right. I hadn't updated the description af
14 // (comma-separated variable).
15 // Each row represents successive frames in an animated image.
16 // (frame 0, frame 1, frame 2...)
17 // Each column represents a single frame decoded from successive iterations.
18 // Iteration 0: (frame 0, frame 1, frame 2...)
19 // Iteration 1: (frame 0, frame 1, frame 2...)
20 //
21 // This means non-animated images will show up as one column.
22 //
23 // This .csv-formatted output is a common format for spreadsheet and data
24 // visualization programs. A user can take timings before and after a change,
25 // import the two .csv files, and overlay their line graphs ontop of each other
26 // to see how the two measurements compare.
27 //
28 // It should tell the user 1.) if the change was an improvement, 2.) if so, how
29 // much, and 3.) if the measurements are noisy and thus reduce our trust in the
30 // improvement.
31 //
32 // Each frame is different and so comparing separate frames may not be fruitful.
33 // For example, frame 1 may cover a large area and require more time to decode.
34 // Frame 2 may have a small rect and be faster to decode.
35 //
13 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute 36 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute
14 // the decoded image frame md5 and output that value. 37 // the decoded image frame md5 and output that value.
15 // 38 //
16 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz, 39 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz,
17 // using the image corpii used to assess Blink image decode performance. Refer 40 // using the image corpii used to assess Blink image decode performance. Refer
18 // to http://crbug.com/398235#c103 and http://crbug.com/258324#c5 41 // to http://crbug.com/398235#c103 and http://crbug.com/258324#c5
19 42
43 #include <cmath>
20 #include <memory> 44 #include <memory>
45 #include <vector>
21 #include "base/command_line.h" 46 #include "base/command_line.h"
22 #include "platform/SharedBuffer.h" 47 #include "platform/SharedBuffer.h"
23 #include "platform/image-decoders/ImageDecoder.h" 48 #include "platform/image-decoders/ImageDecoder.h"
24 #include "platform/wtf/PassRefPtr.h" 49 #include "platform/wtf/PassRefPtr.h"
25 #include "platform/wtf/PtrUtil.h" 50 #include "platform/wtf/PtrUtil.h"
26 #include "public/platform/Platform.h" 51 #include "public/platform/Platform.h"
27 #include "ui/gfx/test/icc_profiles.h" 52 #include "ui/gfx/test/icc_profiles.h"
28 53
29 #if defined(_WIN32) 54 #if defined(_WIN32)
30 #include <mmsystem.h> 55 #include <mmsystem.h>
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 WrapArrayUnique(new unsigned char[file_size]); 222 WrapArrayUnique(new unsigned char[file_size]);
198 if (file_size != fread(buffer.get(), 1, file_size, fp)) { 223 if (file_size != fread(buffer.get(), 1, file_size, fp)) {
199 fprintf(stderr, "Error reading file %s\n", file_name); 224 fprintf(stderr, "Error reading file %s\n", file_name);
200 exit(2); 225 exit(2);
201 } 226 }
202 227
203 fclose(fp); 228 fclose(fp);
204 return SharedBuffer::Create(buffer.get(), file_size); 229 return SharedBuffer::Create(buffer.get(), file_size);
205 } 230 }
206 231
207 bool DecodeImageData(SharedBuffer* data, 232 // This vector represents a single iteration of one (possibly animated) image.
208 bool color_correction, 233 // Each entry is a single timing of a single frame.
209 size_t packet_size) { 234 using FrameTimings = std::vector<double>;
210 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( 235 using IterationsOfFrameTimings = std::vector<FrameTimings>;
211 data, true, ImageDecoder::kAlphaPremultiplied, 236
212 color_correction ? ColorBehavior::TransformToTargetForTesting() 237 void Write2DResultsToFile(const IterationsOfFrameTimings& timings,
213 : ColorBehavior::Ignore()); 238 const char* file_name) {
214 if (!packet_size) { 239 FILE* fp = fopen(file_name, "wb");
240 if (!fp) {
241 fprintf(stderr, "Can't open file %s\n", file_name);
242 exit(2);
243 }
244
245 FrameTimings first_iteration = timings[0];
246 for (size_t i = 0; i < first_iteration.size(); ++i) {
247 fprintf(fp, "\"Frame %zu\",", i);
248 }
249 fprintf(fp, "\n");
250
251 for (const FrameTimings& iteration : timings) {
252 for (double frame_time : iteration) {
253 fprintf(fp, "%f,", frame_time);
254 }
255 fprintf(fp, "\n");
256 }
257 fprintf(fp, "\n");
258
259 fclose(fp);
260 }
261
262 double GetMean(const IterationsOfFrameTimings& timings) {
scroggo_chromium 2017/06/13 20:55:57 The description explains why we shouldn't average
cblume 2017/06/14 21:18:37 Good point. After we talked about the central limi
scroggo_chromium 2017/06/15 14:03:50 Should we report the min, too?
cblume 2017/07/05 08:57:54 I say yes. I just didn't want to rock the boat too
263 size_t count = 0;
264 double total = 0;
265
266 for (const FrameTimings& iteration : timings) {
267 for (double frame_time : iteration) {
268 ++count;
269 total += frame_time;
270 }
271 }
272
273 return total / count;
274 }
275
276 double GetStandardDeviation(const IterationsOfFrameTimings& timings,
277 double mean) {
278 size_t count = 0;
279 double squared_difference_sum = 0;
280
281 for (const FrameTimings& iteration : timings) {
282 for (double frame_time : iteration) {
283 double difference = frame_time - mean;
284 double absolute_difference = std::abs(difference);
285 double squared_difference = std::pow(absolute_difference, 2);
286 ++count;
287 squared_difference_sum += squared_difference;
288 }
289 }
290
291 return std::sqrt(squared_difference_sum / count);
292 }
293
294 IterationsOfFrameTimings TimeDecode(PassRefPtr<SharedBuffer> data,
295 bool apply_color_correction,
296 size_t iterations) {
297 // Find total frame count.
298 std::unique_ptr<ImageDecoder> frame_count_decoder =
299 ImageDecoder::Create(data.Get(), true, ImageDecoder::kAlphaPremultiplied,
300 ColorBehavior::Ignore());
301 size_t frame_count = frame_count_decoder->FrameCount();
302
303 IterationsOfFrameTimings timings(iterations, FrameTimings(frame_count, 0.0));
304
305 for (size_t i = 0; i < iterations; ++i) {
306 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create(
307 data.Get(), true, ImageDecoder::kAlphaPremultiplied,
308 apply_color_correction ? ColorBehavior::TransformToTargetForTesting()
309 : ColorBehavior::Ignore());
215 bool all_data_received = true; 310 bool all_data_received = true;
216 decoder->SetData(data, all_data_received); 311 decoder->SetData(data.Get(), all_data_received);
312 for (size_t frame_index = 0; frame_index < frame_count; ++frame_index) {
313 double start_time = GetCurrentTime();
314 ImageFrame* frame = decoder->FrameBufferAtIndex(frame_index);
315 double elapsed_time = GetCurrentTime() - start_time;
316 if (frame->GetStatus() != ImageFrame::kFrameComplete) {
317 fprintf(stderr, "Image decode failed\n");
318 exit(3);
319 }
320 timings[i][frame_index] = elapsed_time;
321 }
322 }
217 323
218 int frame_count = decoder->FrameCount(); 324 return timings;
219 for (int i = 0; i < frame_count; ++i) { 325 }
220 if (!decoder->FrameBufferAtIndex(i))
221 return false;
222 }
223 326
224 return !decoder->Failed(); 327 // This function mimics deferred decoding in Chromium when not all data has been
225 } 328 // received yet.
329 IterationsOfFrameTimings TimePacketedDecode(PassRefPtr<SharedBuffer> data,
330 bool apply_color_correction,
331 size_t packet_size,
332 size_t iterations) {
333 // Find total frame count.
334 // Doing this requires a decoder with full data (no packet size).
335 std::unique_ptr<ImageDecoder> frame_count_decoder =
336 ImageDecoder::Create(data.Get(), true, ImageDecoder::kAlphaPremultiplied,
337 ColorBehavior::Ignore());
338
339 bool total_all_data_received = true;
340 frame_count_decoder->SetData(data.Get(), total_all_data_received);
341 size_t total_frame_count = frame_count_decoder->FrameCount();
342
343 IterationsOfFrameTimings timings(iterations,
344 FrameTimings(total_frame_count, 0.0));
226 345
227 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); 346 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create();
228 size_t position = 0; 347 size_t position = 0;
229 size_t next_frame_to_decode = 0; 348 size_t next_frame_to_decode = 0;
230 while (true) { 349 while (true) {
231 const char* packet; 350 const char* packet;
232 size_t length = data->GetSomeData(packet, position); 351 size_t length = data->GetSomeData(packet, position);
233 352
234 length = std::min(length, packet_size); 353 length = std::min(length, packet_size);
235 packet_data->Append(packet, length); 354 packet_data->Append(packet, length);
236 position += length; 355 position += length;
237 356
238 bool all_data_received = position == data->size(); 357 bool all_data_received = position == data->size();
239 decoder->SetData(packet_data.Get(), all_data_received);
240 358
241 size_t frame_count = decoder->FrameCount(); 359 for (size_t i = 0; i < iterations; ++i) {
242 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { 360 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create(
243 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); 361 data.Get(), true, ImageDecoder::kAlphaPremultiplied,
244 if (frame->GetStatus() != ImageFrame::kFrameComplete) 362 apply_color_correction ? ColorBehavior::TransformToTargetForTesting()
245 break; 363 : ColorBehavior::Ignore());
364 decoder->SetData(packet_data.Get(), all_data_received);
365
366 size_t frame_count = decoder->FrameCount();
367 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) {
368 double start_time = GetCurrentTime();
369 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode);
370 double elapsed_time = GetCurrentTime() - start_time;
371 if (frame->GetStatus() != ImageFrame::kFrameComplete) {
372 fprintf(stderr, "Image decode failed\n");
373 exit(3);
374 }
375 timings[i][next_frame_to_decode] = elapsed_time;
376 }
377
378 if (all_data_received || decoder->Failed()) {
scroggo_chromium 2017/06/13 20:55:57 Wait, why do we print failure and exit if all_data
cblume 2017/06/14 21:18:37 Good question. all_data_received should not be her
379 fprintf(stderr, "Image decode failed\n");
380 exit(3);
381 }
246 } 382 }
247
248 if (all_data_received || decoder->Failed())
249 break;
250 } 383 }
251 384
252 return !decoder->Failed(); 385 return timings;
253 } 386 }
254 387
255 } // namespace 388 } // namespace
256 389
257 int Main(int argc, char* argv[]) { 390 int Main(int argc, char* argv[]) {
258 base::CommandLine::Init(argc, argv); 391 base::CommandLine::Init(argc, argv);
259 392
260 // If the platform supports color correction, allow it to be controlled. 393 // If the platform supports color correction, allow it to be controlled.
261 394
262 bool apply_color_correction = false; 395 bool apply_color_correction = false;
263
264 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { 396 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) {
265 apply_color_correction = (--argc, ++argv, true); 397 --argc;
398 ++argv;
399 apply_color_correction = true;
266 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); 400 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin();
267 ColorBehavior::SetGlobalTargetColorProfile(profile); 401 ColorBehavior::SetGlobalTargetColorProfile(profile);
268 } 402 }
269 403
404 const char* raw_output_file = nullptr;
405 if (argc >= 2 && strcmp(argv[1], "--raw-output") == 0) {
406 if (argc < 3) {
407 fprintf(stderr,
408 "--raw-output specified without also specifying a file after it");
409 exit(1);
410 }
411
412 raw_output_file = argv[2];
413 argc -= 2;
414 argv += 2;
415 }
416
270 if (argc < 2) { 417 if (argc < 2) {
271 fprintf(stderr, 418 fprintf(stderr,
272 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", 419 "Usage: %s [--color-correct] [--raw-output output_file] file "
scroggo_chromium 2017/06/13 20:55:57 Maybe "file" should now be "input_file", or "image
cblume 2017/06/14 21:18:37 Done.
420 "[iterations] [packetSize]\n",
273 argv[0]); 421 argv[0]);
274 exit(1); 422 exit(1);
275 } 423 }
276 424
277 // Control decode bench iterations and packet size. 425 // Control decode bench iterations and packet size.
278 426
279 size_t iterations = 1; 427 size_t iterations = 1;
280 if (argc >= 3) { 428 if (argc >= 3) {
281 char* end = 0; 429 char* end = 0;
282 iterations = strtol(argv[2], &end, 10); 430 iterations = strtol(argv[2], &end, 10);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 // segments into one, contiguous block of memory. 462 // segments into one, contiguous block of memory.
315 463
316 RefPtr<SharedBuffer> data = ReadFile(argv[1]); 464 RefPtr<SharedBuffer> data = ReadFile(argv[1]);
317 if (!data.Get() || !data->size()) { 465 if (!data.Get() || !data->size()) {
318 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); 466 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]);
319 exit(2); 467 exit(2);
320 } 468 }
321 469
322 data->Data(); 470 data->Data();
323 471
324 // Warm-up: throw out the first iteration for more consistent results. 472 IterationsOfFrameTimings timings;
325 473 if (packet_size) {
326 if (!DecodeImageData(data.Get(), apply_color_correction, packet_size)) { 474 timings = TimePacketedDecode(data.Get(), apply_color_correction,
327 fprintf(stderr, "Image decode failed [%s]\n", argv[1]); 475 packet_size, iterations);
328 exit(3); 476 } else {
477 timings = TimeDecode(data.Get(), apply_color_correction, iterations);
329 } 478 }
330 479
331 // Image decode bench for iterations. 480 if (raw_output_file)
481 Write2DResultsToFile(timings, raw_output_file);
332 482
333 double total_time = 0.0; 483 double mean = GetMean(timings);
484 double standard_deviation = GetStandardDeviation(timings, mean);
334 485
335 for (size_t i = 0; i < iterations; ++i) { 486 printf("mean: %f standard deviation: %f\n", mean, standard_deviation);
336 double start_time = GetCurrentTime();
337 bool decoded =
338 DecodeImageData(data.Get(), apply_color_correction, packet_size);
339 double elapsed_time = GetCurrentTime() - start_time;
340 total_time += elapsed_time;
341 if (!decoded) {
342 fprintf(stderr, "Image decode failed [%s]\n", argv[1]);
343 exit(3);
344 }
345 }
346 487
347 // Results to stdout.
348
349 double average_time = total_time / static_cast<double>(iterations);
350 printf("%f %f\n", total_time, average_time);
351 return 0; 488 return 0;
352 } 489 }
353 490
354 } // namespace blink 491 } // namespace blink
355 492
356 int main(int argc, char* argv[]) { 493 int main(int argc, char* argv[]) {
357 return blink::Main(argc, argv); 494 return blink::Main(argc, argv);
358 } 495 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698