1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 |
|
---|
6 | import java.io.ByteArrayInputStream;
|
---|
7 | import java.io.ByteArrayOutputStream;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 |
|
---|
13 | import org.junit.BeforeClass;
|
---|
14 | import org.junit.Rule;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.junit.rules.Timeout;
|
---|
17 | import org.openstreetmap.josm.JOSMFixture;
|
---|
18 | import org.openstreetmap.josm.PerformanceTestUtils;
|
---|
19 | import org.openstreetmap.josm.PerformanceTestUtils.PerformanceTestTimer;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This test tests how fast we are at reading an OSM file.
|
---|
24 | * <p>
|
---|
25 | * For this, we use the neubrandenburg-file, which is a good real world example of an OSM file. We ignore disk access times.
|
---|
26 | *
|
---|
27 | * @author Michael Zangl
|
---|
28 | */
|
---|
29 | public class OsmReaderPerformanceTest {
|
---|
30 | private static final int TIMES = 4;
|
---|
31 | private static String DATA_FILE = "data_nodist/neubrandenburg.osm.bz2";
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Global timeout applied to all test methods.
|
---|
35 | */
|
---|
36 | @Rule
|
---|
37 | public Timeout globalTimeout = Timeout.seconds(15*60);
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Prepare the test.
|
---|
41 | */
|
---|
42 | @BeforeClass
|
---|
43 | public static void createJOSMFixture() {
|
---|
44 | JOSMFixture.createPerformanceTestFixture().init(true);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Simulates a plain read of a .osm.bz2 file (from memory)
|
---|
49 | * @throws Exception if an error occurs
|
---|
50 | */
|
---|
51 | @Test
|
---|
52 | public void testCompressed() throws Exception {
|
---|
53 | runTest("compressed (.osm.bz2)", false);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Simulates a plain read of a .osm file (from memory)
|
---|
58 | * @throws Exception if an error occurs
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void test() throws Exception {
|
---|
62 | runTest(".osm-file", true);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void runTest(String what, boolean decompressBeforeRead) throws IllegalDataException, IOException {
|
---|
66 | InputStream is = loadFile(decompressBeforeRead);
|
---|
67 | PerformanceTestTimer timer = PerformanceTestUtils.startTimer("load " + what + " " + TIMES + " times");
|
---|
68 | DataSet ds = null;
|
---|
69 | for (int i = 0; i < TIMES; i++) {
|
---|
70 | is.reset();
|
---|
71 |
|
---|
72 | ds = OsmReader.parseDataSet(decompressBeforeRead ? is : Compression.byExtension(DATA_FILE)
|
---|
73 | .getUncompressedInputStream(is), null);
|
---|
74 | }
|
---|
75 | timer.done();
|
---|
76 | assertNotNull(ds);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private InputStream loadFile(boolean decompressBeforeRead) throws IOException {
|
---|
80 | File file = new File(DATA_FILE);
|
---|
81 | try (InputStream is = decompressBeforeRead ? Compression.getUncompressedFileInputStream(file) : new FileInputStream(file)) {
|
---|
82 | ByteArrayOutputStream temporary = new ByteArrayOutputStream();
|
---|
83 | byte[] readBuffer = new byte[4096];
|
---|
84 | int readBytes = 0;
|
---|
85 | while (readBytes != -1) {
|
---|
86 | temporary.write(readBuffer, 0, readBytes);
|
---|
87 | readBytes = is.read(readBuffer);
|
---|
88 | }
|
---|
89 | return new ByteArrayInputStream(temporary.toByteArray());
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|