1818package org .apache .commons .csv .perf ;
1919
2020import java .io .BufferedReader ;
21+ import java .io .File ;
22+ import java .io .FileInputStream ;
23+ import java .io .FileNotFoundException ;
24+ import java .io .FileOutputStream ;
2125import java .io .FileReader ;
2226import java .io .IOException ;
27+ import java .io .InputStream ;
28+ import java .io .OutputStream ;
2329import java .io .Reader ;
30+ import java .util .zip .GZIPInputStream ;
2431
2532import org .apache .commons .csv .CSVFormat ;
33+ import org .apache .commons .io .IOUtils ;
34+ import org .junit .BeforeClass ;
2635import org .junit .Ignore ;
2736import org .junit .Test ;
2837
2938/**
3039 * Tests performance.
31- *
40+ *
3241 * Only enable for your own development.
3342 */
43+ @ Ignore
3444public class PerformanceTest {
3545
3646 private final int max = 10 ;
3747
48+ private static final File BIG_FILE = new File (System .getProperty ("java.io.tmpdir" ), "worldcitiespop.txt" );
49+
50+ @ BeforeClass
51+ public static void setUpClass () throws FileNotFoundException , IOException {
52+ if (BIG_FILE .exists ()) {
53+ System .out .println (String .format ("Found test fixture %s: %,d bytes." , BIG_FILE , BIG_FILE .length ()));
54+ return ;
55+ }
56+ System .out .println ("Decompressing test fixture " + BIG_FILE + "..." );
57+ final InputStream input = new GZIPInputStream (new FileInputStream ("src/test/resources/perf/worldcitiespop.txt.gz" ));
58+ final OutputStream output = new FileOutputStream (BIG_FILE );
59+ IOUtils .copy (input , output );
60+ System .out .println (String .format ("Decompressed test fixture %s: %,d bytes." , BIG_FILE , BIG_FILE .length ()));
61+ }
62+
3863 private BufferedReader getBufferedReader () throws IOException {
39- return new BufferedReader (new FileReader ("src/test/resources/worldcitiespop.txt" ));
64+ return new BufferedReader (new FileReader (BIG_FILE ));
4065 }
4166
42- private long parse (Reader in ) throws IOException {
43- CSVFormat format = CSVFormat .DEFAULT .withIgnoreSurroundingSpaces (false );
67+ private long parse (final Reader in ) throws IOException {
68+ final CSVFormat format = CSVFormat .DEFAULT .withIgnoreSurroundingSpaces (false );
4469 long count = 0 ;
45- for (Object record : format .parse (in )) {
70+ for (final Object record : format .parse (in )) {
4671 count ++;
4772 }
4873 return count ;
@@ -52,47 +77,51 @@ private void println() {
5277 System .out .println ();
5378 }
5479
55- private void println (String s ) {
80+ private void println (final String s ) {
5681 System .out .println (s );
5782 }
5883
59- private long readAll (BufferedReader in ) throws IOException {
84+ private long readAll (final BufferedReader in ) throws IOException {
6085 long count = 0 ;
6186 while (in .readLine () != null ) {
6287 count ++;
6388 }
6489 return count ;
6590 }
6691
67- @ Test
68- @ Ignore
69- public void testParseBigFile () throws Exception {
70- long t0 = System .currentTimeMillis ();
71- long count = this .parse (this .getBufferedReader ());
72- this .println ("File parsed in " + (System .currentTimeMillis () - t0 ) + "ms with Commons CSV" + " " + count
73- + " lines" );
74- this .println ();
92+ public long testParseBigFile () throws Exception {
93+ final long startMillis = System .currentTimeMillis ();
94+ final long count = this .parse (this .getBufferedReader ());
95+ final long totalMillis = System .currentTimeMillis () - startMillis ;
96+ this .println (String .format ("File parsed in %,d milliseconds with Commons CSV: %,d lines." , totalMillis , count ));
97+ return totalMillis ;
7598 }
7699
77100 @ Test
78- @ Ignore
79101 public void testParseBigFileRepeat () throws Exception {
102+ long bestTime = Long .MAX_VALUE ;
80103 for (int i = 0 ; i < this .max ; i ++) {
81- this .testParseBigFile ();
104+ bestTime = Math . min ( this .testParseBigFile (), bestTime );
82105 }
83- this .println ();
106+ this .println (String . format ( "Best time out of %,d is %,d milliseconds." , this . max , bestTime ) );
84107 }
85108
86109 @ Test
87- @ Ignore
88110 public void testReadBigFile () throws Exception {
111+ long bestTime = Long .MAX_VALUE ;
89112 for (int i = 0 ; i < this .max ; i ++) {
90- BufferedReader in = this .getBufferedReader ();
91- long t0 = System .currentTimeMillis ();
92- long count = this .readAll (in );
93- in .close ();
94- this .println ("File read in " + (System .currentTimeMillis () - t0 ) + "ms" + " " + count + " lines" );
113+ final BufferedReader in = this .getBufferedReader ();
114+ final long startMillis = System .currentTimeMillis ();
115+ long count = 0 ;
116+ try {
117+ count = this .readAll (in );
118+ } finally {
119+ in .close ();
120+ }
121+ final long totalMillis = System .currentTimeMillis () - startMillis ;
122+ bestTime = Math .min (totalMillis , bestTime );
123+ this .println (String .format ("File read in %,d milliseconds: %,d lines." , totalMillis , count ));
95124 }
96- this .println ();
125+ this .println (String . format ( "Best time out of %,d is %,d milliseconds." , this . max , bestTime ) );
97126 }
98127}
0 commit comments