1+ package org .apache .commons .csv .perf ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .FileReader ;
5+ import java .io .IOException ;
6+ import java .io .Reader ;
7+
8+ import org .apache .commons .csv .CSVFormat ;
9+ import org .junit .Ignore ;
10+ import org .junit .Test ;
11+
12+ /**
13+ * Tests performance.
14+ *
15+ * Only enable for your own development.
16+ */
17+ public class PerformanceTest {
18+
19+ private final int max = 10 ;
20+
21+ private BufferedReader getBufferedReader () throws IOException {
22+ return new BufferedReader (new FileReader ("src/test/resources/worldcitiespop.txt" ));
23+ }
24+
25+ private long parse (Reader in ) throws IOException {
26+ CSVFormat format = CSVFormat .DEFAULT .withSurroundingSpacesIgnored (false );
27+ long count = 0 ;
28+ for (Object record : format .parse (in )) {
29+ count ++;
30+ }
31+ return count ;
32+ }
33+
34+ private void println () {
35+ System .out .println ();
36+ }
37+
38+ private void println (String s ) {
39+ System .out .println (s );
40+ }
41+
42+ private long readAll (BufferedReader in ) throws IOException {
43+ long count = 0 ;
44+ while (in .readLine () != null ) {
45+ count ++;
46+ }
47+ return count ;
48+ }
49+
50+ @ Test
51+ @ Ignore
52+ public void testParseBigFile () throws Exception {
53+ long t0 = System .currentTimeMillis ();
54+ long count = this .parse (this .getBufferedReader ());
55+ this .println ("File parsed in " + (System .currentTimeMillis () - t0 ) + "ms with Commons CSV" + " " + count
56+ + " lines" );
57+ this .println ();
58+ }
59+
60+ @ Test
61+ @ Ignore
62+ public void testParseBigFileRepeat () throws Exception {
63+ for (int i = 0 ; i < this .max ; i ++) {
64+ this .testParseBigFile ();
65+ }
66+ this .println ();
67+ }
68+
69+ @ Test
70+ @ Ignore
71+ public void testReadBigFile () throws Exception {
72+ for (int i = 0 ; i < this .max ; i ++) {
73+ BufferedReader in = this .getBufferedReader ();
74+ long t0 = System .currentTimeMillis ();
75+ long count = this .readAll (in );
76+ in .close ();
77+ this .println ("File read in " + (System .currentTimeMillis () - t0 ) + "ms" + " " + count + " lines" );
78+ }
79+ this .println ();
80+ }
81+ }
0 commit comments