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