Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5537559

Browse files
drinckeszongweil
authored andcommitted
Add benchmarks to Java (google#346)
* benchmarking * formatting
1 parent 5d44700 commit 5537559

File tree

3 files changed

+99
-12
lines changed

3 files changed

+99
-12
lines changed

java/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ java_library(
55
],
66
)
77

8+
java_test(
9+
name = "BenchmarkTest",
10+
size = "small",
11+
srcs = [
12+
"src/test/java/com/google/openlocationcode/BenchmarkTest.java",
13+
],
14+
test_class = "com.google.openlocationcode.BenchmarkTest",
15+
deps = [
16+
":openlocationcode",
17+
],
18+
)
19+
820
java_test(
921
name = "DecodingTest",
1022
size = "small",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.google.openlocationcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.JUnit4;
10+
11+
/** Benchmark the encode and decode methods. */
12+
@RunWith(JUnit4.class)
13+
public class BenchmarkTest {
14+
15+
public static final int LOOPS = 1000000;
16+
17+
public static Random generator = new Random();
18+
19+
private static class TestData {
20+
21+
private final double latitude;
22+
private final double longitude;
23+
private final int length;
24+
private final String code;
25+
26+
public TestData() {
27+
this.latitude = generator.nextDouble() * 180 - 90;
28+
this.longitude = generator.nextDouble() * 360 - 180;
29+
int length = generator.nextInt(11) + 4;
30+
if (length < 10 && length % 2 == 1) {
31+
length += 1;
32+
}
33+
this.length = length;
34+
this.code = OpenLocationCode.encode(this.latitude, this.longitude, this.length);
35+
}
36+
}
37+
38+
private final List<TestData> testDataList = new ArrayList<>();
39+
40+
@Before
41+
public void setUp() throws Exception {
42+
testDataList.clear();
43+
for (int i = 0; i < LOOPS; i++) {
44+
testDataList.add(new TestData());
45+
}
46+
}
47+
48+
@Test
49+
public void benchmarkEncode() {
50+
long start = System.nanoTime();
51+
for (TestData testData : testDataList) {
52+
OpenLocationCode.encode(testData.latitude, testData.longitude, testData.length);
53+
}
54+
long microsecs = (System.nanoTime() - start) / 1000;
55+
56+
System.out.printf(
57+
"Encode %d loops in %d usecs, %.3f usec per call\n",
58+
LOOPS, microsecs, (double) microsecs / LOOPS);
59+
}
60+
61+
@Test
62+
public void benchmarkDecode() {
63+
long start = System.nanoTime();
64+
for (TestData testData : testDataList) {
65+
OpenLocationCode.decode(testData.code);
66+
}
67+
long microsecs = (System.nanoTime() - start) / 1000;
68+
69+
System.out.printf(
70+
"Decode %d loops in %d usecs, %.3f usec per call\n",
71+
LOOPS, microsecs, (double) microsecs / LOOPS);
72+
}
73+
}

java/src/test/java/com/google/openlocationcode/PrecisionTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@
88
/** Tests size of rectangles defined by open location codes of various size. */
99
@RunWith(JUnit4.class)
1010
public class PrecisionTest {
11+
12+
private static final double epsilon = 1e-10;
1113

1214
@Test
1315
public void testWidthInDegrees() {
14-
Assert.assertEquals(new OpenLocationCode("67000000+").decode().getLongitudeWidth(), 20., 0);
15-
Assert.assertEquals(new OpenLocationCode("67890000+").decode().getLongitudeWidth(), 1., 0);
16-
Assert.assertEquals(new OpenLocationCode("6789CF00+").decode().getLongitudeWidth(), 0.05, 0);
17-
Assert.assertEquals(new OpenLocationCode("6789CFGH+").decode().getLongitudeWidth(), 0.0025, 0);
16+
Assert.assertEquals(new OpenLocationCode("67000000+").decode().getLongitudeWidth(), 20., epsilon);
17+
Assert.assertEquals(new OpenLocationCode("67890000+").decode().getLongitudeWidth(), 1., epsilon);
18+
Assert.assertEquals(new OpenLocationCode("6789CF00+").decode().getLongitudeWidth(), 0.05, epsilon);
19+
Assert.assertEquals(new OpenLocationCode("6789CFGH+").decode().getLongitudeWidth(), 0.0025, epsilon);
1820
Assert.assertEquals(
19-
new OpenLocationCode("6789CFGH+JM").decode().getLongitudeWidth(), 0.000125, 0);
21+
new OpenLocationCode("6789CFGH+JM").decode().getLongitudeWidth(), 0.000125, epsilon);
2022
Assert.assertEquals(
21-
new OpenLocationCode("6789CFGH+JMP").decode().getLongitudeWidth(), 0.00003125, 0);
23+
new OpenLocationCode("6789CFGH+JMP").decode().getLongitudeWidth(), 0.00003125, epsilon);
2224
}
2325

2426
@Test
2527
public void testHeightInDegrees() {
26-
Assert.assertEquals(new OpenLocationCode("67000000+").decode().getLatitudeHeight(), 20., 0);
27-
Assert.assertEquals(new OpenLocationCode("67890000+").decode().getLatitudeHeight(), 1., 0);
28-
Assert.assertEquals(new OpenLocationCode("6789CF00+").decode().getLatitudeHeight(), 0.05, 0);
29-
Assert.assertEquals(new OpenLocationCode("6789CFGH+").decode().getLatitudeHeight(), 0.0025, 0);
28+
Assert.assertEquals(new OpenLocationCode("67000000+").decode().getLatitudeHeight(), 20., epsilon);
29+
Assert.assertEquals(new OpenLocationCode("67890000+").decode().getLatitudeHeight(), 1., epsilon);
30+
Assert.assertEquals(new OpenLocationCode("6789CF00+").decode().getLatitudeHeight(), 0.05, epsilon);
31+
Assert.assertEquals(new OpenLocationCode("6789CFGH+").decode().getLatitudeHeight(), 0.0025, epsilon);
3032
Assert.assertEquals(
31-
new OpenLocationCode("6789CFGH+JM").decode().getLatitudeHeight(), 0.000125, 0);
33+
new OpenLocationCode("6789CFGH+JM").decode().getLatitudeHeight(), 0.000125, epsilon);
3234
Assert.assertEquals(
33-
new OpenLocationCode("6789CFGH+JMP").decode().getLatitudeHeight(), 0.000025, 0);
35+
new OpenLocationCode("6789CFGH+JMP").decode().getLatitudeHeight(), 0.000025, epsilon);
3436
}
3537
}

0 commit comments

Comments
 (0)