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