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