-
Notifications
You must be signed in to change notification settings - Fork 1
/
geos_perf.c
325 lines (282 loc) · 7 KB
/
geos_perf.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "geos_perf.h"
/************************************************************************
* REGISTER NEW TESTS HERE
*
* To register a new test module, add the config function
* signature here.
*/
gp_test config_buffer_watersheds(void);
gp_test config_buffer_australia(void);
gp_test config_intersection(void);
gp_test config_point_in_polygon(void);
gp_test config_tree_points(void);
gp_test config_tree_points_nn(void);
gp_test config_union_watersheds(void);
gp_test config_isvalid_australia(void);
gp_test config_valid_watersheds(void);
gp_test config_isvalid_landcover(void);
gp_test config_delaunay(void);
/*
* And then add the function name here
*/
static gp_config_func gp_config_funcs[] =
{
config_buffer_watersheds,
config_buffer_australia,
config_intersection,
config_union_watersheds,
config_tree_points,
config_tree_points_nn,
config_point_in_polygon,
config_isvalid_australia,
config_valid_watersheds,
config_isvalid_landcover,
config_delaunay,
NULL
};
/************************************************************************
* Utilities for a geometry list.
*/
void
geomlist_init(GEOSGeometryList* gl)
{
static size_t capacity = 16;
gl->capacity = capacity;
gl->geoms = malloc(sizeof(GEOSGeometry*) * gl->capacity);
gl->ngeoms = 0;
return;
}
void
geomlist_free(GEOSGeometryList* gl)
{
size_t i;
for (i = 0; i < gl->ngeoms; i++)
{
if (gl->geoms[i])
GEOSGeom_destroy(gl->geoms[i]);
}
free(gl->geoms);
gl->geoms = NULL;
return;
}
void
geomlist_release(GEOSGeometryList* gl)
{
free(gl->geoms);
gl->geoms = NULL;
return;
}
size_t
geomlist_push(GEOSGeometryList* gl, GEOSGeometry *g)
{
if (gl->ngeoms >= gl->capacity)
{
gl->capacity *= 2;
gl->geoms = realloc(gl->geoms, sizeof(GEOSGeometry*) * gl->capacity);
}
gl->geoms[gl->ngeoms++] = g;
return gl->ngeoms;
}
GEOSGeometry*
geomlist_pop(GEOSGeometryList* gl)
{
if (gl->ngeoms == 0)
return NULL;
GEOSGeometry* g = gl->geoms[gl->ngeoms-1];
gl->geoms[gl->ngeoms-1] = NULL;
gl->ngeoms--;
return g;
}
size_t
geomlist_size(GEOSGeometryList* gl)
{
return gl->ngeoms;
}
const GEOSGeometry*
geomlist_get(GEOSGeometryList* gl, size_t i)
{
if (i > gl->ngeoms-1)
return NULL;
return gl->geoms[i];
}
void
geomlist_print(GEOSGeometryList* gl)
{
GEOSWKTWriter *writer = GEOSWKTWriter_create();
size_t size = geomlist_size(gl);
size_t i;
for (i = 0; i < size; i++)
{
const GEOSGeometry *g = geomlist_get(gl, i);
if (g)
{
char *wkt = GEOSWKTWriter_write(writer, g);
printf("%s\n", wkt);
GEOSFree(wkt);
}
}
GEOSWKTWriter_destroy(writer);
}
/************************************************************************
* Utility functions to polyfill old GEOS versions
*/
/* GEOS < 3.7 does not have GEOSGeom_createPointFromXY */
GEOSGeometry *
createPointFromXY(double x, double y)
{
GEOSCoordSequence* cs = GEOSCoordSeq_create(
1, /* size */
2); /* dims */
GEOSCoordSeq_setX(cs, 0, x);
GEOSCoordSeq_setY(cs, 0, y);
return GEOSGeom_createPoint(cs);
}
/************************************************************************
* Utility functions for the testing process.
*/
static struct timeval
time_now()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t;
}
static double
time_difference(struct timeval start, struct timeval end)
{
int64_t diff_us = (int64_t)(end.tv_usec - start.tv_usec);
if (start.tv_sec == end.tv_sec)
{
return (double)diff_us / 1000000.0;
}
else
{
int64_t diff_s = (int64_t)(end.tv_sec - start.tv_sec);
return (double)(diff_s) + (double)(diff_us) / 1000000.0;;
}
}
static void
geos_log_stderr(const char* fmt, ...)
{
va_list ap;
if (debug_level < 1)
return;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static void
log_stderr(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void
debug_stderr(uint32_t level, const char* fmt, ...)
{
va_list ap;
if (debug_level < level)
return;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static void
result_to_csv(const gp_result* result)
{
fprintf(stdout, "%s,%s,%u,%0.5g,%0.5g,%0.5g\n",
result->version,
result->name,
result->count,
result->setup_time,
result->run_time,
result->cleanup_time);
}
static gp_result
run_test(const gp_test* test)
{
size_t i;
gp_result result;
double run_time = 0.0,
setup_time = 0.0,
cleanup_time = 0.0;
struct timeval start, end;
/* Prepare to run tests */
log_stderr("SETUP [%s] ...", test->name);
start = time_now();
if (test->func_setup)
test->func_setup();
end = time_now();
setup_time = time_difference(start, end);
log_stderr(" %0.3gs\n", setup_time);
/* Run the tests and time them */
log_stderr(" RUN [%s] ...", test->name);
for (i = 0; i < test->count; i++)
{
start = time_now();
if (test->func_run)
test->func_run();
end = time_now();
run_time += time_difference(start, end);
}
log_stderr(" %0.3gs\n", run_time);
/* Clean up after the tests */
log_stderr("CLEAN [%s] ...", test->name);
start = time_now();
if (test->func_cleanup)
test->func_cleanup();
end = time_now();
cleanup_time = time_difference(start, end);
log_stderr(" %0.3gs\n", cleanup_time);
/* Sumarize the results */
result.version = GEOSversion();
result.count = test->count;
result.setup_time = setup_time;
result.run_time = run_time;
result.cleanup_time = cleanup_time;
result.name = test->name;
return result;
}
/************************************************************************
* Main testing loop
*/
uint32_t debug_level = 0;
int
main(int argc, char *argv[])
{
initGEOS(geos_log_stderr, geos_log_stderr);
log_stderr("VERSION [GEOS %s]\n", GEOSversion());
gp_config_func* config_func;
for (config_func = gp_config_funcs; *config_func != NULL; config_func++)
{
gp_test test = (*config_func)();
// If command-line arguments are provided, interpret them to be the
// set of tests we should run and skip tests not included in the list.
if (argc > 1) {
int found = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(test.name, argv[i]) == 0) {
found = 1;
}
}
if (!found) {
continue;
}
}
if (test.count < 1)
continue;
gp_result result = run_test(&test);
result_to_csv(&result);
}
finishGEOS();
return 0;
}