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

Last change on this file since 16006 was 16006, checked in by Don-vip, 4 years ago

see #18140 - reorganization of data(_nodist), images(_nodist), styles(_nodist), IDE and native files in a more practical file tree.

  • Everything belonging to the jar is now in resources (data, images, styles)
  • Everything not belonging to the jar is now in nodist (data, images, styles)
  • Everything related to OS native functions is now in native (linux, macosx, windows)
  • Everything related to an IDE is now in ide (eclipse, netbeans)
File size: 3.1 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
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * This test tests how fast we are at reading an OSM file.
26 * <p>
27 * For this, we use the neubrandenburg-file, which is a good real world example of an OSM file. We ignore disk access times.
28 *
29 * @author Michael Zangl
30 */
31public class OsmReaderPerformanceTest {
32 private static final int TIMES = 4;
33 private static String DATA_FILE = "nodist/data/neubrandenburg.osm.bz2";
34
35 /**
36 * Global timeout applied to all test methods.
37 */
38 @Rule
39 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
40 public Timeout globalTimeout = Timeout.seconds(15*60);
41
42 /**
43 * Prepare the test.
44 */
45 @BeforeClass
46 public static void createJOSMFixture() {
47 JOSMFixture.createPerformanceTestFixture().init(true);
48 }
49
50 /**
51 * Simulates a plain read of a .osm.bz2 file (from memory)
52 * @throws Exception if an error occurs
53 */
54 @Test
55 public void testCompressed() throws Exception {
56 runTest("compressed (.osm.bz2)", false);
57 }
58
59 /**
60 * Simulates a plain read of a .osm file (from memory)
61 * @throws Exception if an error occurs
62 */
63 @Test
64 public void testPlain() throws Exception {
65 runTest(".osm-file", true);
66 }
67
68 private void runTest(String what, boolean decompressBeforeRead) throws IllegalDataException, IOException {
69 InputStream is = loadFile(decompressBeforeRead);
70 PerformanceTestTimer timer = PerformanceTestUtils.startTimer("load " + what + " " + TIMES + " times");
71 DataSet ds = null;
72 for (int i = 0; i < TIMES; i++) {
73 is.reset();
74
75 ds = OsmReader.parseDataSet(decompressBeforeRead ? is : Compression.byExtension(DATA_FILE)
76 .getUncompressedInputStream(is), null);
77 }
78 timer.done();
79 assertNotNull(ds);
80 }
81
82 private InputStream loadFile(boolean decompressBeforeRead) throws IOException {
83 File file = new File(DATA_FILE);
84 try (InputStream is = decompressBeforeRead ? Compression.getUncompressedFileInputStream(file) : new FileInputStream(file)) {
85 ByteArrayOutputStream temporary = new ByteArrayOutputStream();
86 byte[] readBuffer = new byte[4096];
87 int readBytes = 0;
88 while (readBytes != -1) {
89 temporary.write(readBuffer, 0, readBytes);
90 readBytes = is.read(readBuffer);
91 }
92 return new ByteArrayInputStream(temporary.toByteArray());
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.