// How fast can we insert 62 million “records” into LevelDB? // This program does it in 3'24" on my laptop (a four-core 1.6GHz // N3700 with a 100-gigabyte 50-megabyte-per-second SSD), which works // out to 304 kilorecords per second. The ultimately resulting // LevelDB is 516 megabytes, for 8.3 bytes per record and 2.5 // megabytes per second, though some of this is fixed transaction log // overhead. #include #include #include #include #include using leveldb::DB; using leveldb::Options; using leveldb::WriteOptions; using leveldb::WriteBatch; using leveldb::Status; using std::cerr; using std::endl; using std::string; using std::to_string; // C++11; g++ -std=c++11 const int batchSize = 2000; int main(int argc, char **argv) { DB *db = 0; Options options; options.create_if_missing = true; Status s = DB::Open(options, "62m.tmp.db", &db); string sk = "k"; string sv = "v"; for (int i = 0; i < 62*1000*1000; i += batchSize) { if (s.ok()) { WriteBatch batch; for (int j = 0; j != batchSize; ++j) { string is = to_string(i + j); batch.Put(sk + is, sv + is); } s = db->Write(WriteOptions(), &batch); } } delete db; // I should really be using a smart pointer, huh? if (!s.ok()) { cerr << s.ToString() << endl; return -1; } return 0; }