-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
156 lines (139 loc) · 5.6 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* -*- mode: C++; c-file-style: "Google"; c-basic-offset: 4 -*- */
#include <signal.h>
#include <sysexits.h>
#include <unistd.h>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <type_traits>
#include <vector>
#include "mongo/client/dbclient.h"
#include "collection.h"
#include "cortisol.h"
#include "options.h"
#include "output.h"
#include "thread.h"
#include "timing.h"
using std::cerr;
using std::endl;
namespace cortisol {
using std::cout;
using std::pair;
using std::string;
using std::unique_ptr;
using std::vector;
thread_interrupter interrupter;
void int_handler(int sig) {
interrupter.interrupt();
signal(sig, SIG_DFL);
}
static string collname(size_t i) {
stringstream ss;
ss << "stress_test" << i << ".coll";
return ss.str();
}
void run(const Options &opts) {
interrupter.check_for_interrupt();
{
vector<Collection> colls;
size_t i = 0;
std::generate_n(std::back_inserter(colls), Collection::collections,
[opts, &i]() {
return std::move(Collection(opts, collname(i++)));
});
if (opts.create) {
if (!opts.keep_database) {
std::for_each(colls.begin(), colls.end(), std::mem_fn(&Collection::drop));
}
interrupter.check_for_interrupt();
vector<std::thread> threads;
std::transform(colls.begin(), colls.end(), std::back_inserter(threads),
[opts](Collection &coll) {
return std::move(std::thread(std::mem_fn(&Collection::fill), &coll));
});
std::for_each(threads.begin(), threads.end(), std::mem_fun_ref(&std::thread::join));
interrupter.check_for_interrupt();
}
}
interrupter.check_for_interrupt();
if (opts.stress) {
timestamp_t t0 = now();
{
vector<unique_ptr<CollectionRunner> > runners;
for (size_t i = 0; i < Collection::collections; ++i) {
string coll = collname(i);
size_t id = 0;
std::generate_n(std::back_inserter(runners), UpdateRunner::threads,
[opts, coll, &id, t0]() -> unique_ptr<CollectionRunner> {
return unique_ptr<CollectionRunner>(new UpdateRunner(opts, coll, id++, t0));
});
id = 0;
std::generate_n(std::back_inserter(runners), PointQueryRunner::threads,
[opts, coll, &id, t0]() -> unique_ptr<CollectionRunner> {
return unique_ptr<CollectionRunner>(new PointQueryRunner(opts, coll, id++, t0));
});
id = 0;
std::generate_n(std::back_inserter(runners), RangeQueryRunner::threads,
[opts, coll, &id, t0]() -> unique_ptr<CollectionRunner> {
return unique_ptr<CollectionRunner>(new RangeQueryRunner(opts, coll, id++, t0));
});
}
vector<std::thread> threads;
std::transform(runners.begin(), runners.end(), std::back_inserter(threads),
[](const unique_ptr<CollectionRunner> &runner) {
return std::move(std::thread(std::ref(*runner)));
});
try {
double elapsed = 0.0;
for (int i = 0; ; interrupter.check_for_interrupt(), ++i) {
usleep(std::min((opts.seconds - elapsed), out::output_period) * 1000000);
timestamp_t ti = now();
elapsed = ts_to_secs(ti - t0);
if (elapsed >= opts.seconds) {
break;
}
if ((i * threads.size()) % out::header_period == 0 ||
(i == 0 && out::header_period >= 0)) {
CollectionRunner::header(cout);
}
std::for_each(runners.begin(), runners.end(),
[ti](const unique_ptr<CollectionRunner> &runner) {
runner->report(cout, ti);
});
}
} catch (interrupt_exception) {
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
throw;
}
std::for_each(runners.begin(), runners.end(), [](const unique_ptr<CollectionRunner> &runner) { runner->stop(); });
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
timestamp_t t1 = now();
cout << endl << "# TOTALS:" << endl;
std::for_each(runners.begin(), runners.end(),
[t1](const unique_ptr<CollectionRunner> &runner) {
runner->total(cout, t1);
});
}
}
}
} // namespace cortisol
int main(int argc, const char *argv[]) {
cortisol::Options opts = cortisol::Options::default_options();
bool ok = cortisol::parse_cmdline(argc, argv, opts);
if (!ok) {
return EX_USAGE;
}
signal(SIGINT, cortisol::int_handler);
try {
cortisol::run(opts);
} catch (cortisol::interrupt_exception) {
// ok
} catch (const mongo::DBException &e) {
cerr << "caught " << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}