OLD | NEW |
---|---|
(Empty) | |
1 #include "net/spdy/hpack_string_util.h" | |
2 | |
3 #include <cstddef> | |
4 #include <cstring> | |
5 | |
6 #include "base/basictypes.h" | |
7 #include "base/logging.h" | |
8 #include "base/strings/string_piece.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace net { | |
12 | |
13 namespace { | |
14 | |
15 using std::string; | |
16 | |
17 // Make sure StringPiecesEqualConstantTime() behaves like the regular | |
18 // string equality operator. | |
19 TEST(HpackStringUtilTest, StringPiecesEqualConstantTime) { | |
20 EXPECT_TRUE(StringPiecesEqualConstantTime("foo", "foo")); | |
21 EXPECT_FALSE(StringPiecesEqualConstantTime("foo", "foox")); | |
22 EXPECT_FALSE(StringPiecesEqualConstantTime("foo", "bar")); | |
23 } | |
24 | |
25 // TODO(jgraettinger): Support this benchmark. | |
akalin
2014/01/14 20:54:10
Out of curiosity, what's the issue here? Does Chro
Johnny
2014/01/14 21:11:21
Correct, not that I can tell.
On 2014/01/14 20:54
| |
26 /* | |
27 enum BM_StringPieceEqualityType { | |
28 STRCMP_EQUAL, | |
29 STRCMP_FIRST_CHAR_DIFFERS, | |
30 STRING_PIECES_EQUAL_CONSTANT_TIME_EQUAL, | |
31 STRING_PIECES_EQUAL_CONSTANT_TIME_FIRST_CHAR_DIFFERS, | |
32 }; | |
33 | |
34 void BM_StringPieceEquality(int iters, int size, int type_int) { | |
35 BM_StringPieceEqualityType type = | |
36 static_cast<BM_StringPieceEqualityType>(type_int); | |
37 string str_a(size, 'x'); | |
38 string str_b(size, 'x'); | |
39 int result = 0; | |
40 switch (type) { | |
41 case STRCMP_EQUAL: | |
42 for (int i = 0; i < iters; ++i) { | |
43 result |= std::strcmp(str_a.c_str(), str_b.c_str()); | |
44 } | |
45 CHECK_EQ(result, 0); | |
46 return; | |
47 | |
48 case STRCMP_FIRST_CHAR_DIFFERS: | |
49 str_b[0] = 'y'; | |
50 for (int i = 0; i < iters; ++i) { | |
51 result |= std::strcmp(str_a.c_str(), str_b.c_str()); | |
52 } | |
53 CHECK_LT(result, 0); | |
54 return; | |
55 | |
56 case STRING_PIECES_EQUAL_CONSTANT_TIME_EQUAL: | |
57 for (int i = 0; i < iters; ++i) { | |
58 result |= StringPiecesEqualConstantTime(str_a, str_b); | |
59 } | |
60 CHECK_EQ(result, 1); | |
61 return; | |
62 | |
63 case STRING_PIECES_EQUAL_CONSTANT_TIME_FIRST_CHAR_DIFFERS: | |
64 str_b[0] = 'y'; | |
65 for (int i = 0; i < iters; ++i) { | |
66 result |= StringPiecesEqualConstantTime(str_a, str_b); | |
67 } | |
68 CHECK_EQ(result, 0); | |
69 return; | |
70 } | |
71 | |
72 DCHECK(false); | |
73 } | |
74 | |
75 // Results should resemble the table below, where 0 and 1 are clearly | |
76 // different (STRCMP), but 2 and 3 are roughly the same | |
77 // (STRING_PIECES_EQUAL_CONSTANT_TIME). | |
78 // | |
79 // DEBUG: Benchmark Time(ns) CPU(ns) Iterations | |
80 // ------------------------------------------------------------------- | |
81 // DEBUG: BM_StringPieceEquality/1M/0 77796 77141 7778 | |
82 // DEBUG: BM_StringPieceEquality/1M/1 10 10 70000000 | |
83 // DEBUG: BM_StringPieceEquality/1M/2 7729735 7700000 100 | |
84 // DEBUG: BM_StringPieceEquality/1M/3 7803051 7800000 100 | |
85 BENCHMARK(BM_StringPieceEquality) | |
86 ->ArgPair(1<<20, STRCMP_EQUAL) | |
87 ->ArgPair(1<<20, STRCMP_FIRST_CHAR_DIFFERS) | |
88 ->ArgPair(1<<20, STRING_PIECES_EQUAL_CONSTANT_TIME_EQUAL) | |
89 ->ArgPair(1<<20, STRING_PIECES_EQUAL_CONSTANT_TIME_FIRST_CHAR_DIFFERS); | |
90 */ | |
91 | |
92 } // namespace | |
93 | |
94 } // namespace net | |
OLD | NEW |