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