21 #include "caffe2/core/db.h" 22 #include "caffe2/core/init.h" 23 #include "caffe2/core/timer.h" 24 #include "caffe2/core/logging.h" 26 C10_DEFINE_string(input_db,
"",
"The input db.");
27 C10_DEFINE_string(input_db_type,
"",
"The input db type.");
28 C10_DEFINE_int(report_interval, 1000,
"The report interval.");
29 C10_DEFINE_int(repeat, 10,
"The number to repeat the throughput test.");
30 C10_DEFINE_bool(use_reader,
false,
"If true, use the reader interface.");
34 "The number of concurrent reading threads.");
41 void TestThroughputWithDB() {
42 std::unique_ptr<DB> in_db(caffe2::db::CreateDB(
43 FLAGS_input_db_type, FLAGS_input_db, caffe2::db::READ));
44 std::unique_ptr<Cursor> cursor(in_db->NewCursor());
45 for (
int iter_id = 0; iter_id < FLAGS_repeat; ++iter_id) {
47 for (
int i = 0; i < FLAGS_report_interval; ++i) {
48 string key = cursor->key();
49 string value = cursor->value();
52 if (!cursor->Valid()) {
53 cursor->SeekToFirst();
56 double elapsed_seconds = timer.
Seconds();
58 "Iteration %03d, took %4.5f seconds, throughput %f items/sec.\n",
61 FLAGS_report_interval / elapsed_seconds);
65 void TestThroughputWithReaderWorker(
const DBReader* reader,
int thread_id) {
67 for (
int iter_id = 0; iter_id < FLAGS_repeat; ++iter_id) {
69 for (
int i = 0; i < FLAGS_report_interval; ++i) {
70 reader->
Read(&key, &value);
72 double elapsed_seconds = timer.
Seconds();
74 "Thread %03d iteration %03d, took %4.5f seconds, " 75 "throughput %f items/sec.\n",
79 FLAGS_report_interval / elapsed_seconds);
83 void TestThroughputWithReader() {
85 std::vector<std::unique_ptr<std::thread>> reading_threads(
86 FLAGS_num_read_threads);
87 for (
int i = 0; i < reading_threads.size(); ++i) {
88 reading_threads[i].reset(
new std::thread(
89 TestThroughputWithReaderWorker, &reader, i));
91 for (
int i = 0; i < reading_threads.size(); ++i) {
92 reading_threads[i]->join();
96 int main(
int argc,
char** argv) {
98 if (FLAGS_use_reader) {
99 TestThroughputWithReader();
101 TestThroughputWithDB();
void Read(string *key, string *value) const
Read a set of key and value from the db and move to next.
An abstract class for the cursor of the database while reading.
A reader wrapper for DB that also allows us to serialize it.
float Seconds()
Returns the elapsed time in seconds.
An abstract class for accessing a database of key-value pairs.
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.
A simple timer object for measuring time.