github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/benchmark/fs/rocksdb_storage.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #include "benchmark/benchmark_instances.h" 21 #include "benchmark/fs/common.h" 22 #include "common/configuration.h" 23 #include "fs/util.h" 24 #include <vector> 25 26 std::string cur_path; 27 std::string db_read_path; 28 std::string db_write_path; 29 30 std::unique_ptr<neb::fs::rocksdb_storage> db_read_ptr; 31 std::unique_ptr<neb::fs::rocksdb_storage> db_write_ptr; 32 33 BENCHMARK(rocksdb_storage, rocksdb_storage_init) { 34 cur_path = neb::configuration::instance().root_dir(); 35 db_read_path = neb::fs::join_path(cur_path, "test/data/read-data.db"); 36 db_write_path = neb::fs::join_path(cur_path, "test/data/write-data.db"); 37 38 db_read_ptr = std::make_unique<neb::fs::rocksdb_storage>(); 39 db_write_ptr = std::make_unique<neb::fs::rocksdb_storage>(); 40 } 41 42 BENCHMARK(rocksdb_storage, open_close_database) { 43 db_read_ptr->open_database(db_read_path, neb::fs::storage_open_for_readonly); 44 db_read_ptr->close_database(); 45 } 46 47 BENCHMARK(rocksdb_storage, put_bytes) { 48 db_write_ptr->open_database(db_write_path, 49 neb::fs::storage_open_for_readwrite); 50 51 std::string key = "benchmark_rocksdb_storage"; 52 size_t eval_count = 10; 53 for (size_t i = 0; i < eval_count; i++) { 54 std::string k = key + '_' + std::to_string(i); 55 std::string v = neb::util::string_to_byte(k).to_hex(); 56 db_write_ptr->put_bytes(neb::util::string_to_byte(k), 57 neb::util::string_to_byte(v)); 58 } 59 db_write_ptr->close_database(); 60 } 61 62 BENCHMARK(rocksdb_storage, get_bytes) { 63 db_write_ptr->open_database(db_write_path, 64 neb::fs::storage_open_for_readonly); 65 66 std::string key = "benchmark_rocksdb_storage"; 67 size_t eval_count = 10; 68 for (size_t i = 0; i < eval_count; i++) { 69 std::string k = key + '_' + std::to_string(i); 70 db_write_ptr->get_bytes(neb::util::string_to_byte(k)); 71 } 72 db_write_ptr->close_database(); 73 } 74 75 BENCHMARK(rocksdb_storage, del_bytes) { 76 db_write_ptr->open_database(db_write_path, 77 neb::fs::storage_open_for_readwrite); 78 79 std::string key = "benchmark_rocksdb_storage"; 80 size_t eval_count = 10; 81 for (size_t i = 0; i < eval_count; i++) { 82 std::string k = key + '_' + std::to_string(i); 83 db_write_ptr->del_by_bytes(neb::util::string_to_byte(k)); 84 } 85 db_write_ptr->close_database(); 86 } 87 88 BENCHMARK(rocksdb_storage, put) { 89 db_write_ptr->open_database(db_write_path, 90 neb::fs::storage_open_for_readwrite); 91 92 std::string key = "benchmark_rocksdb_storage"; 93 size_t eval_count = 10; 94 for (size_t i = 0; i < eval_count; i++) { 95 std::string k = key + '_' + std::to_string(i); 96 std::string v = neb::util::string_to_byte(k).to_hex(); 97 db_write_ptr->put(k, neb::util::string_to_byte(v)); 98 } 99 db_write_ptr->close_database(); 100 } 101 102 BENCHMARK(rocksdb_storage, get) { 103 db_write_ptr->open_database(db_write_path, 104 neb::fs::storage_open_for_readonly); 105 106 std::string key = "benchmark_rocksdb_storage"; 107 size_t eval_count = 10; 108 for (size_t i = 0; i < eval_count; i++) { 109 std::string k = key + '_' + std::to_string(i); 110 db_write_ptr->get(k); 111 } 112 db_write_ptr->close_database(); 113 } 114 115 BENCHMARK(rocksdb_storage, del) { 116 db_write_ptr->open_database(db_write_path, 117 neb::fs::storage_open_for_readwrite); 118 119 std::string key = "benchmark_rocksdb_storage"; 120 size_t eval_count = 10; 121 for (size_t i = 0; i < eval_count; i++) { 122 std::string k = key + '_' + std::to_string(i); 123 db_write_ptr->del(k); 124 } 125 db_write_ptr->close_database(); 126 } 127 128 BENCHMARK(rocksdb_storage, rocksdb_storage_destroy) { 129 db_read_ptr.reset(nullptr); 130 db_write_ptr.reset(nullptr); 131 }