From b8b0cde5c722d59613a564c8cfc77a2ddc9814da Mon Sep 17 00:00:00 2001 From: Davi Reis Date: Wed, 7 Mar 2012 01:00:17 -0500 Subject: [PATCH] Added miss ratio to benchmark tools. --- cxxmph/bm_common.h | 4 ++-- cxxmph/bm_index.cc | 6 +++--- cxxmph/bm_map.cc | 50 +++++++++++++--------------------------------- 3 files changed, 19 insertions(+), 41 deletions(-) diff --git a/cxxmph/bm_common.h b/cxxmph/bm_common.h index c67b5cd..aaf12b9 100644 --- a/cxxmph/bm_common.h +++ b/cxxmph/bm_common.h @@ -31,8 +31,8 @@ class UrlsBenchmark : public Benchmark { class SearchUrlsBenchmark : public UrlsBenchmark { public: - SearchUrlsBenchmark(const std::string& urls_file, uint32_t nsearches) - : UrlsBenchmark(urls_file), nsearches_(nsearches), miss_ratio_(0.2) {} + SearchUrlsBenchmark(const std::string& urls_file, uint32_t nsearches, float miss_ratio) + : UrlsBenchmark(urls_file), nsearches_(nsearches), miss_ratio_(miss_ratio) {} protected: virtual bool SetUp(); const uint32_t nsearches_; diff --git a/cxxmph/bm_index.cc b/cxxmph/bm_index.cc index a7c0a14..e700840 100644 --- a/cxxmph/bm_index.cc +++ b/cxxmph/bm_index.cc @@ -42,7 +42,7 @@ class BM_STLIndexCreate : public UrlsBenchmark { class BM_MPHIndexSearch : public SearchUrlsBenchmark { public: BM_MPHIndexSearch(const std::string& urls_file, int nsearches) - : SearchUrlsBenchmark(urls_file, nsearches) { } + : SearchUrlsBenchmark(urls_file, nsearches, 0) { } virtual void Run() { for (auto it = random_.begin(); it != random_.end(); ++it) { auto idx = index_.index(*it); @@ -62,7 +62,7 @@ class BM_MPHIndexSearch : public SearchUrlsBenchmark { class BM_CmphIndexSearch : public SearchUrlsBenchmark { public: BM_CmphIndexSearch(const std::string& urls_file, int nsearches) - : SearchUrlsBenchmark(urls_file, nsearches) { } + : SearchUrlsBenchmark(urls_file, nsearches, 0) { } ~BM_CmphIndexSearch() { if (index_) cmph_destroy(index_); } virtual void Run() { for (auto it = random_.begin(); it != random_.end(); ++it) { @@ -112,7 +112,7 @@ class BM_CmphIndexSearch : public SearchUrlsBenchmark { class BM_STLIndexSearch : public SearchUrlsBenchmark { public: BM_STLIndexSearch(const std::string& urls_file, int nsearches) - : SearchUrlsBenchmark(urls_file, nsearches) { } + : SearchUrlsBenchmark(urls_file, nsearches, 0) { } virtual void Run() { for (auto it = random_.begin(); it != random_.end(); ++it) { auto idx = index_.find(*it); diff --git a/cxxmph/bm_map.cc b/cxxmph/bm_map.cc index 8d2aef1..42516b6 100644 --- a/cxxmph/bm_map.cc +++ b/cxxmph/bm_map.cc @@ -10,24 +10,11 @@ namespace cxxmph { - uint64_t myfind(const unordered_map& mymap, const uint64_t& k) { + template + const T* myfind(const Container& mymap, const T& k) { auto it = mymap.find(k); - if (it == mymap.end()) return -1; - return it->second; - } - - uint64_t myfind(const mph_map& mymap, const uint64_t& k) { - return mymap.find(k)->second; - } - - const StringPiece& myfind(const unordered_map& mymap, const StringPiece& k) { - auto it = mymap.find(k); - if (it == mymap.end()) return ".force_miss"; - return it->second; - } - StringPiece myfind(const mph_map& mymap, const StringPiece& k) { - auto it = mymap.find(k); - return it->second; + if (it == mymap.end()) return NULL; + return &it->second; } template @@ -45,25 +32,14 @@ template class BM_SearchUrls : public SearchUrlsBenchmark { public: - BM_SearchUrls(const std::string& urls_file, int nsearches) - : SearchUrlsBenchmark(urls_file, nsearches) { } + BM_SearchUrls(const std::string& urls_file, int nsearches, float miss_ratio) + : SearchUrlsBenchmark(urls_file, nsearches, miss_ratio) { } virtual void Run() { - fprintf(stderr, "Running benchmark\n"); for (auto it = random_.begin(); it != random_.end(); ++it) { - if (it->ends_with(".force_miss")) { - fprintf(stderr, "About to miss\n"); - } else { - fprintf(stderr, "No miss\n"); - } - fprintf(stderr, "it: *%s\n", it->as_string().c_str()); auto v = myfind(mymap_, *it); - fprintf(stderr, "v: %s, it: *%s\n", v.as_string().c_str(), it->as_string().c_str()); - if (v != *it && !it->ends_with(".force_miss")) { - fprintf(stderr, "Looked for %s got %s\n", it->data(), v.data()); - exit(-1); - } + assert(it->ends_with(".force_miss") ^ v != NULL); + assert(!v || *v == *it); } - fprintf(stderr, "Done running benchmark\n"); } protected: virtual bool SetUp() { @@ -96,8 +72,8 @@ class BM_SearchUint64 : public SearchUint64Benchmark { virtual void Run() { for (auto it = random_.begin(); it != random_.end(); ++it) { auto v = myfind(mymap_, *it); - if (v != *it) { - fprintf(stderr, "Looked for %lu got %lu\n", *it, v); + if (*v != *it) { + fprintf(stderr, "Looked for %lu got %lu\n", *it, *v); exit(-1); } } @@ -115,8 +91,10 @@ int main(int argc, char** argv) { Benchmark::Register(new BM_CreateUrls>("URLS100k")); Benchmark::Register(new BM_CreateUrls>("URLS100k")); */ - // Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000)); - Benchmark::Register(new BM_SearchUrls>("URLS100k", 10)); + Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0)); + Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0)); + Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0.9)); + Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0.9)); /* Benchmark::Register(new BM_SearchUint64>); Benchmark::Register(new BM_SearchUint64>);