cannam@62
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
cannam@62
|
2 // Licensed under the MIT License:
|
cannam@62
|
3 //
|
cannam@62
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@62
|
5 // of this software and associated documentation files (the "Software"), to deal
|
cannam@62
|
6 // in the Software without restriction, including without limitation the rights
|
cannam@62
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@62
|
8 // copies of the Software, and to permit persons to whom the Software is
|
cannam@62
|
9 // furnished to do so, subject to the following conditions:
|
cannam@62
|
10 //
|
cannam@62
|
11 // The above copyright notice and this permission notice shall be included in
|
cannam@62
|
12 // all copies or substantial portions of the Software.
|
cannam@62
|
13 //
|
cannam@62
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@62
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@62
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@62
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@62
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@62
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@62
|
20 // THE SOFTWARE.
|
cannam@62
|
21
|
cannam@62
|
22 #include "catrank.pb.h"
|
cannam@62
|
23 #include "protobuf-common.h"
|
cannam@62
|
24
|
cannam@62
|
25 namespace capnp {
|
cannam@62
|
26 namespace benchmark {
|
cannam@62
|
27 namespace protobuf {
|
cannam@62
|
28
|
cannam@62
|
29 struct ScoredResult {
|
cannam@62
|
30 double score;
|
cannam@62
|
31 const SearchResult* result;
|
cannam@62
|
32
|
cannam@62
|
33 ScoredResult() = default;
|
cannam@62
|
34 ScoredResult(double score, const SearchResult* result): score(score), result(result) {}
|
cannam@62
|
35
|
cannam@62
|
36 inline bool operator<(const ScoredResult& other) const { return score > other.score; }
|
cannam@62
|
37 };
|
cannam@62
|
38
|
cannam@62
|
39 class CatRankTestCase {
|
cannam@62
|
40 public:
|
cannam@62
|
41 typedef SearchResultList Request;
|
cannam@62
|
42 typedef SearchResultList Response;
|
cannam@62
|
43 typedef int Expectation;
|
cannam@62
|
44
|
cannam@62
|
45 static int setupRequest(SearchResultList* request) {
|
cannam@62
|
46 int count = fastRand(1000);
|
cannam@62
|
47 int goodCount = 0;
|
cannam@62
|
48
|
cannam@62
|
49 for (int i = 0; i < count; i++) {
|
cannam@62
|
50 SearchResult* result = request->add_result();
|
cannam@62
|
51 result->set_score(1000 - i);
|
cannam@62
|
52 result->set_url("http://example.com/");
|
cannam@62
|
53 std::string* url = result->mutable_url();
|
cannam@62
|
54 int urlSize = fastRand(100);
|
cannam@62
|
55 for (int j = 0; j < urlSize; j++) {
|
cannam@62
|
56 url->push_back('a' + fastRand(26));
|
cannam@62
|
57 }
|
cannam@62
|
58
|
cannam@62
|
59 bool isCat = fastRand(8) == 0;
|
cannam@62
|
60 bool isDog = fastRand(8) == 0;
|
cannam@62
|
61 goodCount += isCat && !isDog;
|
cannam@62
|
62
|
cannam@62
|
63 std::string* snippet = result->mutable_snippet();
|
cannam@62
|
64 snippet->reserve(7 * 22);
|
cannam@62
|
65 snippet->push_back(' ');
|
cannam@62
|
66
|
cannam@62
|
67 int prefix = fastRand(20);
|
cannam@62
|
68 for (int j = 0; j < prefix; j++) {
|
cannam@62
|
69 snippet->append(WORDS[fastRand(WORDS_COUNT)]);
|
cannam@62
|
70 }
|
cannam@62
|
71
|
cannam@62
|
72 if (isCat) snippet->append("cat ");
|
cannam@62
|
73 if (isDog) snippet->append("dog ");
|
cannam@62
|
74
|
cannam@62
|
75 int suffix = fastRand(20);
|
cannam@62
|
76 for (int j = 0; j < suffix; j++) {
|
cannam@62
|
77 snippet->append(WORDS[fastRand(WORDS_COUNT)]);
|
cannam@62
|
78 }
|
cannam@62
|
79 }
|
cannam@62
|
80
|
cannam@62
|
81 return goodCount;
|
cannam@62
|
82 }
|
cannam@62
|
83
|
cannam@62
|
84 static void handleRequest(const SearchResultList& request, SearchResultList* response) {
|
cannam@62
|
85 std::vector<ScoredResult> scoredResults;
|
cannam@62
|
86
|
cannam@62
|
87 for (auto& result: request.result()) {
|
cannam@62
|
88 double score = result.score();
|
cannam@62
|
89 if (result.snippet().find(" cat ") != std::string::npos) {
|
cannam@62
|
90 score *= 10000;
|
cannam@62
|
91 }
|
cannam@62
|
92 if (result.snippet().find(" dog ") != std::string::npos) {
|
cannam@62
|
93 score /= 10000;
|
cannam@62
|
94 }
|
cannam@62
|
95 scoredResults.emplace_back(score, &result);
|
cannam@62
|
96 }
|
cannam@62
|
97
|
cannam@62
|
98 std::sort(scoredResults.begin(), scoredResults.end());
|
cannam@62
|
99
|
cannam@62
|
100 for (auto& result: scoredResults) {
|
cannam@62
|
101 SearchResult* out = response->add_result();
|
cannam@62
|
102 out->set_score(result.score);
|
cannam@62
|
103 out->set_url(result.result->url());
|
cannam@62
|
104 out->set_snippet(result.result->snippet());
|
cannam@62
|
105 }
|
cannam@62
|
106 }
|
cannam@62
|
107
|
cannam@62
|
108 static bool checkResponse(const SearchResultList& response, int expectedGoodCount) {
|
cannam@62
|
109 int goodCount = 0;
|
cannam@62
|
110 for (auto& result: response.result()) {
|
cannam@62
|
111 if (result.score() > 1001) {
|
cannam@62
|
112 ++goodCount;
|
cannam@62
|
113 } else {
|
cannam@62
|
114 break;
|
cannam@62
|
115 }
|
cannam@62
|
116 }
|
cannam@62
|
117
|
cannam@62
|
118 return goodCount == expectedGoodCount;
|
cannam@62
|
119 }
|
cannam@62
|
120 };
|
cannam@62
|
121
|
cannam@62
|
122 } // namespace protobuf
|
cannam@62
|
123 } // namespace benchmark
|
cannam@62
|
124 } // namespace capnp
|
cannam@62
|
125
|
cannam@62
|
126 int main(int argc, char* argv[]) {
|
cannam@62
|
127 return capnp::benchmark::benchmarkMain<
|
cannam@62
|
128 capnp::benchmark::protobuf::BenchmarkTypes,
|
cannam@62
|
129 capnp::benchmark::protobuf::CatRankTestCase>(argc, argv);
|
cannam@62
|
130 }
|