Commit b2ab0060 authored by ricea's avatar ricea Committed by Commit bot

Move WebSocket masking benchmarks to net_perftests

Previously WebSocketFrameTestMaskBenchmark.* were included in
net_unittests (with a small number of iterations to avoid slowing down
testing). Move them to net_perftests.

Also remove the command-line option to control the number of iterations,
and use the standard base::PerfTimeLogger output instead of custom logging.

BUG=407445
TEST=net_unittests, net_perftests

Review URL: https://codereview.chromium.org/502353002

Cr-Commit-Position: refs/heads/master@{#292856}
parent fdf8d7a3
......@@ -852,6 +852,7 @@
'cookies/cookie_monster_perftest.cc',
'disk_cache/blockfile/disk_cache_perftest.cc',
'proxy/proxy_resolver_perftest.cc',
'websockets/websocket_frame_perftest.cc',
],
'conditions': [
[ 'use_v8_in_net==1', {
......@@ -877,6 +878,11 @@
# TODO(jschuh): crbug.com/167187 fix size_t to int truncations.
'msvs_disabled_warnings': [4267, ],
}],
[ 'enable_websockets != 1', {
'sources!': [
'websockets/websocket_frame_perftest.cc',
],
}],
],
},
{
......
......@@ -25,7 +25,7 @@ websocket_throttle_test.cc
The following files are part of the new implementation. The new implementation
performs framing and implements protocol semantics in the browser process, and
presents a high-level interface to the renderer process similar to a
multiplexing proxy. This is not yet used in any stable Chromium version.
multiplexing proxy. This is the default implementation from M38.
websocket_basic_handshake_stream.cc
websocket_basic_handshake_stream.h
......@@ -60,6 +60,7 @@ websocket_frame_parser.cc
websocket_frame_parser.h
websocket_frame_parser_test.cc
websocket_frame_test.cc
websocket_frame_perftest.cc
websocket_handshake_stream_base.h
websocket_handshake_stream_create_helper.cc
websocket_handshake_stream_create_helper.h
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/websockets/websocket_frame.h"
#include <algorithm>
#include <vector>
#include "base/macros.h"
#include "base/test/perf_time_logger.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const int kIterations = 100000;
const int kLongPayloadSize = 1 << 16;
const char kMaskingKey[] = "\xFE\xED\xBE\xEF";
COMPILE_ASSERT(arraysize(kMaskingKey) ==
WebSocketFrameHeader::kMaskingKeyLength + 1,
incorrect_masking_key_size);
class WebSocketFrameTestMaskBenchmark : public ::testing::Test {
protected:
void Benchmark(const char* const name,
const char* const payload,
size_t size) {
std::vector<char> scratch(payload, payload + size);
WebSocketMaskingKey masking_key;
std::copy(kMaskingKey,
kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength,
masking_key.key);
base::PerfTimeLogger timer(name);
for (int x = 0; x < kIterations; ++x) {
MaskWebSocketFramePayload(
masking_key, x % size, &scratch.front(), scratch.size());
}
timer.Done();
}
};
TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) {
static const char kShortPayload[] = "Short Payload";
Benchmark(
"Frame_mask_short_payload", kShortPayload, arraysize(kShortPayload));
}
TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) {
std::vector<char> payload(kLongPayloadSize, 'a');
Benchmark("Frame_mask_long_payload", &payload.front(), payload.size());
}
} // namespace
} // namespace net
......@@ -5,29 +5,17 @@
#include "net/websockets/websocket_frame.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/aligned_memory.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
// Run
// out/Release/net_unittests --websocket-mask-iterations=100000
// --gtest_filter='WebSocketFrameTestMaskBenchmark.*'
// to benchmark the MaskWebSocketFramePayload() function.
static const char kBenchmarkIterations[] = "websocket-mask-iterations";
static const int kDefaultIterations = 10;
static const int kLongPayloadSize = 1 << 16;
namespace net {
namespace {
TEST(WebSocketFrameHeaderTest, FrameLengths) {
struct TestCase {
const char* frame_header;
......@@ -342,64 +330,6 @@ TEST(WebSocketFrameTest, MaskPayloadAlignment) {
}
}
class WebSocketFrameTestMaskBenchmark : public testing::Test {
public:
WebSocketFrameTestMaskBenchmark() : iterations_(kDefaultIterations) {}
virtual void SetUp() {
std::string iterations(
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kBenchmarkIterations));
int benchmark_iterations = 0;
if (!iterations.empty() &&
base::StringToInt(iterations, &benchmark_iterations)) {
iterations_ = benchmark_iterations;
}
}
void Benchmark(const char* const payload, size_t size) {
std::vector<char> scratch(payload, payload + size);
static const char kMaskingKey[] = "\xFE\xED\xBE\xEF";
COMPILE_ASSERT(
arraysize(kMaskingKey) == WebSocketFrameHeader::kMaskingKeyLength + 1,
incorrect_masking_key_size);
WebSocketMaskingKey masking_key;
std::copy(kMaskingKey,
kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength,
masking_key.key);
LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " << iterations_
<< " iterations";
using base::TimeTicks;
TimeTicks start = TimeTicks::HighResNow();
for (int x = 0; x < iterations_; ++x) {
MaskWebSocketFramePayload(
masking_key, x % size, &scratch.front(), scratch.size());
}
double total_time_ms =
1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() /
iterations_;
LOG(INFO) << "Payload size " << size
<< base::StringPrintf(" took %.03f microseconds per iteration",
total_time_ms);
}
private:
int iterations_;
DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark);
};
TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) {
static const char kShortPayload[] = "Short Payload";
Benchmark(kShortPayload, arraysize(kShortPayload));
}
TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) {
scoped_ptr<char[]> payload(new char[kLongPayloadSize]);
std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a');
Benchmark(payload.get(), kLongPayloadSize);
}
// "IsKnownDataOpCode" is currently implemented in an "obviously correct"
// manner, but we test is anyway in case it changes to a more complex
// implementation in future.
......@@ -459,4 +389,6 @@ TEST(WebSocketFrameHeaderTest, IsKnownControlOpCode) {
EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF));
}
} // namespace
} // namespace net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment