-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
255 lines (226 loc) · 6.71 KB
/
test.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// mapnik
#include <mapnik/wkt/wkt_factory.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/util/conversions.hpp>
// geos
#include <geos_c.h>
// boost
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
using namespace boost::chrono;
// stl
#include <exception>
#include <iostream>
#include <fstream>
#include <vector>
static unsigned test_num = 1;
static bool dry_run = false;
static std::set<int> test_set;
typedef process_cpu_clock clock_type;
typedef clock_type::duration dur;
template <typename T>
void benchmark(T & test_runner, std::string const& name)
{
try {
bool should_run_test = true;
if (!test_set.empty())
{
should_run_test = test_set.find(test_num) != test_set.end();
}
if (should_run_test || dry_run)
{
if (!test_runner.validate())
{
std::clog << "test did not validate: " << name << "\n";
//throw std::runtime_error(std::string("test did not validate: ") + name);
}
if (dry_run)
{
std::clog << test_num << ") " << (test_runner.threads_ ? "threaded -> ": "")
<< name << "\n";
}
else
{
process_cpu_clock::time_point start;
dur elapsed;
if (test_runner.threads_ > 0)
{
boost::thread_group tg;
for (unsigned i=0;i<test_runner.threads_;++i)
{
tg.create_thread(test_runner);
//tg.create_thread(boost::bind(&T::operator(),&test_runner));
}
start = process_cpu_clock::now();
tg.join_all();
elapsed = process_cpu_clock::now() - start;
}
else
{
start = process_cpu_clock::now();
test_runner();
elapsed = process_cpu_clock::now() - start;
}
std::clog << test_num << ") " << (test_runner.threads_ ? "threaded -> ": "")
<< name << ": "
<< boost::chrono::duration_cast<milliseconds>(elapsed) << "\n";
}
}
}
catch (std::exception const& ex)
{
std::clog << "test runner did not complete: " << ex.what() << "\n";
}
test_num++;
}
struct test_mapnik
{
std::vector<std::string> & wkt_;
unsigned iter_;
unsigned threads_;
test_mapnik(std::vector<std::string> & wkt,
unsigned iterations,
unsigned threads=0) :
wkt_(wkt),
iter_(iterations),
threads_(threads)
{}
bool validate()
{
return true;
}
void operator()()
{
for (unsigned i=0;i<iter_;++i) {
mapnik::wkt_parser parse_wkt;
BOOST_FOREACH(std::string const& wkt, wkt_)
{
boost::ptr_vector<mapnik::geometry_type> paths;
//if (!mapnik::from_wkt(wkt, paths)) // slow, grammar per parse
if (!parse_wkt.parse(wkt, paths)) // fast, grammar re-use
{
throw std::runtime_error("Failed to parse WKT");
}
}
}
}
};
void geos_notice(const char* format, ...)
{
#ifdef MAPNIK_LOG
char buffer[512];
va_list args;
va_start(args, format);
vsnprintf(buffer, 512, format, args);
va_end(args);
MAPNIK_LOG_WARN(geos) << "geos_datasource: " << buffer;
#endif
}
void geos_error(const char* format, ...)
{
#ifdef MAPNIK_LOG
char buffer[512];
va_list args;
va_start(args, format);
vsnprintf(buffer, 512, format, args);
va_end(args);
MAPNIK_LOG_ERROR(geos) << "geos_datasource: " << buffer;
#endif
}
struct test_geos
{
std::vector<std::string> & wkt_;
unsigned iter_;
unsigned threads_;
test_geos(std::vector<std::string> & wkt,
unsigned iterations,
unsigned threads=0) :
wkt_(wkt),
iter_(iterations),
threads_(threads)
{}
bool validate()
{
return true;
}
void operator()()
{
GEOSWKTReader * reader = GEOSWKTReader_create();
for (unsigned i=0;i<iter_;++i) {
BOOST_FOREACH(std::string const& wkt, wkt_)
{
GEOSGeometry* geometry = GEOSWKTReader_read(reader, wkt.c_str());
if (!geometry)
{
throw std::runtime_error("GEOS Plugin: invalid <wkt> geometry specified");
}
GEOSGeom_destroy(geometry);
}
}
GEOSWKTReader_destroy(reader);
}
};
int main( int argc, char** argv)
{
if (argc > 0) {
for (int i=0;i<argc;++i) {
std::string opt(argv[i]);
if (opt == "-d" || opt == "--dry-run") {
dry_run = true;
} else if (opt[0] != '-') {
int arg;
if (mapnik::util::string2int(opt,arg)) {
test_set.insert(arg);
}
}
}
}
try {
std::string filename("./cases/wkt.csv");
std::ifstream stream(filename.c_str(),std::ios_base::in | std::ios_base::binary);
if (!stream.is_open())
throw std::runtime_error("could not open: '" + filename + "'");
std::vector<std::string> wkt_list;
std::string csv_line;
while(std::getline(stream,csv_line,'\n'))
{
if (csv_line.empty() || csv_line[0] == '#') continue;
std::string dequoted = csv_line.substr(1,csv_line.size()-2);
wkt_list.push_back(dequoted);
}
stream.close();
unsigned iterations = 10000;
unsigned threads = 10;
// mapnik threaded
{
test_mapnik runner(wkt_list,iterations,threads);
benchmark(runner, "mapnik");
}
// geos threaded
{
initGEOS(geos_notice, geos_error);
test_geos runner(wkt_list,iterations,threads);
benchmark(runner, "geos");
}
// mapnik serial, no threads
{
test_mapnik runner(wkt_list,iterations*threads,0);
benchmark(runner, "mapnik");
}
// geos serial, no threads
{
initGEOS(geos_notice, geos_error);
test_geos runner(wkt_list,iterations*threads,0);
benchmark(runner, "geos");
}
}
catch (std::exception const& ex)
{
std::cerr << ex.what() << "\n";
}
return 0;
}