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"
10 #include "base/logging.h"
11 #include "base/timer/elapsed_timer.h"
12 #include "testing/gtest/include/gtest/gtest.h"
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"
24 "Actus Primus. Scoena Prima.\r\n"
26 "Enter Barnardo and Francisco two Centinels.\r\n"
28 " Barnardo. Who's there?\r\n"
29 " Fran. Nay answer me: Stand & vnfold\r\n"
32 " Bar. Long liue the King\r\n"
34 " Fran. Barnardo?\r\n"
37 " Fran. You come most carefully vpon your houre\r\n"
39 " Bar. 'Tis now strook twelue, get thee to bed Francisco\r\n"
41 " Fran. For this releefe much thankes: 'Tis bitter cold,\r\n"
42 "And I am sicke at heart\r\n"
44 " Barn. Haue you had quiet Guard?\r\n"
45 " Fran. Not a Mouse stirring\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"
51 " Fran. I thinke I heare them. Stand: who's there?\r\n"
52 " Hor. Friends to this ground\r\n"
54 " Mar. And Leige-men to the Dane\r\n"
56 " Fran. Giue you good night\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"
63 " Mar. Holla Barnardo\r\n"
65 " Bar. Say, what is Horatio there?\r\n"
66 " Hor. A peece of him\r\n"
68 " Bar. Welcome Horatio, welcome good Marcellus\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
=
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
))