OLD | NEW |
---|---|
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 Loading... | |
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, | |
212 color_correction ? ColorBehavior::TransformToTargetForTesting() | |
213 : ColorBehavior::Ignore()); | |
214 if (!packet_size) { | |
215 bool all_data_received = true; | |
216 decoder->SetData(data, all_data_received); | |
217 | 234 |
218 int frame_count = decoder->FrameCount(); | 235 void Write2DResultsToFile(const IterationsOfFrameTimings& timings, |
219 for (int i = 0; i < frame_count; ++i) { | 236 const char* file_name) { |
220 if (!decoder->FrameBufferAtIndex(i)) | 237 FILE* fp = fopen(file_name, "wb"); |
221 return false; | 238 if (!fp) { |
222 } | 239 fprintf(stderr, "Can't open file %s\n", file_name); |
223 | 240 exit(2); |
224 return !decoder->Failed(); | |
225 } | 241 } |
226 | 242 |
227 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); | 243 FrameTimings first_iteration = timings[0]; |
228 size_t position = 0; | 244 for (size_t i = 0; i < first_iteration.size(); ++i) { |
229 size_t next_frame_to_decode = 0; | 245 fprintf(fp, "\"Frame %zu\",", i); |
230 while (true) { | 246 } |
231 const char* packet; | 247 fprintf(fp, "\n"); |
232 size_t length = data->GetSomeData(packet, position); | |
233 | 248 |
234 length = std::min(length, packet_size); | 249 for (const FrameTimings& iteration : timings) { |
235 packet_data->Append(packet, length); | 250 for (double frame_time : iteration) { |
236 position += length; | 251 fprintf(fp, "%f,", frame_time); |
252 } | |
253 fprintf(fp, "\n"); | |
254 } | |
255 fprintf(fp, "\n"); | |
237 | 256 |
238 bool all_data_received = position == data->size(); | 257 fclose(fp); |
239 decoder->SetData(packet_data.Get(), all_data_received); | 258 } |
240 | 259 |
241 size_t frame_count = decoder->FrameCount(); | 260 class MeanAndMin { |
242 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { | 261 public: |
243 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); | 262 MeanAndMin(double mean, double min) : mean_(mean), min_(min) {} |
244 if (frame->GetStatus() != ImageFrame::kFrameComplete) | 263 double mean_; |
264 double min_; | |
265 }; | |
266 | |
267 MeanAndMin GetMeanAndMin(const IterationsOfFrameTimings& timings) { | |
268 size_t count = 0; | |
269 double total = 0.0; | |
270 | |
271 double min = 0.0; | |
Noel Gordon
2017/07/17 02:17:08
This can't be right. The minimum reported by this
cblume
2017/07/17 05:47:29
Done.
| |
272 | |
273 for (const FrameTimings& iteration : timings) { | |
274 for (double frame_time : iteration) { | |
275 ++count; | |
276 total += frame_time; | |
277 min = std::min(min, frame_time); | |
278 } | |
279 } | |
280 | |
281 return MeanAndMin(total / count, min); | |
282 } | |
283 | |
284 double GetStandardDeviation(const IterationsOfFrameTimings& timings, | |
285 double mean) { | |
286 size_t count = 0; | |
287 double squared_difference_sum = 0; | |
288 | |
289 for (const FrameTimings& iteration : timings) { | |
290 for (double frame_time : iteration) { | |
291 double difference = frame_time - mean; | |
292 double absolute_difference = fabs(difference); | |
293 double squared_difference = pow(absolute_difference, 2); | |
294 ++count; | |
295 squared_difference_sum += squared_difference; | |
296 } | |
297 } | |
298 | |
299 return std::sqrt(squared_difference_sum / count); | |
300 } | |
301 | |
302 size_t GetFrameCount(const PassRefPtr<SharedBuffer>& data) { | |
303 auto decoder = ImageDecoder::Create(data.Get(), true, | |
304 ImageDecoder::kAlphaPremultiplied, ColorBehavior::Ignore()); | |
305 if (!decoder) { | |
306 fprintf(stderr, "Image decode failed\n"); | |
307 exit(3); | |
308 } | |
309 size_t frame_count = decoder->FrameCount(); | |
310 if (decoder->Failed()) { | |
311 fprintf(stderr, "Image decode failed\n"); | |
312 exit(3); | |
313 } | |
314 return frame_count; | |
315 } | |
316 | |
317 IterationsOfFrameTimings TimeDecode(const RefPtr<SharedBuffer>& data, | |
318 bool apply_color_correction, | |
319 size_t iterations) { | |
320 size_t frame_count = GetFrameCount(data); | |
321 | |
322 IterationsOfFrameTimings timings(iterations, FrameTimings(frame_count, 0.0)); | |
323 | |
324 for (size_t i = 0; i < iterations; ++i) { | |
325 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( | |
326 data.Get(), true, ImageDecoder::kAlphaPremultiplied, | |
327 apply_color_correction ? ColorBehavior::TransformToTargetForTesting() | |
328 : ColorBehavior::Ignore()); | |
329 bool all_data_received = true; | |
330 decoder->SetData(data.Get(), all_data_received); | |
331 for (size_t frame_index = 0; frame_index < frame_count; ++frame_index) { | |
332 double start_time = GetCurrentTime(); | |
333 ImageFrame* frame = decoder->FrameBufferAtIndex(frame_index); | |
334 double elapsed_time = GetCurrentTime() - start_time; | |
335 if (frame->GetStatus() != ImageFrame::kFrameComplete) { | |
336 fprintf(stderr, "Image decode failed\n"); | |
337 exit(3); | |
338 } | |
339 timings[i][frame_index] = elapsed_time; | |
340 } | |
341 } | |
342 | |
343 return timings; | |
344 } | |
345 | |
346 // This function mimics deferred decoding in Chromium when not all data has been | |
347 // received yet. | |
348 IterationsOfFrameTimings TimePacketedDecode(const RefPtr<SharedBuffer>& data, | |
349 bool apply_color_correction, | |
350 size_t packet_size, | |
351 size_t iterations) { | |
352 size_t total_frame_count = GetFrameCount(data); | |
353 | |
354 IterationsOfFrameTimings timings(iterations, | |
355 FrameTimings(total_frame_count, 0.0)); | |
356 | |
357 for (size_t i = 0; i < iterations; ++i) { | |
358 size_t position = 0; | |
359 size_t next_frame_to_decode = 0; | |
360 | |
361 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); | |
scroggo_chromium
2017/07/05 13:16:15
You probably want at least one packet before you a
| |
362 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( | |
363 packet_data.Get(), false, ImageDecoder::kAlphaPremultiplied, | |
364 apply_color_correction ? ColorBehavior::TransformToTargetForTesting() | |
365 : ColorBehavior::Ignore()); | |
366 while (true) { | |
367 const char* packet; | |
368 size_t length = data->GetSomeData(packet, position); | |
369 | |
370 length = std::min(length, packet_size); | |
371 packet_data->Append(packet, length); | |
372 position += length; | |
373 | |
374 bool all_data_received = position == data->size(); | |
375 decoder->SetData(packet_data.Get(), all_data_received); | |
376 | |
377 size_t frame_count = decoder->FrameCount(); | |
378 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { | |
379 double start_time = GetCurrentTime(); | |
380 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); | |
381 double elapsed_time = GetCurrentTime() - start_time; | |
382 if (frame->GetStatus() != ImageFrame::kFrameComplete) | |
383 break; | |
384 timings[i][next_frame_to_decode] = elapsed_time; | |
385 } | |
386 | |
387 if (decoder->Failed()) { | |
388 fprintf(stderr, "Image decode failed\n"); | |
389 exit(3); | |
390 } | |
391 | |
392 if (all_data_received) | |
245 break; | 393 break; |
246 } | 394 } |
247 | |
248 if (all_data_received || decoder->Failed()) | |
249 break; | |
250 } | 395 } |
251 | 396 |
252 return !decoder->Failed(); | 397 return timings; |
253 } | 398 } |
254 | 399 |
255 } // namespace | 400 } // namespace |
256 | 401 |
257 int Main(int argc, char* argv[]) { | 402 int Main(int argc, char* argv[]) { |
258 base::CommandLine::Init(argc, argv); | 403 base::CommandLine::Init(argc, argv); |
259 | 404 |
260 // If the platform supports color correction, allow it to be controlled. | 405 // If the platform supports color correction, allow it to be controlled. |
261 | 406 |
262 bool apply_color_correction = false; | 407 bool apply_color_correction = false; |
263 | |
264 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { | 408 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { |
265 apply_color_correction = (--argc, ++argv, true); | 409 --argc; |
410 ++argv; | |
411 apply_color_correction = true; | |
266 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); | 412 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); |
267 ColorBehavior::SetGlobalTargetColorProfile(profile); | 413 ColorBehavior::SetGlobalTargetColorProfile(profile); |
268 } | 414 } |
269 | 415 |
416 const char* raw_output_file = nullptr; | |
417 if (argc >= 2 && strcmp(argv[1], "--raw-output") == 0) { | |
418 if (argc < 3) { | |
419 fprintf(stderr, | |
420 "--raw-output specified without also specifying a file after it"); | |
421 exit(1); | |
422 } | |
423 | |
424 raw_output_file = argv[2]; | |
425 argc -= 2; | |
426 argv += 2; | |
427 } | |
428 | |
270 if (argc < 2) { | 429 if (argc < 2) { |
271 fprintf(stderr, | 430 fprintf(stderr, |
272 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", | 431 "Usage: %s [--color-correct] [--raw-output output_file] image_file " |
432 "[iterations] [packetSize]\n", | |
273 argv[0]); | 433 argv[0]); |
274 exit(1); | 434 exit(1); |
275 } | 435 } |
276 | 436 |
277 // Control decode bench iterations and packet size. | 437 // Control decode bench iterations and packet size. |
278 | 438 |
279 size_t iterations = 1; | 439 size_t iterations = 1; |
280 if (argc >= 3) { | 440 if (argc >= 3) { |
281 char* end = 0; | 441 char* end = 0; |
282 iterations = strtol(argv[2], &end, 10); | 442 iterations = strtol(argv[2], &end, 10); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 // segments into one, contiguous block of memory. | 474 // segments into one, contiguous block of memory. |
315 | 475 |
316 RefPtr<SharedBuffer> data = ReadFile(argv[1]); | 476 RefPtr<SharedBuffer> data = ReadFile(argv[1]); |
317 if (!data.Get() || !data->size()) { | 477 if (!data.Get() || !data->size()) { |
318 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); | 478 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); |
319 exit(2); | 479 exit(2); |
320 } | 480 } |
321 | 481 |
322 data->Data(); | 482 data->Data(); |
323 | 483 |
324 // Warm-up: throw out the first iteration for more consistent results. | 484 IterationsOfFrameTimings timings; |
325 | 485 if (packet_size) { |
326 if (!DecodeImageData(data.Get(), apply_color_correction, packet_size)) { | 486 timings = TimePacketedDecode(data, apply_color_correction, |
327 fprintf(stderr, "Image decode failed [%s]\n", argv[1]); | 487 packet_size, iterations); |
328 exit(3); | 488 } else { |
489 timings = TimeDecode(data, apply_color_correction, iterations); | |
329 } | 490 } |
330 | 491 |
331 // Image decode bench for iterations. | 492 if (raw_output_file) |
493 Write2DResultsToFile(timings, raw_output_file); | |
332 | 494 |
333 double total_time = 0.0; | 495 MeanAndMin mean_and_min = GetMeanAndMin(timings); |
496 double standard_deviation = GetStandardDeviation(timings, mean_and_min.mean_); | |
334 | 497 |
335 for (size_t i = 0; i < iterations; ++i) { | 498 printf("min: %f mean: %f standard deviation: %f\n", mean_and_min.min_, |
336 double start_time = GetCurrentTime(); | 499 mean_and_min.mean_, standard_deviation); |
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 | 500 |
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; | 501 return 0; |
352 } | 502 } |
353 | 503 |
354 } // namespace blink | 504 } // namespace blink |
355 | 505 |
356 int main(int argc, char* argv[]) { | 506 int main(int argc, char* argv[]) { |
357 return blink::Main(argc, argv); | 507 return blink::Main(argc, argv); |
358 } | 508 } |
OLD | NEW |