Skip to content

Instantly share code, notes, and snippets.

@JakSprats
Created December 15, 2011 12:40
Show Gist options
  • Save JakSprats/1480963 to your computer and use it in GitHub Desktop.
Save JakSprats/1480963 to your computer and use it in GitHub Desktop.
LevelDB vs BerkeleyDB benchmark
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <db_cxx.h>
using namespace std;
int main(int argc, char** argv)
{
if (argc < 2)
{
cerr << "invalid number of arguments. Missing keys file\n";
return 1;
}
Db db(0,0);
db.set_error_stream(&cerr);
db.set_pagesize(1024);
db.set_cachesize(0, 64 * 1024, 0);
db.open(NULL, "../bdb.db", NULL, DB_BTREE, DB_RDONLY, 0664);
ifstream inFile(argv[1]);
string line;
string keys[1000000];
cout << "ready to load keys from " << argv[1] << "\n";
int index = 0;
while(inFile.good())
{
getline(inFile, line);
if (line.length() < 1)
continue;
keys[index] = line;
++index;
}
cout << "done loading keys, ready to start test..." << endl;
int iterations = 2000000;
Dbt key;
Dbt data;
index = 0;
int ret = 0;
srand(time(NULL));
clock_t start = clock();
for(int i = 0; i < iterations; ++i)
{
index = rand() % 1000000;
string& strKey = keys[index];
key.set_data((void*)strKey.c_str());
key.set_size(strKey.length() + 1);
ret = db.get(NULL, &key, &data, 0);
if (ret == DB_NOTFOUND)
cout << "key " << strKey << " not found\n";
}
clock_t finish = clock();
printf("time to random access %d keys is %.5f\n",
iterations,
((double) finish - start) / CLOCKS_PER_SEC);
db.close(0);
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <leveldb/db.h>
#include <leveldb/slice.h>
using namespace std;
int main(int argc, char** argv)
{
if (argc < 2)
{
cerr << "invalid number of arguments. Missing keys file\n";
return 1;
}
leveldb::DB* db = 0;
leveldb::Options options;
options.create_if_missing = false;
leveldb::Status status = leveldb::DB::Open(options, "../leveldb", &db);
if (!status.ok())
{
cerr << "failed to open leveldb" << endl;
cerr << status.ToString() << endl;
return 1;
}
ifstream inFile(argv[1]);
string line;
string keys[1000000];
cout << "ready to load keys from " << argv[1] << "\n";
int index = 0;
while(inFile.good())
{
getline(inFile, line);
if (line.length() < 1)
continue;
keys[index] = line;
++index;
}
cout << "done loading keys, ready to start test..." << endl;
int iterations = 2000000;
index = 0;
srand(time(NULL));
string data;
clock_t start = clock();
for(int i = 0; i < iterations; ++i)
{
index = rand() % 1000000;
string& strKey = keys[index];
leveldb::Status ret = db->Get(leveldb::ReadOptions(), strKey, &data);
if (!ret.ok())
{
cout << "key " << strKey << "value " << data << " lookup failed\n";
cout << ret.ToString() << endl;
return 1;
}
}
clock_t finish = clock();
printf("time to random access %d keys is %.5f\n",
iterations,
((double) finish - start) / CLOCKS_PER_SEC);
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <db_cxx.h>
using namespace std;
int main(int argc, char** argv)
{
Db db(0,0);
db.set_error_stream(&cerr);
db.set_pagesize(1024);
db.set_cachesize(0, 64 * 1024, 0);
db.open(NULL, "../bdb.db", NULL, DB_BTREE, DB_CREATE, 0664);
ifstream inFile(argv[1]);
string line;
char keybuf[10];
long lenTotal = 0;
int count = 0;
while(inFile.good())
{
getline(inFile, line);
memcpy(keybuf, line.c_str(), 9);
keybuf[9] = 0;
Dbt key((void*)keybuf, 10);
Dbt data((void*)line.c_str(), line.length() + 1);
if (line.length() < 1)
continue;
++count;
lenTotal += line.length() + 1;
int ret = db.put(0, &key, &data, DB_NOOVERWRITE);
if (ret)
cout << "key " << keybuf << " already exists.\n";
else
cout << "inserted " << keybuf << " data len = " << line.length() + 1 << "\n";
}
db.close(0);
cout << "avg data size " << (int)lenTotal/count << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <leveldb/db.h>
#include <leveldb/slice.h>
using namespace std;
int main(int argc, char** argv)
{
leveldb::DB* db = 0;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "../leveldb", &db);
if (!status.ok())
{
cerr << "failed to create leveldb" << endl;
cerr << status.ToString() << endl;
return 1;
}
ifstream inFile(argv[1]);
string line;
char keybuf[10];
long lenTotal = 0;
int count = 0;
while(inFile.good())
{
getline(inFile, line);
memcpy(keybuf, line.c_str(), 9);
keybuf[9] = 0;
if (line.length() < 1)
continue;
leveldb::Slice key(keybuf);
leveldb::Slice data(line);
++count;
lenTotal += line.length() + 1;
status = db->Put(leveldb::WriteOptions(), key, data);
if (!status.ok())
cout << "key " << keybuf << " already exists.\n";
else
cout << "inserted " << keybuf << " data len = " << line.length() + 1 << "\n";
}
cout << "avg data size " << (int)lenTotal/count << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment