/
barrage_db.cpp
129 lines (106 loc) · 4.13 KB
/
barrage_db.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
This file is part of barrageDB
Copyright (C) 2015 Spyridon Papageorgiou
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iostream>
#include <vector>
#include <boost/program_options.hpp>
#include "worker_thread.hpp"
#include "db_metadata.hpp"
#include "db_ops.hpp"
int main(int argc, char *argv[])
{
int i = 0, ret;
int thread_count;
int cleanup;
int duration;
int prevalidate;
std::string conf_file;
db_metadata db;
namespace po = boost::program_options;
po::options_description desc("barrageDB options");
desc.add_options()
("help", "generates help message")
("threads", po::value<int>(&thread_count)->default_value(10), "set number of threads")
("time", po::value<int>(&duration)->default_value(60), "set time to run (in seconds)")
("conf-file", po::value<std::string>(&conf_file)->default_value("config.ini"), "configuration file name")
("validate", "validate database contents offline")
("prevalidate", po::value<int>(&prevalidate)->default_value(0), "validate existing table before starting")
("cleanup", po::value<int>(&cleanup)->default_value(0), "cleanup database on exit");
po::variables_map var_map;
po::store(po::parse_command_line(argc, argv, desc), var_map);
po::notify(var_map);
if (var_map.count("help")) {
std::cout << desc << "\n";
return 1;
}
try {
db.load(conf_file);
//db.print_contents();
std::cout << "========= barrageDB ========" << std::endl;
if (var_map.count("validate")) {
// Simply validate database contents and exit.
ret = check_table_exists(db.generate_connection_string());
if (ret == 0) {
std::cout << ">Validation mode: did not find barrage_data table in the database." << std::endl;
return 0;
} else if (ret == CONNECTION_ERROR) {
std::cout << ">Unable to connect to the database server specified. The following configuration was provided:" << std::endl;
db.print_contents();
return 0;
}
// Could make this smart, have more worker threads to speed things up
WorkGroup validation_group(db.generate_connection_string(), 1, 0, VALIDATOR);
validation_group.start();
validation_group.wait_all();
} else {
std::cout << ">Number of threads : " << thread_count << std::endl;
std::cout << ">Test duration : " << duration << std::endl;
std::cout << ">configuration file : " << conf_file << std::endl;
ret = check_table_exists(db.generate_connection_string());
if (ret == 0) {
create_table(db.generate_connection_string());
std::cout << "Created new table. Prepopulating with 1000 entries" << std::endl;
WorkGroup population_group(db.generate_connection_string(), 10, 0, POPULATOR);
population_group.start();
population_group.wait_all();
} else if (ret == CONNECTION_ERROR) {
std::cout << ">Unable to connect to the database server specified. The following configuration was provided:" << std::endl;
db.print_contents();
return 0;
} else {
// Table exists, could validate before running?
if (prevalidate) {
WorkGroup validation_group(db.generate_connection_string(), 1, 0, VALIDATOR);
validation_group.start();
validation_group.wait_all();
}
}
WorkGroup group1(db.generate_connection_string(), thread_count, duration, GENERATOR);
group1.start();
group1.wait_all();
}
if (cleanup) {
ret = drop_table(db.generate_connection_string());
// Check return type
if (ret == 0) {
}
}
}
catch (std::exception &e)
{
std::cout << "Error: " << e.what() << "\n";
}
return 0;
}