1919
2020import java .io .BufferedReader ;
2121import java .io .File ;
22- import java .io .FileInputStream ;
2322import java .io .FileNotFoundException ;
2423import java .io .FileOutputStream ;
2524import java .io .FileReader ;
@@ -46,6 +45,7 @@ public class PerformanceTest {
4645
4746 private final int max = 10 ;
4847
48+ private static final String TEST_RESRC = "org/apache/commons/csv/perf/worldcitiespop.txt.gz" ;
4949 private static final File BIG_FILE = new File (System .getProperty ("java.io.tmpdir" ), "worldcitiespop.txt" );
5050
5151 @ BeforeAll
@@ -54,10 +54,10 @@ public static void setUpClass() throws FileNotFoundException, IOException {
5454 System .out .println (String .format ("Found test fixture %s: %,d bytes." , BIG_FILE , BIG_FILE .length ()));
5555 return ;
5656 }
57- System .out .println ("Decompressing test fixture " + BIG_FILE + "..." );
57+ System .out .println ("Decompressing test fixture to: " + BIG_FILE + "..." );
5858 try (
5959 final InputStream input = new GZIPInputStream (
60- new FileInputStream ( "src/test/resources/perf/worldcitiespop.txt.gz" ));
60+ PerformanceTest . class . getClassLoader (). getResourceAsStream ( TEST_RESRC ));
6161 final OutputStream output = new FileOutputStream (BIG_FILE )) {
6262 IOUtils .copy (input , output );
6363 System .out .println (String .format ("Decompressed test fixture %s: %,d bytes." , BIG_FILE , BIG_FILE .length ()));
@@ -85,24 +85,12 @@ private long parse(final Reader reader, final boolean traverseColumns) throws IO
8585 return recordCount ;
8686 }
8787
88- private void println (final String s ) {
89- System .out .println (s );
90- }
91-
92- private long readAll (final BufferedReader in ) throws IOException {
93- long count = 0 ;
94- while (in .readLine () != null ) {
95- count ++;
96- }
97- return count ;
98- }
99-
100- public long testParseBigFile (final boolean traverseColumns ) throws Exception {
88+ private long testParseBigFile (final boolean traverseColumns ) throws Exception {
10189 final long startMillis = System .currentTimeMillis ();
10290 try (final BufferedReader reader = this .createBufferedReader ()) {
10391 final long count = this .parse (reader , traverseColumns );
10492 final long totalMillis = System .currentTimeMillis () - startMillis ;
105- this .println (
93+ System . out .println (
10694 String .format ("File parsed in %,d milliseconds with Commons CSV: %,d lines." , totalMillis , count ));
10795 return totalMillis ;
10896 }
@@ -114,23 +102,25 @@ public void testParseBigFileRepeat() throws Exception {
114102 for (int i = 0 ; i < this .max ; i ++) {
115103 bestTime = Math .min (this .testParseBigFile (false ), bestTime );
116104 }
117- this .println (String .format ("Best time out of %,d is %,d milliseconds." , this .max , bestTime ));
105+ System . out .println (String .format ("Best time out of %,d is %,d milliseconds." , this .max , bestTime ));
118106 }
119107
120108 @ Test
121109 public void testReadBigFile () throws Exception {
122110 long bestTime = Long .MAX_VALUE ;
123- long count ;
111+ long count = 0L ;
124112 for (int i = 0 ; i < this .max ; i ++) {
125113 final long startMillis ;
126114 try (final BufferedReader in = this .createBufferedReader ()) {
127115 startMillis = System .currentTimeMillis ();
128- count = this .readAll (in );
116+ while (in .readLine () != null ) {
117+ count ++;
118+ }
129119 }
130120 final long totalMillis = System .currentTimeMillis () - startMillis ;
131121 bestTime = Math .min (totalMillis , bestTime );
132- this .println (String .format ("File read in %,d milliseconds: %,d lines." , totalMillis , count ));
122+ System . out .println (String .format ("File read in %,d milliseconds: %,d lines." , totalMillis , count ));
133123 }
134- this .println (String .format ("Best time out of %,d is %,d milliseconds." , this .max , bestTime ));
124+ System . out .println (String .format ("Best time out of %,d is %,d milliseconds." , this .max , bestTime ));
135125 }
136126}
0 commit comments