Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / mod_pagespeed / mod_pagespeed_metrics_unittest.cc
blob38ef0cb91136b66a445d17bf9cecd0bea3664089
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 "chrome/browser/mod_pagespeed/mod_pagespeed_metrics.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "content/public/common/resource_type.h"
16 #include "net/http/http_response_headers.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 namespace mod_pagespeed {
22 // Ensure that we count PageSpeed headers correctly.
23 TEST(ModPagespeedMetricsTest, CountPageSpeedHeadersTest) {
24 base::StatisticsRecorder::Initialize();
25 GURL url("http://google.com");
26 std::string temp("HTTP/1.1 200 OK\n\n");
27 std::replace(temp.begin(), temp.end(), '\n', '\0');
28 scoped_refptr<net::HttpResponseHeaders> headers(
29 new net::HttpResponseHeaders(temp));
31 int num_responses = 0;
32 int num_mps = 0;
33 int num_ngx = 0;
34 int num_pss = 0;
35 int num_other = 0;
36 int num_bucket_1 = 0; // unrecognized format/value bucket
37 int num_bucket_30 = 0; // 1.2.24.1 bucket
38 int num_bucket_33 = 0; // 1.3.25.2 bucket
40 scoped_ptr<base::HistogramSamples> server_samples;
41 scoped_ptr<base::HistogramSamples> version_samples;
43 // No PageSpeed header. The VersionCounts histogram isn't created yet.
44 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, url, headers.get());
45 base::HistogramBase* server_histogram =
46 base::StatisticsRecorder::FindHistogram(
47 "Prerender.PagespeedHeader.ServerCounts");
48 ASSERT_TRUE(server_histogram != NULL);
49 ASSERT_TRUE(NULL == base::StatisticsRecorder::FindHistogram(
50 "Prerender.PagespeedHeader.VersionCounts"));
52 server_samples = server_histogram->SnapshotSamples();
53 EXPECT_EQ(++num_responses, server_samples->GetCount(0));
54 EXPECT_EQ(num_mps, server_samples->GetCount(1));
55 EXPECT_EQ(num_ngx, server_samples->GetCount(2));
56 EXPECT_EQ(num_pss, server_samples->GetCount(3));
57 EXPECT_EQ(num_other, server_samples->GetCount(4));
59 // X-Mod-Pagespeed header in expected format. VersionCounts now exists.
60 headers->AddHeader("X-Mod-Pagespeed: 1.2.24.1-2300");
61 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, url, headers.get());
62 base::HistogramBase* version_histogram =
63 base::StatisticsRecorder::FindHistogram(
64 "Prerender.PagespeedHeader.VersionCounts");
65 ASSERT_TRUE(version_histogram != NULL);
66 server_samples = server_histogram->SnapshotSamples();
67 version_samples = version_histogram->SnapshotSamples();
68 EXPECT_EQ(++num_responses, server_samples->GetCount(0));
69 EXPECT_EQ(++num_mps, server_samples->GetCount(1));
70 EXPECT_EQ(num_ngx, server_samples->GetCount(2));
71 EXPECT_EQ(num_pss, server_samples->GetCount(3));
72 EXPECT_EQ(num_other, server_samples->GetCount(4));
73 EXPECT_EQ(num_bucket_1, version_samples->GetCount(1));
74 EXPECT_EQ(++num_bucket_30, version_samples->GetCount(30)); // +1 for #30
75 EXPECT_EQ(num_bucket_33, version_samples->GetCount(33));
76 headers->RemoveHeader("X-Mod-Pagespeed");
78 // X-Mod-Pagespeed header in unexpected format.
79 headers->AddHeader("X-Mod-Pagespeed: Powered By PageSpeed!");
80 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, url, headers.get());
81 server_samples = server_histogram->SnapshotSamples();
82 version_samples = version_histogram->SnapshotSamples();
83 EXPECT_EQ(++num_responses, server_samples->GetCount(0));
84 EXPECT_EQ(++num_mps, server_samples->GetCount(1));
85 EXPECT_EQ(num_ngx, server_samples->GetCount(2));
86 EXPECT_EQ(num_pss, server_samples->GetCount(3));
87 EXPECT_EQ(num_other, server_samples->GetCount(4));
88 EXPECT_EQ(++num_bucket_1, version_samples->GetCount(1)); // +1 for 'huh?'
89 EXPECT_EQ(num_bucket_30, version_samples->GetCount(30));
90 EXPECT_EQ(num_bucket_33, version_samples->GetCount(33));
91 headers->RemoveHeader("X-Mod-Pagespeed");
93 // X-Page-Speed header in mod_pagespeed format (so ngx_pagespeed).
94 headers->AddHeader("X-Page-Speed: 1.3.25.2-2530");
95 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, url, headers.get());
96 server_samples = server_histogram->SnapshotSamples();
97 version_samples = version_histogram->SnapshotSamples();
98 EXPECT_EQ(++num_responses, server_samples->GetCount(0));
99 EXPECT_EQ(num_mps, server_samples->GetCount(1));
100 EXPECT_EQ(++num_ngx, server_samples->GetCount(2));
101 EXPECT_EQ(num_pss, server_samples->GetCount(3));
102 EXPECT_EQ(num_other, server_samples->GetCount(4));
103 EXPECT_EQ(num_bucket_1, version_samples->GetCount(1));
104 EXPECT_EQ(num_bucket_30, version_samples->GetCount(30));
105 EXPECT_EQ(++num_bucket_33, version_samples->GetCount(33)); // +1 for #33
106 headers->RemoveHeader("X-Page-Speed");
108 // X-Page-Speed header in PageSpeed Service format.
109 headers->AddHeader("X-Page-Speed: 97_4_bo");
110 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, url, headers.get());
111 server_samples = server_histogram->SnapshotSamples();
112 version_samples = version_histogram->SnapshotSamples();
113 EXPECT_EQ(++num_responses, server_samples->GetCount(0));
114 EXPECT_EQ(num_mps, server_samples->GetCount(1)); // no change
115 EXPECT_EQ(num_ngx, server_samples->GetCount(2));
116 EXPECT_EQ(++num_pss, server_samples->GetCount(3)); // +1 for PSS
117 EXPECT_EQ(num_other, server_samples->GetCount(4));
118 EXPECT_EQ(num_bucket_1, version_samples->GetCount(1));
119 EXPECT_EQ(num_bucket_30, version_samples->GetCount(30));
120 EXPECT_EQ(num_bucket_33, version_samples->GetCount(33));
121 headers->RemoveHeader("X-Page-Speed");
123 // X-Page-Speed header in an unrecognized format (IISpeed in this case).
124 headers->AddHeader("X-Page-Speed: 1.0PS1.2-20130615");
125 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, url, headers.get());
126 server_samples = server_histogram->SnapshotSamples();
127 version_samples = version_histogram->SnapshotSamples();
128 EXPECT_EQ(++num_responses, server_samples->GetCount(0));
129 EXPECT_EQ(num_mps, server_samples->GetCount(1)); // no change
130 EXPECT_EQ(num_pss, server_samples->GetCount(3));
131 EXPECT_EQ(++num_other, server_samples->GetCount(4)); // +1 for 'other'
132 EXPECT_EQ(num_bucket_1, version_samples->GetCount(1));
133 EXPECT_EQ(num_bucket_30, version_samples->GetCount(30));
134 EXPECT_EQ(num_bucket_33, version_samples->GetCount(33));
136 // Not a main frame => not counted at all.
137 RecordMetrics(content::RESOURCE_TYPE_SUB_FRAME, url, headers.get());
138 server_samples = server_histogram->SnapshotSamples();
139 version_samples = version_histogram->SnapshotSamples();
140 EXPECT_EQ(num_responses, server_samples->GetCount(0));
141 EXPECT_EQ(num_mps, server_samples->GetCount(1));
142 EXPECT_EQ(num_ngx, server_samples->GetCount(2));
143 EXPECT_EQ(num_pss, server_samples->GetCount(3));
144 EXPECT_EQ(num_other, server_samples->GetCount(4));
145 EXPECT_EQ(num_bucket_1, version_samples->GetCount(1));
146 EXPECT_EQ(num_bucket_30, version_samples->GetCount(30));
147 EXPECT_EQ(num_bucket_33, version_samples->GetCount(33));
149 // Not a http/https URL => not counted at all.
150 GURL data_url("data:image/png;base64,yadda yadda==");
151 RecordMetrics(content::RESOURCE_TYPE_MAIN_FRAME, data_url, headers.get());
152 server_samples = server_histogram->SnapshotSamples();
153 version_samples = version_histogram->SnapshotSamples();
154 EXPECT_EQ(num_responses, server_samples->GetCount(0));
155 EXPECT_EQ(num_mps, server_samples->GetCount(1));
156 EXPECT_EQ(num_ngx, server_samples->GetCount(2));
157 EXPECT_EQ(num_pss, server_samples->GetCount(3));
158 EXPECT_EQ(num_other, server_samples->GetCount(4));
159 EXPECT_EQ(num_bucket_1, version_samples->GetCount(1));
160 EXPECT_EQ(num_bucket_30, version_samples->GetCount(30));
161 EXPECT_EQ(num_bucket_33, version_samples->GetCount(33));
163 headers->RemoveHeader("X-Page-Speed");
166 } // namespace mod_pagespeed