| | 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.File; |
| | 8 | import java.io.FileInputStream; |
| | 9 | import java.io.IOException; |
| | 10 | import java.io.InputStream; |
| | 11 | |
| | 12 | import org.junit.BeforeClass; |
| | 13 | import org.junit.Test; |
| | 14 | import org.openstreetmap.josm.JOSMFixture; |
| | 15 | import org.openstreetmap.josm.PerformanceTestUtils; |
| | 16 | import org.openstreetmap.josm.PerformanceTestUtils.PerformanceTestTimer; |
| | 17 | import org.openstreetmap.josm.data.osm.DataSet; |
| | 18 | |
| | 19 | import 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 | */ |
| | 28 | public 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 | } |