2011-05-23 21:01:08 +03:00
|
|
|
#include <string>
|
2012-06-01 23:49:00 +03:00
|
|
|
#include <unordered_map>
|
2011-05-23 21:01:08 +03:00
|
|
|
|
|
|
|
#include "bm_common.h"
|
|
|
|
#include "mph_map.h"
|
|
|
|
|
2012-03-07 08:48:20 +02:00
|
|
|
using std::string;
|
2011-05-23 21:01:08 +03:00
|
|
|
|
2012-03-21 15:20:30 +02:00
|
|
|
// Another reference benchmark:
|
|
|
|
// http://blog.aggregateknowledge.com/tag/bigmemory/
|
|
|
|
|
2012-03-07 08:48:20 +02:00
|
|
|
namespace cxxmph {
|
2011-05-23 21:01:08 +03:00
|
|
|
|
2012-04-14 23:59:15 +03:00
|
|
|
template <class MapType, class T>
|
2012-03-07 08:48:20 +02:00
|
|
|
const T* myfind(const MapType& mymap, const T& k) {
|
|
|
|
auto it = mymap.find(k);
|
2012-03-12 04:21:18 +02:00
|
|
|
auto end = mymap.end();
|
|
|
|
if (it == end) return NULL;
|
2012-03-07 08:48:20 +02:00
|
|
|
return &it->second;
|
|
|
|
}
|
2011-06-14 08:24:40 +03:00
|
|
|
|
2012-03-07 08:48:20 +02:00
|
|
|
template <class MapType>
|
|
|
|
class BM_CreateUrls : public UrlsBenchmark {
|
|
|
|
public:
|
|
|
|
BM_CreateUrls(const string& urls_file) : UrlsBenchmark(urls_file) { }
|
|
|
|
virtual void Run() {
|
|
|
|
MapType mymap;
|
|
|
|
for (auto it = urls_.begin(); it != urls_.end(); ++it) {
|
|
|
|
mymap[*it] = *it;
|
2011-05-23 21:01:08 +03:00
|
|
|
}
|
2012-03-07 08:48:20 +02:00
|
|
|
}
|
2011-05-23 21:01:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class MapType>
|
2011-06-14 08:24:40 +03:00
|
|
|
class BM_SearchUrls : public SearchUrlsBenchmark {
|
2011-05-23 21:01:08 +03:00
|
|
|
public:
|
2012-03-07 08:00:17 +02:00
|
|
|
BM_SearchUrls(const std::string& urls_file, int nsearches, float miss_ratio)
|
|
|
|
: SearchUrlsBenchmark(urls_file, nsearches, miss_ratio) { }
|
2012-04-14 23:59:15 +03:00
|
|
|
virtual ~BM_SearchUrls() {}
|
2011-05-23 21:01:08 +03:00
|
|
|
virtual void Run() {
|
2012-04-21 22:48:32 +03:00
|
|
|
uint32_t total = 1;
|
2011-05-23 21:01:08 +03:00
|
|
|
for (auto it = random_.begin(); it != random_.end(); ++it) {
|
2012-04-21 22:48:32 +03:00
|
|
|
auto v = myfind(mymap_, *it);
|
|
|
|
if (v) total += v->length();
|
2011-05-23 21:01:08 +03:00
|
|
|
}
|
2012-04-21 22:48:32 +03:00
|
|
|
fprintf(stderr, "Total: %u\n", total);
|
2011-05-23 21:01:08 +03:00
|
|
|
}
|
|
|
|
protected:
|
2011-05-24 03:18:24 +03:00
|
|
|
virtual bool SetUp() {
|
2011-06-13 08:16:19 +03:00
|
|
|
if (!SearchUrlsBenchmark::SetUp()) return false;
|
2011-05-23 21:01:08 +03:00
|
|
|
for (auto it = urls_.begin(); it != urls_.end(); ++it) {
|
|
|
|
mymap_[*it] = *it;
|
|
|
|
}
|
2011-05-24 03:18:24 +03:00
|
|
|
mymap_.rehash(mymap_.bucket_count());
|
2012-03-14 06:29:13 +02:00
|
|
|
fprintf(stderr, "Occupation: %f\n", static_cast<float>(mymap_.size())/mymap_.bucket_count());
|
2011-05-24 03:18:24 +03:00
|
|
|
return true;
|
2011-05-23 21:01:08 +03:00
|
|
|
}
|
|
|
|
MapType mymap_;
|
|
|
|
};
|
|
|
|
|
2011-06-14 08:24:40 +03:00
|
|
|
template <class MapType>
|
|
|
|
class BM_SearchUint64 : public SearchUint64Benchmark {
|
|
|
|
public:
|
2012-03-14 06:29:13 +02:00
|
|
|
BM_SearchUint64() : SearchUint64Benchmark(100000, 10*1000*1000) { }
|
2011-06-14 08:24:40 +03:00
|
|
|
virtual bool SetUp() {
|
|
|
|
if (!SearchUint64Benchmark::SetUp()) return false;
|
2012-04-15 06:03:00 +03:00
|
|
|
for (uint32_t i = 0; i < values_.size(); ++i) {
|
2011-06-14 08:24:40 +03:00
|
|
|
mymap_[values_[i]] = values_[i];
|
|
|
|
}
|
|
|
|
mymap_.rehash(mymap_.bucket_count());
|
2011-06-14 09:32:02 +03:00
|
|
|
// Double check if everything is all right
|
2012-04-22 03:41:43 +03:00
|
|
|
cerr << "Doing double check" << endl;
|
2012-04-15 06:03:00 +03:00
|
|
|
for (uint32_t i = 0; i < values_.size(); ++i) {
|
2012-04-22 03:41:43 +03:00
|
|
|
if (mymap_[values_[i]] != values_[i]) {
|
2012-05-28 07:39:36 +03:00
|
|
|
cerr << "Looking for " << i << " th key value " << values_[i];
|
|
|
|
cerr << " yielded " << mymap_[values_[i]] << endl;
|
2012-04-22 03:41:43 +03:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-14 09:32:02 +03:00
|
|
|
}
|
2011-06-14 08:24:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
virtual void Run() {
|
|
|
|
for (auto it = random_.begin(); it != random_.end(); ++it) {
|
|
|
|
auto v = myfind(mymap_, *it);
|
2012-03-07 08:00:17 +02:00
|
|
|
if (*v != *it) {
|
2012-05-28 07:39:36 +03:00
|
|
|
cerr << "Looked for " << *it << " got " << *v << endl;
|
2011-06-14 09:32:02 +03:00
|
|
|
exit(-1);
|
|
|
|
}
|
2011-06-14 08:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MapType mymap_;
|
|
|
|
};
|
|
|
|
|
2011-05-23 21:01:08 +03:00
|
|
|
} // namespace cxxmph
|
|
|
|
|
|
|
|
using namespace cxxmph;
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2011-06-14 09:32:02 +03:00
|
|
|
srandom(4);
|
2012-06-03 09:13:06 +03:00
|
|
|
Benchmark::Register(new BM_CreateUrls<dense_hash_map<StringPiece, StringPiece>>("URLS100k"));
|
|
|
|
Benchmark::Register(new BM_CreateUrls<std::unordered_map<StringPiece, StringPiece>>("URLS100k"));
|
2011-06-14 08:24:40 +03:00
|
|
|
Benchmark::Register(new BM_CreateUrls<mph_map<StringPiece, StringPiece>>("URLS100k"));
|
2012-06-03 09:13:06 +03:00
|
|
|
Benchmark::Register(new BM_CreateUrls<sparse_hash_map<StringPiece, StringPiece>>("URLS100k"));
|
|
|
|
|
|
|
|
Benchmark::Register(new BM_SearchUrls<dense_hash_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0));
|
|
|
|
Benchmark::Register(new BM_SearchUrls<std::unordered_map<StringPiece, StringPiece, Murmur3StringPiece>>("URLS100k", 10*1000 * 1000, 0));
|
2012-03-07 08:00:17 +02:00
|
|
|
Benchmark::Register(new BM_SearchUrls<mph_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0));
|
2012-06-03 09:13:06 +03:00
|
|
|
Benchmark::Register(new BM_SearchUrls<sparse_hash_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0));
|
|
|
|
|
|
|
|
Benchmark::Register(new BM_SearchUrls<dense_hash_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0.9));
|
|
|
|
Benchmark::Register(new BM_SearchUrls<std::unordered_map<StringPiece, StringPiece, Murmur3StringPiece>>("URLS100k", 10*1000 * 1000, 0.9));
|
2012-03-15 04:23:48 +02:00
|
|
|
Benchmark::Register(new BM_SearchUrls<mph_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0.9));
|
2012-06-03 09:13:06 +03:00
|
|
|
Benchmark::Register(new BM_SearchUrls<sparse_hash_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0.9));
|
|
|
|
|
|
|
|
Benchmark::Register(new BM_SearchUint64<dense_hash_map<uint64_t, uint64_t>>);
|
|
|
|
Benchmark::Register(new BM_SearchUint64<std::unordered_map<uint64_t, uint64_t>>);
|
2011-06-14 08:24:40 +03:00
|
|
|
Benchmark::Register(new BM_SearchUint64<mph_map<uint64_t, uint64_t>>);
|
2012-06-03 09:13:06 +03:00
|
|
|
Benchmark::Register(new BM_SearchUint64<sparse_hash_map<uint64_t, uint64_t>>);
|
2011-05-23 21:01:08 +03:00
|
|
|
Benchmark::RunAll();
|
|
|
|
}
|