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

Side by Side Diff: net/websockets/websocket_frame_unittest.cc

Issue 11572010: Optimise MaskWebSocketFramePayload(). (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Compile fix for Windows. Created 8 years 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
« no previous file with comments | « net/websockets/websocket_frame.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <vector> 8 #include <vector>
8 9
9 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/memory/aligned_memory.h"
14 #include "base/string_number_conversions.h"
15 #include "base/stringprintf.h"
16 #include "base/time.h"
10 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
11 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
12 19
20 // Run
21 // out/Release/net_unittests --websocket-mask-iterations=100000
22 // --gtest_filter='WebSocketFrameTestMaskBenchmark.*'
23 // to benchmark the MaskWebSocketFramePayload() function.
24 static const char kBenchmarkIterations[] = "websocket-mask-iterations";
25 static const int kDefaultIterations = 10;
26 static const int kLongPayloadSize = 1 << 16;
27
13 namespace net { 28 namespace net {
14 29
15 TEST(WebSocketFrameHeaderTest, FrameLengths) { 30 TEST(WebSocketFrameHeaderTest, FrameLengths) {
16 struct TestCase { 31 struct TestCase {
17 const char* frame_header; 32 const char* frame_header;
18 size_t frame_header_length; 33 size_t frame_header_length;
19 uint64 frame_length; 34 uint64 frame_length;
20 }; 35 };
21 static const TestCase kTests[] = { 36 static const TestCase kTests[] = {
22 { "\x81\x00", 2, GG_UINT64_C(0) }, 37 { "\x81\x00", 2, GG_UINT64_C(0) },
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 std::vector<char> expected_output(kTests[i].output, 275 std::vector<char> expected_output(kTests[i].output,
261 kTests[i].output + kTests[i].data_length); 276 kTests[i].output + kTests[i].data_length);
262 MaskWebSocketFramePayload(masking_key, 277 MaskWebSocketFramePayload(masking_key,
263 kTests[i].frame_offset, 278 kTests[i].frame_offset,
264 frame_data.empty() ? NULL : &frame_data.front(), 279 frame_data.empty() ? NULL : &frame_data.front(),
265 frame_data.size()); 280 frame_data.size());
266 EXPECT_EQ(expected_output, frame_data); 281 EXPECT_EQ(expected_output, frame_data);
267 } 282 }
268 } 283 }
269 284
285 // Check that all combinations of alignment, frame offset and chunk size work
286 // correctly for MaskWebSocketFramePayload(). This is mainly used to ensure that
287 // vectorisation optimisations don't break anything. We could take a "white box"
288 // approach and only test the edge cases, but since the exhaustive "black box"
289 // approach runs in acceptable time, we don't have to take the risk of being
290 // clever.
291 //
292 // This brute-force approach runs in O(N^3) time where N is the size of the
293 // maximum vector size we want to test again. This might need reconsidering if
294 // MaskWebSocketFramePayload() is ever optimised for a dedicated vector
295 // architecture.
296 TEST(WebSocketFrameTest, MaskPayloadAlignment) {
297 // This reflects what might be implemented in the future, rather than
298 // the current implementation. FMA3 and FMA4 support 256-bit vector ops.
299 static const size_t kMaxVectorSizeInBits = 256;
300 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8;
301 static const size_t kMaxVectorAlignment = kMaxVectorSize;
302 static const size_t kMaskingKeyLength =
303 WebSocketFrameHeader::kMaskingKeyLength;
304 static const size_t kScratchBufferSize =
305 kMaxVectorAlignment + kMaxVectorSize * 2;
306 static const char kTestMask[] = "\xd2\xba\x5a\xbe";
307 // We use 786 bits of random input to reduce the risk of correlated errors.
308 static const char kTestInput[] =
309 { "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b"
310 "\x69\x8c\xda\x4b\xfc\xac\x2c\xd3\x49\x96\x6e\x8a\x7b\x5a\x32\x76"
311 "\xd0\x11\x43\xa0\x89\xfc\x76\x2b\x10\x2f\x4c\x7b\x4f\xa6\xdd\xe4"
312 "\xfc\x8e\xd8\x72\xcf\x7e\x37\xcd\x31\xcd\xc1\xc0\x89\x0c\xa7\x4c"
313 "\xda\xa8\x4b\x75\xa1\xcb\xa9\x77\x19\x4d\x6e\xdf\xc8\x08\x1c\xb6"
314 "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8"
315 };
316 static const size_t kTestInputSize = arraysize(kTestInput) - 1;
317 static const char kTestOutput[] =
318 { "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5"
319 "\xbb\x36\x80\xf5\x2e\x16\x76\x6d\x9b\x2c\x34\x34\xa9\xe0\x68\xc8"
320 "\x02\xab\x19\x1e\x5b\x46\x2c\x95\xc2\x95\x16\xc5\x9d\x1c\x87\x5a"
321 "\x2e\x34\x82\xcc\x1d\xc4\x6d\x73\xe3\x77\x9b\x7e\x5b\xb6\xfd\xf2"
322 "\x08\x12\x11\xcb\x73\x71\xf3\xc9\xcb\xf7\x34\x61\x1a\xb2\x46\x08"
323 "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16"
324 };
325 COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput),
326 output_and_input_arrays_have_the_same_length);
327 scoped_ptr_malloc<char, base::ScopedPtrAlignedFree> scratch(
328 static_cast<char*>(base::AlignedAlloc(kScratchBufferSize,
329 kMaxVectorAlignment)));
330 WebSocketMaskingKey masking_key;
331 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key);
332 for (size_t frame_offset = 0;
333 frame_offset < kMaskingKeyLength;
334 ++frame_offset) {
335 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) {
336 char* const aligned_scratch = scratch.get() + alignment;
337 const size_t aligned_len =
338 std::min(kScratchBufferSize - alignment,
339 kTestInputSize - frame_offset);
340 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) {
341 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len);
342 for (size_t chunk_start = 0;
343 chunk_start < aligned_len;
344 chunk_start += chunk_size) {
345 const size_t this_chunk_size = std::min(chunk_size,
346 aligned_len - chunk_start);
347 MaskWebSocketFramePayload(masking_key,
348 frame_offset + chunk_start,
349 aligned_scratch + chunk_start,
350 this_chunk_size);
351 }
352 // Stop the test if it fails, since we don't want to spew thousands of
353 // failures.
354 ASSERT_TRUE(std::equal(aligned_scratch, aligned_scratch + aligned_len,
355 kTestOutput + frame_offset))
356 << "Output failed to match for frame_offset="
357 << frame_offset
358 << ", alignment="
359 << alignment
360 << ", chunk_size="
361 << chunk_size;
362 }
363 }
364 }
365 }
366
367 class WebSocketFrameTestMaskBenchmark : public testing::Test {
368 public:
369 WebSocketFrameTestMaskBenchmark()
370 : iterations_(kDefaultIterations) {}
371
372 void SetUp() {
373 std::string iterations(
374 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
375 kBenchmarkIterations));
376 int benchmark_iterations = 0;
377 if (!iterations.empty() && base::StringToInt(iterations,
378 &benchmark_iterations)) {
379 iterations_ = benchmark_iterations;
380 }
381 }
382
383 void Benchmark(const char* const payload, size_t size) {
384 std::vector<char> scratch(payload, payload + size);
385 static const char kMaskingKey[] = "\xFE\xED\xBE\xEF";
386 COMPILE_ASSERT(arraysize(kMaskingKey) ==
387 WebSocketFrameHeader::kMaskingKeyLength + 1,
388 incorrect_masking_key_size);
389 WebSocketMaskingKey masking_key;
390 std::copy(kMaskingKey, kMaskingKey +
391 WebSocketFrameHeader::kMaskingKeyLength,
392 masking_key.key);
393 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for "
394 << iterations_ << " iterations";
395 using base::TimeTicks;
396 TimeTicks start = TimeTicks::HighResNow();
397 for (int x = 0; x < iterations_; ++x) {
398 MaskWebSocketFramePayload(masking_key, x % size, &scratch.front(),
399 scratch.size());
400 }
401 double total_time_ms =
402 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() /
403 iterations_;
404 LOG(INFO) << "Payload size " << size
405 << StringPrintf(" took %.03f microseconds per iteration",
406 total_time_ms);
407 }
408
409 private:
410 int iterations_;
411
412 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark);
413 };
414
415 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) {
416 static const char kShortPayload[] = "Short Payload";
417 Benchmark(kShortPayload, arraysize(kShortPayload));
418 }
419
420 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) {
421 scoped_array<char> payload(new char[kLongPayloadSize]);
422 std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a');
423 Benchmark(payload.get(), kLongPayloadSize);
424 }
425
270 } // namespace net 426 } // namespace net
OLDNEW
« no previous file with comments | « net/websockets/websocket_frame.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698