source: josm/trunk/test/performance/org/openstreetmap/josm/io/OsmReaderPerformanceTest.java

Last change on this file was 18853, checked in by taylor.smock, 7 months ago

See #16567: Update to JUnit 5

This removes new JOSMTestRules() with no additional setup and most
JOSMFixture calls.

Removing the bare JOSMTestRules speeds up the test suite since there are two
fewer System.gc() calls per test.

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