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

Last change on this file since 18690 was 18690, checked in by taylor.smock, 13 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

File size: 2.9 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.FileInputStream;
10import java.io.IOException;
11import java.io.InputStream;
12import java.nio.file.Files;
13import java.util.concurrent.TimeUnit;
14
15import org.junit.jupiter.api.BeforeAll;
16import org.junit.jupiter.api.Test;
17import org.junit.jupiter.api.Timeout;
18import org.openstreetmap.josm.JOSMFixture;
19import org.openstreetmap.josm.PerformanceTestUtils;
20import org.openstreetmap.josm.PerformanceTestUtils.PerformanceTestTimer;
21import 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)
31class 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}
Note: See TracBrowser for help on using the repository browser.