capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

protobuf-catrank.c++ (4134B)


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