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

Last change on this file since 9771 was 9491, checked in by Don-vip, 8 years ago

see #12356 - remove use of internal proprietary API (patch by michael2402)

File size: 3.0 KB
Line 
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.ByteArrayOutputStream;
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.IOException;
11import java.io.InputStream;
12
13import org.junit.BeforeClass;
14import org.junit.Rule;
15import org.junit.Test;
16import org.junit.rules.Timeout;
17import org.openstreetmap.josm.JOSMFixture;
18import org.openstreetmap.josm.PerformanceTestUtils;
19import org.openstreetmap.josm.PerformanceTestUtils.PerformanceTestTimer;
20import org.openstreetmap.josm.data.osm.DataSet;
21
22/**
23 * This test tests how fast we are at reading an OSM file.
24 * <p>
25 * For this, we use the neubrandenburg-file, which is a good real world example of an OSM file. We ignore disk access times.
26 *
27 * @author Michael Zangl
28 */
29public class OsmReaderPerformanceTest {
30 private static final int TIMES = 4;
31 private static String DATA_FILE = "data_nodist/neubrandenburg.osm.bz2";
32
33 /**
34 * Global timeout applied to all test methods.
35 */
36 @Rule
37 public Timeout globalTimeout = Timeout.seconds(15*60);
38
39 /**
40 * Prepare the test.
41 */
42 @BeforeClass
43 public static void createJOSMFixture() {
44 JOSMFixture.createPerformanceTestFixture().init(true);
45 }
46
47 /**
48 * Simulates a plain read of a .osm.bz2 file (from memory)
49 * @throws Exception if an error occurs
50 */
51 @Test
52 public void testCompressed() throws Exception {
53 runTest("compressed (.osm.bz2)", false);
54 }
55
56 /**
57 * Simulates a plain read of a .osm file (from memory)
58 * @throws Exception if an error occurs
59 */
60 @Test
61 public void test() throws Exception {
62 runTest(".osm-file", true);
63 }
64
65 private void runTest(String what, boolean decompressBeforeRead) throws IllegalDataException, IOException {
66 InputStream is = loadFile(decompressBeforeRead);
67 PerformanceTestTimer timer = PerformanceTestUtils.startTimer("load " + what + " " + TIMES + " times");
68 DataSet ds = null;
69 for (int i = 0; i < TIMES; i++) {
70 is.reset();
71
72 ds = OsmReader.parseDataSet(decompressBeforeRead ? is : Compression.byExtension(DATA_FILE)
73 .getUncompressedInputStream(is), null);
74 }
75 timer.done();
76 assertNotNull(ds);
77 }
78
79 private InputStream loadFile(boolean decompressBeforeRead) throws IOException {
80 File file = new File(DATA_FILE);
81 try (InputStream is = decompressBeforeRead ? Compression.getUncompressedFileInputStream(file) : new FileInputStream(file)) {
82 ByteArrayOutputStream temporary = new ByteArrayOutputStream();
83 byte[] readBuffer = new byte[4096];
84 int readBytes = 0;
85 while (readBytes != -1) {
86 temporary.write(readBuffer, 0, readBytes);
87 readBytes = is.read(readBuffer);
88 }
89 return new ByteArrayInputStream(temporary.toByteArray());
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.