/
PerformanceTest.java
161 lines (137 loc) · 5.86 KB
/
PerformanceTest.java
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
// PerformanceTest.java
// Java Spatial Index Library
// Copyright (C) 2002-2005 Infomatiq Limited.
// Copyright (C) 2013 Aled Morris <aled@users.sourceforge.net>
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.infomatiq.jsi;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.infomatiq.jsi.rtree.RTree;
import gnu.trove.procedure.TIntProcedure;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* PerformanceTest
*
* Generates results used for comparing the performance of the Java Spatial
* Index library against alternative implementations.
*
* The idea is for the raw data to be imported into a database, and results
* extracted from that.
*
* This test requires 1024M memory (i.e. use -Xmx1024M)
*/
public class PerformanceTest {
private static final Logger log = LoggerFactory.getLogger(PerformanceTest.class);
private SpatialIndex si;
private float randomFloat(Random r, float min, float max) {
return (r.nextFloat() * (max - min)) + min;
}
protected Point randomPoint(Random r) {
return new Point(randomFloat(r, 0, 100), randomFloat(r, 0, 100));
}
private Rectangle randomRectangle(Random r, float size) {
float x = randomFloat(r, 0, 100);
float y = randomFloat(r, 0, 100);
return new Rectangle(x, y, x + randomFloat(r, 0, size), y + randomFloat(r, 0, size));
}
abstract class Operation {
private final int count[] = new int[1];
private String description;
public Random r;
public Operation(String description, Random r) {
this.description = description;
this.r = r;
}
protected TIntProcedure countProc = new TIntProcedure() {
public boolean execute(int value) {
count[0]++;
return true;
}
};
public int callbackCount() {
return count[0];
}
public String getDescription() {
return description;
}
abstract void execute(SpatialIndex si, Random r);
}
private void benchmark(Operation o, int repetitions) {
long duration = 0;
long startTime = System.nanoTime();
for (int j = 0; j < repetitions; j++) o.execute(si, o.r);
duration += (System.nanoTime() - startTime);
log.info(o.getDescription() + ", " +
"avg callbacks = " + ((float) o.callbackCount() / repetitions) + ", " +
"avg time = " + (duration / repetitions) + " ns");
}
/**
* First attempt at a benchmark
*/
@Test
public void benchmark_1() {
Random rand = new Random(0);
Properties p = new Properties();
p.setProperty("MinNodeEntries", "20");
p.setProperty("MaxNodeEntries", "50");
si = new RTree();
si.init(p);
final int rectangleCount = 1000000;
final Rectangle[] rects = new Rectangle[rectangleCount];
for (int i = 0; i < rectangleCount; i++) {
rects[i] = randomRectangle(rand, 0.01f);
}
long duration;
long startTime;
for (int j = 0; j < 5; j++) {
duration = 0;
startTime = System.nanoTime();
for (int i = 0; i < rectangleCount; i++) {
si.add(rects[i], i);
}
duration += (System.nanoTime() - startTime);
log.info("add " + rectangleCount + " avg tme = " + (duration / rectangleCount) + " ns");
if (j == 4) break; // don't do the delete on the last iteration
duration = 0;
startTime = System.nanoTime();
for (int i = 0; i < rectangleCount; i++) {
si.delete(rects[i], i);
}
duration += (System.nanoTime() - startTime);
log.info("delete " + rectangleCount + " avg tme = " + (duration / rectangleCount) + " ns");
}
ExecutorService exec = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
try {
for (int i = 0; i < 100; i++) {
exec.submit(new Runnable() {public void run() {benchmark(new Operation("nearest", new Random(0)) {void execute(SpatialIndex si, Random r) {si.nearest(randomPoint(r), countProc, 0.1f);}}, 100); }});
exec.submit(new Runnable() {public void run() {benchmark(new Operation("nearestNUnsorted", new Random(0)) {void execute(SpatialIndex si, Random r) {si.nearestNUnsorted(randomPoint(r), countProc, 10, 0.16f);}}, 100); }});
exec.submit(new Runnable() {public void run() {benchmark(new Operation("nearestN", new Random(0)) {void execute(SpatialIndex si, Random r) {si.nearestN(randomPoint(r), countProc, 10, 0.16f);}}, 100); }});
exec.submit(new Runnable() {public void run() {benchmark(new Operation("intersects", new Random(0)) {void execute(SpatialIndex si, Random r) {si.intersects(randomRectangle(r, 0.6f), countProc);}}, 100); }});
exec.submit(new Runnable() {public void run() {benchmark(new Operation("contains", new Random(0)) {void execute(SpatialIndex si, Random r) {si.contains(randomRectangle(r, 0.65f), countProc);}}, 100); }});
}
try { exec.awaitTermination(1, TimeUnit.DAYS); } catch (Exception e) {}
}
finally {
exec.shutdown();
}
}
}