Ticket #12356: patch-OsmReaderPerformanceTest.patch

File patch-OsmReaderPerformanceTest.patch, 3.1 KB (added by michael2402, 8 years ago)
  • new file test/performance/org/openstreetmap/josm/io/OsmReaderPerformanceTest.java

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