mime_sniffer_perftest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  3. // found in the LICENSE file.
  4. #include "net/base/mime_sniffer.h"
  5. #include <vector>
  6. #include "base/bits.h"
  7. #include "base/check_op.h"
  8. #include "base/timer/elapsed_timer.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "testing/perf/perf_result_reporter.h"
  11. namespace net {
  12. namespace {
  13. // This text is supposed to be representative of a plain text file the browser
  14. // might encounter, including a variation in line lengths and blank
  15. // lines. CRLF is used as the line-terminator to make it slightly more
  16. // difficult. It is roughly 1KB.
  17. const char kRepresentativePlainText[] =
  18. "The Tragedie of Hamlet\r\n"
  19. "\r\n"
  20. "Actus Primus. Scoena Prima.\r\n"
  21. "\r\n"
  22. "Enter Barnardo and Francisco two Centinels.\r\n"
  23. "\r\n"
  24. " Barnardo. Who's there?\r\n"
  25. " Fran. Nay answer me: Stand & vnfold\r\n"
  26. "your selfe\r\n"
  27. "\r\n"
  28. " Bar. Long liue the King\r\n"
  29. "\r\n"
  30. " Fran. Barnardo?\r\n"
  31. " Bar. He\r\n"
  32. "\r\n"
  33. " Fran. You come most carefully vpon your houre\r\n"
  34. "\r\n"
  35. " Bar. 'Tis now strook twelue, get thee to bed Francisco\r\n"
  36. "\r\n"
  37. " Fran. For this releefe much thankes: 'Tis bitter cold,\r\n"
  38. "And I am sicke at heart\r\n"
  39. "\r\n"
  40. " Barn. Haue you had quiet Guard?\r\n"
  41. " Fran. Not a Mouse stirring\r\n"
  42. "\r\n"
  43. " Barn. Well, goodnight. If you do meet Horatio and\r\n"
  44. "Marcellus, the Riuals of my Watch, bid them make hast.\r\n"
  45. "Enter Horatio and Marcellus.\r\n"
  46. "\r\n"
  47. " Fran. I thinke I heare them. Stand: who's there?\r\n"
  48. " Hor. Friends to this ground\r\n"
  49. "\r\n"
  50. " Mar. And Leige-men to the Dane\r\n"
  51. "\r\n"
  52. " Fran. Giue you good night\r\n"
  53. "\r\n"
  54. " Mar. O farwel honest Soldier, who hath relieu'd you?\r\n"
  55. " Fra. Barnardo ha's my place: giue you goodnight.\r\n"
  56. "\r\n"
  57. "Exit Fran.\r\n"
  58. "\r\n"
  59. " Mar. Holla Barnardo\r\n"
  60. "\r\n"
  61. " Bar. Say, what is Horatio there?\r\n"
  62. " Hor. A peece of him\r\n"
  63. "\r\n"
  64. " Bar. Welcome Horatio, welcome good Marcellus\r\n"
  65. "\r\n";
  66. void RunLooksLikeBinary(const std::string& plaintext, size_t iterations) {
  67. bool looks_like_binary = false;
  68. for (size_t i = 0; i < iterations; ++i) {
  69. if (LooksLikeBinary(plaintext))
  70. looks_like_binary = true;
  71. }
  72. CHECK(!looks_like_binary);
  73. }
  74. TEST(MimeSnifferTest, PlainTextPerfTest) {
  75. // Android systems have a relatively small CPU cache (512KB to 2MB).
  76. // It is better if the test data fits in cache so that we are not just
  77. // testing bus bandwidth.
  78. const size_t kTargetSize = 1 << 18; // 256KB
  79. const size_t kWarmupIterations = 16;
  80. const size_t kMeasuredIterations = 1 << 15;
  81. std::string plaintext = kRepresentativePlainText;
  82. size_t expected_size = plaintext.size() << base::bits::Log2Ceiling(
  83. kTargetSize / plaintext.size());
  84. plaintext.reserve(expected_size);
  85. while (plaintext.size() < kTargetSize)
  86. plaintext += plaintext;
  87. DCHECK_EQ(expected_size, plaintext.size());
  88. RunLooksLikeBinary(plaintext, kWarmupIterations);
  89. base::ElapsedTimer elapsed_timer;
  90. RunLooksLikeBinary(plaintext, kMeasuredIterations);
  91. perf_test::PerfResultReporter reporter("MimeSniffer.", "PlainText");
  92. reporter.RegisterImportantMetric("throughput",
  93. "bytesPerSecond_biggerIsBetter");
  94. reporter.AddResult("throughput", static_cast<int64_t>(plaintext.size()) *
  95. kMeasuredIterations /
  96. elapsed_timer.Elapsed().InSecondsF());
  97. }
  98. } // namespace
  99. } // namespace net