1 // Copyright 2014 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 <stddef.h>
6
7 #include <iterator>
8 #include <string>
9 #include <string_view>
10 #include <vector>
11
12 #include "base/ranges/algorithm.h"
13 #include "base/time/time.h"
14 #include "base/timer/elapsed_timer.h"
15 #include "net/websockets/websocket_frame.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/perf/perf_result_reporter.h"
18
19 namespace net {
20
21 namespace {
22
23 constexpr int kIterations = 100000;
24 constexpr int kLongPayloadSize = 1 << 16;
25 constexpr std::string_view kMaskingKey = "\xFE\xED\xBE\xEF";
26
27 static constexpr char kMetricPrefixWebSocketFrame[] = "WebSocketFrameMask.";
28 static constexpr char kMetricMaskTimeMs[] = "mask_time";
29
SetUpWebSocketFrameMaskReporter(const std::string & story)30 perf_test::PerfResultReporter SetUpWebSocketFrameMaskReporter(
31 const std::string& story) {
32 perf_test::PerfResultReporter reporter(kMetricPrefixWebSocketFrame, story);
33 reporter.RegisterImportantMetric(kMetricMaskTimeMs, "ms");
34 return reporter;
35 }
36
37 static_assert(kMaskingKey.size() == WebSocketFrameHeader::kMaskingKeyLength,
38 "incorrect masking key size");
39
40 class WebSocketFrameTestMaskBenchmark : public ::testing::Test {
41 protected:
Benchmark(const char * const story,const char * const payload,size_t size)42 void Benchmark(const char* const story,
43 const char* const payload,
44 size_t size) {
45 std::vector<char> scratch(payload, payload + size);
46 WebSocketMaskingKey masking_key;
47 base::ranges::copy(kMaskingKey, masking_key.key);
48 auto reporter = SetUpWebSocketFrameMaskReporter(story);
49 base::ElapsedTimer timer;
50 for (int x = 0; x < kIterations; ++x) {
51 MaskWebSocketFramePayload(masking_key, x % size, scratch.data(),
52 scratch.size());
53 }
54 reporter.AddResult(kMetricMaskTimeMs, timer.Elapsed().InMillisecondsF());
55 }
56 };
57
TEST_F(WebSocketFrameTestMaskBenchmark,BenchmarkMaskShortPayload)58 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) {
59 static constexpr char kShortPayload[] = "Short Payload";
60 Benchmark("short_payload", kShortPayload, std::size(kShortPayload));
61 }
62
TEST_F(WebSocketFrameTestMaskBenchmark,BenchmarkMaskLongPayload)63 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) {
64 std::vector<char> payload(kLongPayloadSize, 'a');
65 Benchmark("long_payload", payload.data(), payload.size());
66 }
67
68 // A 31-byte payload is guaranteed to do 7 byte mask operations and 3 vector
69 // mask operations with an 8-byte vector. With a 16-byte vector it will fall
70 // back to the byte-only code path and do 31 byte mask operations.
TEST_F(WebSocketFrameTestMaskBenchmark,Benchmark31BytePayload)71 TEST_F(WebSocketFrameTestMaskBenchmark, Benchmark31BytePayload) {
72 std::vector<char> payload(31, 'a');
73 Benchmark("31_payload", payload.data(), payload.size());
74 }
75
76 } // namespace
77
78 } // namespace net
79