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