Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: net/spdy/hpack_huffman_aggregator_test.cc

Issue 243153003: HPACK optimal Huffman code instrumentation and UMA collection. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #include "net/spdy/hpack_huffman_aggregator.h"
2
3 #include "base/metrics/histogram.h"
4 #include "base/metrics/statistics_recorder.h"
5 #include "net/base/load_flags.h"
6 #include "net/http/http_request_headers.h"
7 #include "net/http/http_request_info.h"
8 #include "net/http/http_response_headers.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace net {
13
14 using ::testing::Each;
15 using ::testing::ElementsAre;
16 using ::testing::Eq;
17 using ::testing::Pair;
18
19 namespace {
20 const char kHistogramName[] = "Net.SpdyHpackEncodedCharacterFrequency";
21 } // namespace
22
23 namespace test {
24
25 class HpackHuffmanAggregatorPeer {
26 public:
27 explicit HpackHuffmanAggregatorPeer(HpackHuffmanAggregator* agg)
28 : agg_(agg) {}
29
30 std::vector<size_t>* counts() {
31 return &agg_->counts_;
32 }
33 HpackHuffmanAggregator::OriginEncoders* encoders() {
34 return &agg_->encoders_;
35 }
36 size_t total_counts() {
37 return agg_->total_counts_;
38 }
39 void set_total_counts(size_t total_counts) {
40 agg_->total_counts_ = total_counts;
41 }
42 void set_max_encoders(size_t max_encoders) {
43 agg_->max_encoders_ = max_encoders;
44 }
45 static bool IsCrossOrigin(const HttpRequestInfo& request) {
46 return HpackHuffmanAggregator::IsCrossOrigin(request);
47 }
48 static void CreateSpdyHeadersFromHttpResponse(
49 const HttpResponseHeaders& headers,
50 SpdyHeaderBlock* headers_out) {
51 HpackHuffmanAggregator::CreateSpdyHeadersFromHttpResponse(
52 headers, headers_out);
53 }
54 HpackEncoder* ObtainEncoder(const SpdySessionKey& key) {
55 return agg_->ObtainEncoder(key);
56 }
57 void PublishCounts() {
58 agg_->PublishCounts();
59 }
60
61 private:
62 HpackHuffmanAggregator* agg_;
63 };
64
65 } // namespace test
66
67 class HpackHuffmanAggregatorTest : public ::testing::Test {
68 protected:
69 HpackHuffmanAggregatorTest()
70 : peer_(&agg_) {}
71
72 HpackHuffmanAggregator agg_;
73 test::HpackHuffmanAggregatorPeer peer_;
74 };
75
76 TEST_F(HpackHuffmanAggregatorTest, CrossOriginDetermination) {
77 HttpRequestInfo request;
78 request.url = GURL("https://www.foo.com/a/page");
79
80 // Main load without referer.
81 request.load_flags = LOAD_MAIN_FRAME;
82 EXPECT_FALSE(peer_.IsCrossOrigin(request));
83
84 // Non-main load without referer. Treated as cross-origin.
85 request.load_flags = 0;
86 EXPECT_TRUE(peer_.IsCrossOrigin(request));
87
88 // Main load with different referer origin.
89 request.load_flags = LOAD_MAIN_FRAME;
90 request.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
91 "https://www.bar.com/other/page");
92 EXPECT_FALSE(peer_.IsCrossOrigin(request));
93
94 // Non-main load with different referer orign.
95 request.load_flags = 0;
96 EXPECT_TRUE(peer_.IsCrossOrigin(request));
97
98 // Non-main load with same referer orign.
99 request.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
100 "https://www.foo.com/other/page");
101 EXPECT_FALSE(peer_.IsCrossOrigin(request));
102
103 // Non-main load with same referer host but different schemes.
104 request.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
105 "http://www.foo.com/other/page");
106 EXPECT_TRUE(peer_.IsCrossOrigin(request));
107 }
108
109 TEST_F(HpackHuffmanAggregatorTest, EncoderLRUQueue) {
110 peer_.set_max_encoders(2);
111
112 SpdySessionKey key1(HostPortPair("one.com", 443), ProxyServer::Direct(),
113 PRIVACY_MODE_ENABLED);
114 SpdySessionKey key2(HostPortPair("two.com", 443), ProxyServer::Direct(),
115 PRIVACY_MODE_ENABLED);
116 SpdySessionKey key3(HostPortPair("three.com", 443), ProxyServer::Direct(),
117 PRIVACY_MODE_ENABLED);
118
119 // Creates one.com.
120 HpackEncoder* one = peer_.ObtainEncoder(key1);
121 EXPECT_EQ(1u, peer_.encoders()->size());
122
123 // Creates two.com. No evictions.
124 HpackEncoder* two = peer_.ObtainEncoder(key2);
125 EXPECT_EQ(2u, peer_.encoders()->size());
126 EXPECT_NE(one, two);
127
128 // Touch one.com.
129 EXPECT_EQ(one, peer_.ObtainEncoder(key1));
130
131 // Creates three.com. Evicts two.com, as it's least-recently used.
132 HpackEncoder* three = peer_.ObtainEncoder(key3);
133 EXPECT_EQ(one, peer_.ObtainEncoder(key1));
134 EXPECT_NE(one, three);
135 EXPECT_EQ(2u, peer_.encoders()->size());
136 }
137
138 TEST_F(HpackHuffmanAggregatorTest, PublishCounts) {
139 (*peer_.counts())[0] = 1;
140 (*peer_.counts())[255] = 10;
141 (*peer_.counts())[128] = 101;
142 peer_.set_total_counts(112);
143
144 peer_.PublishCounts();
145
146 // Internal counts were reset after being published.
147 EXPECT_THAT(*peer_.counts(), Each(Eq(0u)));
148 EXPECT_EQ(0u, peer_.total_counts());
149
150 // Verify histogram counts match the expectation.
151 scoped_ptr<base::HistogramSamples> samples =
152 base::StatisticsRecorder::FindHistogram(kHistogramName)
153 ->SnapshotSamples();
154
155 EXPECT_EQ(0, samples->GetCount(0));
156 EXPECT_EQ(1, samples->GetCount(1));
157 EXPECT_EQ(101, samples->GetCount(129));
158 EXPECT_EQ(10, samples->GetCount(256));
159 EXPECT_EQ(112, samples->TotalCount());
160
161 // Publish a second round of counts;
162 (*peer_.counts())[1] = 32;
163 (*peer_.counts())[128] = 5;
164 peer_.set_total_counts(37);
165
166 peer_.PublishCounts();
167
168 // Verify they've been aggregated into the previous counts.
169 samples = base::StatisticsRecorder::FindHistogram(kHistogramName)
170 ->SnapshotSamples();
171
172 EXPECT_EQ(0, samples->GetCount(0));
173 EXPECT_EQ(1, samples->GetCount(1));
174 EXPECT_EQ(32, samples->GetCount(2));
175 EXPECT_EQ(106, samples->GetCount(129));
176 EXPECT_EQ(10, samples->GetCount(256));
177 EXPECT_EQ(149, samples->TotalCount());
178 }
179
180 TEST_F(HpackHuffmanAggregatorTest, CreateSpdyResponseHeaders) {
181 char kRawHeaders[] =
182 "HTTP/1.1 202 Accepted \0"
183 "Content-TYPE : text/html; charset=utf-8 \0"
184 "Set-Cookie: foo=bar \0"
185 "Set-Cookie: baz=bing \0"
186 "Cache-Control: pragma=no-cache \0"
187 "Cache-CONTROL: expires=12345 \0\0";
188
189 scoped_refptr<HttpResponseHeaders> parsed_headers(new HttpResponseHeaders(
190 std::string(kRawHeaders, arraysize(kRawHeaders) - 1)));
191
192 SpdyHeaderBlock headers;
193 peer_.CreateSpdyHeadersFromHttpResponse(*parsed_headers, &headers);
194 EXPECT_THAT(headers, ElementsAre(
195 Pair(":status", "202"),
196 Pair("cache-control", std::string("pragma=no-cache\0expires=12345", 29)),
197 Pair("content-type", "text/html; charset=utf-8"),
198 Pair("set-cookie", std::string("foo=bar\0baz=bing", 16))));
199 }
200
201 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698