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