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

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

Issue 2880953002: Measure frame decodes more accurately from ImageDecodeBench
Patch Set: Fix std::pow since we define our own pow. Use fabs instead of std::abs 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 file provided is formatted for use
14 // in a .csv file (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 file 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 on top of each other
26 // to see how the two measurements compare.
27 //
28 // Visualizing the measurements can provide insight into things like an
29 // intermittent noise source or an unexpected measurement.
30 //
31 // Each frame is different and so comparing separate frames may not be fruitful.
32 // For example, frame 1 may cover a large area and require more time to decode.
33 // Frame 2 may have a small rect and be faster to decode.
34 //
13 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute 35 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute
14 // the decoded image frame md5 and output that value. 36 // the decoded image frame md5 and output that value.
15 // 37 //
16 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz, 38 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz,
17 // using the image corpii used to assess Blink image decode performance. Refer 39 // 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 40 // to http://crbug.com/398235#c103 and http://crbug.com/258324#c5
19 41
20 #include <memory> 42 #include <memory>
43 #include <vector>
21 #include "base/command_line.h" 44 #include "base/command_line.h"
22 #include "platform/SharedBuffer.h" 45 #include "platform/SharedBuffer.h"
23 #include "platform/image-decoders/ImageDecoder.h" 46 #include "platform/image-decoders/ImageDecoder.h"
24 #include "platform/wtf/PassRefPtr.h" 47 #include "platform/wtf/PassRefPtr.h"
25 #include "platform/wtf/PtrUtil.h" 48 #include "platform/wtf/PtrUtil.h"
26 #include "public/platform/Platform.h" 49 #include "public/platform/Platform.h"
27 #include "ui/gfx/test/icc_profiles.h" 50 #include "ui/gfx/test/icc_profiles.h"
28 51
29 #if defined(_WIN32) 52 #if defined(_WIN32)
30 #include <mmsystem.h> 53 #include <mmsystem.h>
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 WrapArrayUnique(new unsigned char[file_size]); 220 WrapArrayUnique(new unsigned char[file_size]);
198 if (file_size != fread(buffer.get(), 1, file_size, fp)) { 221 if (file_size != fread(buffer.get(), 1, file_size, fp)) {
199 fprintf(stderr, "Error reading file %s\n", file_name); 222 fprintf(stderr, "Error reading file %s\n", file_name);
200 exit(2); 223 exit(2);
201 } 224 }
202 225
203 fclose(fp); 226 fclose(fp);
204 return SharedBuffer::Create(buffer.get(), file_size); 227 return SharedBuffer::Create(buffer.get(), file_size);
205 } 228 }
206 229
207 bool DecodeImageData(SharedBuffer* data, 230 // This vector represents a single iteration of one (possibly animated) image.
208 bool color_correction, 231 // Each entry is a single timing of a single frame.
209 size_t packet_size) { 232 using FrameTimings = std::vector<double>;
210 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( 233 using IterationsOfFrameTimings = std::vector<FrameTimings>;
211 data, true, ImageDecoder::kAlphaPremultiplied, 234
212 color_correction ? ColorBehavior::TransformToTargetForTesting() 235 void Write2DResultsToFile(const IterationsOfFrameTimings& timings,
213 : ColorBehavior::Ignore()); 236 const char* file_name) {
214 if (!packet_size) { 237 FILE* fp = fopen(file_name, "wb");
238 if (!fp) {
239 fprintf(stderr, "Can't open file %s\n", file_name);
240 exit(2);
241 }
242
243 FrameTimings first_iteration = timings[0];
244 for (size_t i = 0; i < first_iteration.size(); ++i) {
245 fprintf(fp, "\"Frame %zu\",", i);
246 }
247 fprintf(fp, "\n");
248
249 for (const FrameTimings& iteration : timings) {
250 for (double frame_time : iteration) {
251 fprintf(fp, "%f,", frame_time);
252 }
253 fprintf(fp, "\n");
254 }
255 fprintf(fp, "\n");
256
257 fclose(fp);
258 }
259
260 double GetMean(const IterationsOfFrameTimings& timings) {
261 size_t count = 0;
262 double total = 0;
263
264 for (const FrameTimings& iteration : timings) {
265 for (double frame_time : iteration) {
266 ++count;
267 total += frame_time;
268 }
269 }
270
271 return total / count;
272 }
273
274 double GetStandardDeviation(const IterationsOfFrameTimings& timings,
275 double mean) {
276 size_t count = 0;
277 double squared_difference_sum = 0;
278
279 for (const FrameTimings& iteration : timings) {
280 for (double frame_time : iteration) {
281 double difference = frame_time - mean;
282 double absolute_difference = fabs(difference);
283 double squared_difference = pow(absolute_difference, 2);
284 ++count;
285 squared_difference_sum += squared_difference;
286 }
287 }
288
289 return std::sqrt(squared_difference_sum / count);
290 }
291
292 IterationsOfFrameTimings TimeDecode(PassRefPtr<SharedBuffer> data,
293 bool apply_color_correction,
294 size_t iterations) {
295 // Find total frame count.
296 std::unique_ptr<ImageDecoder> frame_count_decoder =
297 ImageDecoder::Create(data.Get(), true, ImageDecoder::kAlphaPremultiplied,
298 ColorBehavior::Ignore());
299 size_t frame_count = frame_count_decoder->FrameCount();
300
301 IterationsOfFrameTimings timings(iterations, FrameTimings(frame_count, 0.0));
302
303 for (size_t i = 0; i < iterations; ++i) {
304 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create(
305 data.Get(), true, ImageDecoder::kAlphaPremultiplied,
306 apply_color_correction ? ColorBehavior::TransformToTargetForTesting()
307 : ColorBehavior::Ignore());
215 bool all_data_received = true; 308 bool all_data_received = true;
216 decoder->SetData(data, all_data_received); 309 decoder->SetData(data.Get(), all_data_received);
310 for (size_t frame_index = 0; frame_index < frame_count; ++frame_index) {
311 double start_time = GetCurrentTime();
312 ImageFrame* frame = decoder->FrameBufferAtIndex(frame_index);
313 double elapsed_time = GetCurrentTime() - start_time;
314 if (frame->GetStatus() != ImageFrame::kFrameComplete) {
315 fprintf(stderr, "Image decode failed\n");
316 exit(3);
317 }
318 timings[i][frame_index] = elapsed_time;
319 }
320 }
217 321
218 int frame_count = decoder->FrameCount(); 322 return timings;
219 for (int i = 0; i < frame_count; ++i) { 323 }
220 if (!decoder->FrameBufferAtIndex(i))
221 return false;
222 }
223 324
224 return !decoder->Failed(); 325 // This function mimics deferred decoding in Chromium when not all data has been
225 } 326 // received yet.
327 IterationsOfFrameTimings TimePacketedDecode(PassRefPtr<SharedBuffer> data,
328 bool apply_color_correction,
329 size_t packet_size,
330 size_t iterations) {
331 // Find total frame count.
332 // Doing this requires a decoder with full data (no packet size).
333 std::unique_ptr<ImageDecoder> frame_count_decoder =
334 ImageDecoder::Create(data.Get(), true, ImageDecoder::kAlphaPremultiplied,
scroggo_chromium 2017/06/15 14:03:50 Why not move "total_all_data_received" up here and
cblume 2017/07/05 08:57:55 Done.
335 ColorBehavior::Ignore());
336
337 bool total_all_data_received = true;
338 frame_count_decoder->SetData(data.Get(), total_all_data_received);
scroggo_chromium 2017/06/15 14:03:51 This line is unnecessary. Create has already set t
cblume 2017/07/05 08:57:55 Done.
339 size_t total_frame_count = frame_count_decoder->FrameCount();
scroggo_chromium 2017/06/15 14:03:50 The start of this method looks like it could use a
cblume 2017/07/05 08:57:55 Done.
340
341 IterationsOfFrameTimings timings(iterations,
342 FrameTimings(total_frame_count, 0.0));
226 343
227 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); 344 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create();
scroggo_chromium 2017/06/15 14:03:51 I think these three variables need to be inside th
cblume 2017/07/05 08:57:55 Done.
228 size_t position = 0; 345 size_t position = 0;
229 size_t next_frame_to_decode = 0; 346 size_t next_frame_to_decode = 0;
230 while (true) {
231 const char* packet;
232 size_t length = data->GetSomeData(packet, position);
233 347
234 length = std::min(length, packet_size); 348 for (size_t i = 0; i < iterations; ++i) {
235 packet_data->Append(packet, length); 349 while (true) {
236 position += length; 350 const char* packet;
351 size_t length = data->GetSomeData(packet, position);
237 352
238 bool all_data_received = position == data->size(); 353 length = std::min(length, packet_size);
239 decoder->SetData(packet_data.Get(), all_data_received); 354 packet_data->Append(packet, length);
355 position += length;
240 356
241 size_t frame_count = decoder->FrameCount(); 357 bool all_data_received = position == data->size();
242 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { 358
243 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); 359 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create(
244 if (frame->GetStatus() != ImageFrame::kFrameComplete) 360 data.Get(), true, ImageDecoder::kAlphaPremultiplied,
scroggo_chromium 2017/06/15 14:03:51 The second parameter here is whether all the data
cblume 2017/07/05 08:57:55 Good catch. I'm embarrassed. :) Thank you for seei
361 apply_color_correction ? ColorBehavior::TransformToTargetForTesting()
362 : ColorBehavior::Ignore());
363 decoder->SetData(packet_data.Get(), all_data_received);
364
365 size_t frame_count = decoder->FrameCount();
366 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) {
367 double start_time = GetCurrentTime();
368 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode);
369 double elapsed_time = GetCurrentTime() - start_time;
370 if (frame->GetStatus() != ImageFrame::kFrameComplete) {
371 fprintf(stderr, "Image decode failed\n");
scroggo_chromium 2017/06/15 14:03:51 In the old code, this was a "break" - we haven't d
Noel Gordon 2017/06/16 17:32:56 Good spot. A second question is: why are we measu
cblume 2017/07/05 08:57:55 Whoops. This shouldn't be a failure case. When we
372 exit(3);
373 }
374 timings[i][next_frame_to_decode] = elapsed_time;
375 }
376
377 if (all_data_received || decoder->Failed()) {
scroggo_chromium 2017/06/15 14:03:50 In patch set 16 you said all_data_received should
cblume 2017/07/05 08:57:55 Done.
378 fprintf(stderr, "Image decode failed\n");
379 exit(3);
380 }
381
382 if (all_data_received)
245 break; 383 break;
246 } 384 }
247
248 if (all_data_received || decoder->Failed())
249 break;
250 } 385 }
251 386
252 return !decoder->Failed(); 387 return timings;
253 } 388 }
254 389
255 } // namespace 390 } // namespace
256 391
257 int Main(int argc, char* argv[]) { 392 int Main(int argc, char* argv[]) {
258 base::CommandLine::Init(argc, argv); 393 base::CommandLine::Init(argc, argv);
259 394
260 // If the platform supports color correction, allow it to be controlled. 395 // If the platform supports color correction, allow it to be controlled.
261 396
262 bool apply_color_correction = false; 397 bool apply_color_correction = false;
263
264 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { 398 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) {
265 apply_color_correction = (--argc, ++argv, true); 399 --argc;
400 ++argv;
401 apply_color_correction = true;
266 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); 402 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin();
267 ColorBehavior::SetGlobalTargetColorProfile(profile); 403 ColorBehavior::SetGlobalTargetColorProfile(profile);
268 } 404 }
269 405
406 const char* raw_output_file = nullptr;
407 if (argc >= 2 && strcmp(argv[1], "--raw-output") == 0) {
408 if (argc < 3) {
409 fprintf(stderr,
410 "--raw-output specified without also specifying a file after it");
411 exit(1);
412 }
413
414 raw_output_file = argv[2];
415 argc -= 2;
416 argv += 2;
417 }
418
270 if (argc < 2) { 419 if (argc < 2) {
271 fprintf(stderr, 420 fprintf(stderr,
272 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", 421 "Usage: %s [--color-correct] [--raw-output output_file] image_file "
422 "[iterations] [packetSize]\n",
273 argv[0]); 423 argv[0]);
274 exit(1); 424 exit(1);
275 } 425 }
276 426
277 // Control decode bench iterations and packet size. 427 // Control decode bench iterations and packet size.
278 428
279 size_t iterations = 1; 429 size_t iterations = 1;
280 if (argc >= 3) { 430 if (argc >= 3) {
281 char* end = 0; 431 char* end = 0;
282 iterations = strtol(argv[2], &end, 10); 432 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. 464 // segments into one, contiguous block of memory.
315 465
316 RefPtr<SharedBuffer> data = ReadFile(argv[1]); 466 RefPtr<SharedBuffer> data = ReadFile(argv[1]);
317 if (!data.Get() || !data->size()) { 467 if (!data.Get() || !data->size()) {
318 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); 468 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]);
319 exit(2); 469 exit(2);
320 } 470 }
321 471
322 data->Data(); 472 data->Data();
323 473
324 // Warm-up: throw out the first iteration for more consistent results. 474 IterationsOfFrameTimings timings;
325 475 if (packet_size) {
326 if (!DecodeImageData(data.Get(), apply_color_correction, packet_size)) { 476 timings = TimePacketedDecode(data.Get(), apply_color_correction,
327 fprintf(stderr, "Image decode failed [%s]\n", argv[1]); 477 packet_size, iterations);
328 exit(3); 478 } else {
479 timings = TimeDecode(data.Get(), apply_color_correction, iterations);
329 } 480 }
330 481
331 // Image decode bench for iterations. 482 if (raw_output_file)
483 Write2DResultsToFile(timings, raw_output_file);
332 484
333 double total_time = 0.0; 485 double mean = GetMean(timings);
486 double standard_deviation = GetStandardDeviation(timings, mean);
334 487
335 for (size_t i = 0; i < iterations; ++i) { 488 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 489
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; 490 return 0;
352 } 491 }
353 492
354 } // namespace blink 493 } // namespace blink
355 494
356 int main(int argc, char* argv[]) { 495 int main(int argc, char* argv[]) {
357 return blink::Main(argc, argv); 496 return blink::Main(argc, argv);
358 } 497 }
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