OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/websockets/websocket_frame.h" | 5 #include "net/websockets/websocket_frame.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | |
9 #include <vector> | 8 #include <vector> |
10 | 9 |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/command_line.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/aligned_memory.h" | 11 #include "base/memory/aligned_memory.h" |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "base/strings/stringprintf.h" | |
17 #include "base/time/time.h" | |
18 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
20 | 14 |
21 // Run | 15 namespace net { |
22 // out/Release/net_unittests --websocket-mask-iterations=100000 | |
23 // --gtest_filter='WebSocketFrameTestMaskBenchmark.*' | |
24 // to benchmark the MaskWebSocketFramePayload() function. | |
25 static const char kBenchmarkIterations[] = "websocket-mask-iterations"; | |
26 static const int kDefaultIterations = 10; | |
27 static const int kLongPayloadSize = 1 << 16; | |
28 | 16 |
29 namespace net { | 17 namespace { |
30 | 18 |
31 TEST(WebSocketFrameHeaderTest, FrameLengths) { | 19 TEST(WebSocketFrameHeaderTest, FrameLengths) { |
32 struct TestCase { | 20 struct TestCase { |
33 const char* frame_header; | 21 const char* frame_header; |
34 size_t frame_header_length; | 22 size_t frame_header_length; |
35 uint64 frame_length; | 23 uint64 frame_length; |
36 }; | 24 }; |
37 static const TestCase kTests[] = { | 25 static const TestCase kTests[] = { |
38 { "\x81\x00", 2, GG_UINT64_C(0) }, | 26 { "\x81\x00", 2, GG_UINT64_C(0) }, |
39 { "\x81\x7D", 2, GG_UINT64_C(125) }, | 27 { "\x81\x7D", 2, GG_UINT64_C(125) }, |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 ASSERT_TRUE(std::equal(aligned_scratch, | 323 ASSERT_TRUE(std::equal(aligned_scratch, |
336 aligned_scratch + aligned_len, | 324 aligned_scratch + aligned_len, |
337 kTestOutput + frame_offset)) | 325 kTestOutput + frame_offset)) |
338 << "Output failed to match for frame_offset=" << frame_offset | 326 << "Output failed to match for frame_offset=" << frame_offset |
339 << ", alignment=" << alignment << ", chunk_size=" << chunk_size; | 327 << ", alignment=" << alignment << ", chunk_size=" << chunk_size; |
340 } | 328 } |
341 } | 329 } |
342 } | 330 } |
343 } | 331 } |
344 | 332 |
345 class WebSocketFrameTestMaskBenchmark : public testing::Test { | |
346 public: | |
347 WebSocketFrameTestMaskBenchmark() : iterations_(kDefaultIterations) {} | |
348 | |
349 virtual void SetUp() { | |
350 std::string iterations( | |
351 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
352 kBenchmarkIterations)); | |
353 int benchmark_iterations = 0; | |
354 if (!iterations.empty() && | |
355 base::StringToInt(iterations, &benchmark_iterations)) { | |
356 iterations_ = benchmark_iterations; | |
357 } | |
358 } | |
359 | |
360 void Benchmark(const char* const payload, size_t size) { | |
361 std::vector<char> scratch(payload, payload + size); | |
362 static const char kMaskingKey[] = "\xFE\xED\xBE\xEF"; | |
363 COMPILE_ASSERT( | |
364 arraysize(kMaskingKey) == WebSocketFrameHeader::kMaskingKeyLength + 1, | |
365 incorrect_masking_key_size); | |
366 WebSocketMaskingKey masking_key; | |
367 std::copy(kMaskingKey, | |
368 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, | |
369 masking_key.key); | |
370 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " << iterations_ | |
371 << " iterations"; | |
372 using base::TimeTicks; | |
373 TimeTicks start = TimeTicks::HighResNow(); | |
374 for (int x = 0; x < iterations_; ++x) { | |
375 MaskWebSocketFramePayload( | |
376 masking_key, x % size, &scratch.front(), scratch.size()); | |
377 } | |
378 double total_time_ms = | |
379 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() / | |
380 iterations_; | |
381 LOG(INFO) << "Payload size " << size | |
382 << base::StringPrintf(" took %.03f microseconds per iteration", | |
383 total_time_ms); | |
384 } | |
385 | |
386 private: | |
387 int iterations_; | |
388 | |
389 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark); | |
390 }; | |
391 | |
392 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) { | |
393 static const char kShortPayload[] = "Short Payload"; | |
394 Benchmark(kShortPayload, arraysize(kShortPayload)); | |
395 } | |
396 | |
397 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) { | |
398 scoped_ptr<char[]> payload(new char[kLongPayloadSize]); | |
399 std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a'); | |
400 Benchmark(payload.get(), kLongPayloadSize); | |
401 } | |
402 | |
403 // "IsKnownDataOpCode" is currently implemented in an "obviously correct" | 333 // "IsKnownDataOpCode" is currently implemented in an "obviously correct" |
404 // manner, but we test is anyway in case it changes to a more complex | 334 // manner, but we test is anyway in case it changes to a more complex |
405 // implementation in future. | 335 // implementation in future. |
406 TEST(WebSocketFrameHeaderTest, IsKnownDataOpCode) { | 336 TEST(WebSocketFrameHeaderTest, IsKnownDataOpCode) { |
407 // Make the test less verbose. | 337 // Make the test less verbose. |
408 typedef WebSocketFrameHeader Frame; | 338 typedef WebSocketFrameHeader Frame; |
409 | 339 |
410 // Known opcode, is used for data frames | 340 // Known opcode, is used for data frames |
411 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeContinuation)); | 341 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeContinuation)); |
412 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeText)); | 342 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeText)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 | 382 |
453 // Check that opcodes with the 4 bit set return false | 383 // Check that opcodes with the 4 bit set return false |
454 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); | 384 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); |
455 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); | 385 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
456 | 386 |
457 // Check that out-of-range opcodes return false | 387 // Check that out-of-range opcodes return false |
458 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); | 388 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
459 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); | 389 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
460 } | 390 } |
461 | 391 |
| 392 } // namespace |
| 393 |
462 } // namespace net | 394 } // namespace net |
OLD | NEW |