1818package org .apache .commons .csv ;
1919
2020import java .io .BufferedReader ;
21- import java .io .File ;
22- import java .io .FileInputStream ;
2321import java .io .IOException ;
2422import java .io .InputStream ;
23+ import java .io .Reader ;
2524import java .io .StringReader ;
2625import java .nio .charset .StandardCharsets ;
27- import java .util .List ;
26+ import java .util .Iterator ;
27+ import java .util .Scanner ;
2828import java .util .concurrent .TimeUnit ;
2929import java .util .zip .GZIPInputStream ;
3030
3131import com .generationjava .io .CsvReader ;
32+ import com .opencsv .CSVParserBuilder ;
33+ import com .opencsv .CSVReaderBuilder ;
34+
3235import org .apache .commons .io .IOUtils ;
3336import org .apache .commons .lang3 .StringUtils ;
3437import org .openjdk .jmh .annotations .Benchmark ;
@@ -62,124 +65,140 @@ public class CSVBenchmark {
6265 */
6366 @ Setup
6467 public void init () throws IOException {
65- final File file = new File ("src/test/resources/perf/worldcitiespop.txt.gz" );
66- final InputStream in = new GZIPInputStream (new FileInputStream (file ));
67- this .data = IOUtils .toString (in , StandardCharsets .ISO_8859_1 );
68- in .close ();
68+ InputStream in = this .getClass ().getClassLoader ().getResourceAsStream (
69+ "org/apache/commons/csv/perf/worldcitiespop.txt.gz" );
70+ try (final InputStream gzin = new GZIPInputStream (in , 8192 )) {
71+ this .data = IOUtils .toString (gzin , StandardCharsets .ISO_8859_1 );
72+ }
6973 }
7074
71- private BufferedReader getReader () {
72- return new BufferedReader ( new StringReader (data ) );
75+ private Reader getReader () {
76+ return new StringReader (data );
7377 }
7478
7579 @ Benchmark
7680 public int read (final Blackhole bh ) throws Exception {
77- final BufferedReader in = getReader ();
7881 int count = 0 ;
79- String line ;
80- while ((line = in .readLine ()) != null ) {
81- count ++;
82+
83+ try (BufferedReader reader = new BufferedReader (getReader ())) {
84+ while (reader .readLine () != null ) {
85+ count ++;
86+ }
8287 }
8388
8489 bh .consume (count );
85- in .close ();
8690 return count ;
8791 }
8892
8993 @ Benchmark
90- public int split (final Blackhole bh ) throws Exception {
91- final BufferedReader in = getReader ();
94+ public int scan (final Blackhole bh ) throws Exception {
9295 int count = 0 ;
93- String line ;
94- while ((line = in .readLine ()) != null ) {
95- final String [] values = StringUtils .split (line , ',' );
96- count += values .length ;
96+
97+ try (Scanner scanner = new Scanner (getReader ())) {
98+ while (scanner .hasNextLine ()) {
99+ scanner .nextLine ();
100+ count ++;
101+ }
97102 }
98103
99104 bh .consume (count );
100- in .close ();
101105 return count ;
102106 }
103107
104108 @ Benchmark
105- public int parseCommonsCSV (final Blackhole bh ) throws Exception {
106- final BufferedReader in = getReader () ;
109+ public int split (final Blackhole bh ) throws Exception {
110+ int count = 0 ;
107111
108- final CSVFormat format = CSVFormat .DEFAULT .withHeader ();
112+ try (BufferedReader reader = new BufferedReader (getReader ())) {
113+ String line ;
114+ while ((line = reader .readLine ()) != null ) {
115+ final String [] values = StringUtils .split (line , ',' );
116+ count += values .length ;
117+ }
118+ }
109119
120+ bh .consume (count );
121+ return count ;
122+ }
123+
124+ @ Benchmark
125+ public int parseCommonsCSV (final Blackhole bh ) throws Exception {
110126 int count = 0 ;
111- for (final CSVRecord record : format .parse (in )) {
112- count ++;
127+
128+ try (final Reader in = getReader ()) {
129+ final CSVFormat format = CSVFormat .Builder .create ().setSkipHeaderRecord (true ).build ();
130+ Iterator <CSVRecord > iter = format .parse (in ).iterator ();
131+ while (iter .hasNext ()) {
132+ count ++;
133+ iter .next ();
134+ }
113135 }
114136
115137 bh .consume (count );
116- in .close ();
117138 return count ;
118139 }
119140
120141 @ Benchmark
121142 public int parseGenJavaCSV (final Blackhole bh ) throws Exception {
122- final BufferedReader in = getReader ();
123-
124- final CsvReader reader = new CsvReader (in );
125- reader .setFieldDelimiter (',' );
126-
127143 int count = 0 ;
128- String [] record = null ;
129- while ((record = reader .readLine ()) != null ) {
130- count ++;
144+
145+ try (final Reader in = getReader ()) {
146+ final CsvReader reader = new CsvReader (in );
147+ reader .setFieldDelimiter (',' );
148+ while (reader .readLine () != null ) {
149+ count ++;
150+ }
131151 }
132152
133153 bh .consume (count );
134- in .close ();
135154 return count ;
136155 }
137156
138157 @ Benchmark
139158 public int parseJavaCSV (final Blackhole bh ) throws Exception {
140- final BufferedReader in = getReader ();
141-
142- final com .csvreader .CsvReader reader = new com .csvreader .CsvReader (in , ',' );
143- reader .setRecordDelimiter ('\n' );
144-
145159 int count = 0 ;
146- while (reader .readRecord ()) {
147- count ++;
160+
161+ try (final Reader in = getReader ()) {
162+ final com .csvreader .CsvReader reader = new com .csvreader .CsvReader (in , ',' );
163+ reader .setRecordDelimiter ('\n' );
164+ while (reader .readRecord ()) {
165+ count ++;
166+ }
148167 }
149168
150169 bh .consume (count );
151- in .close ();
152170 return count ;
153171 }
154172
155173 @ Benchmark
156174 public int parseOpenCSV (final Blackhole bh ) throws Exception {
157- final BufferedReader in = getReader () ;
175+ int count = 0 ;
158176
159- final com .opencsv .CSVReader reader = new com .opencsv .CSVReader (in , ',' );
177+ final com .opencsv .CSVParser parser = new CSVParserBuilder ()
178+ .withSeparator (',' ).withIgnoreQuotations (true ).build ();
160179
161- int count = 0 ;
162- while (reader .readNext () != null ) {
163- count ++;
180+ try (final Reader in = getReader ()) {
181+ final com .opencsv .CSVReader reader = new CSVReaderBuilder (in ).withSkipLines (1 ).withCSVParser (parser ).build ();
182+ while (reader .readNext () != null ) {
183+ count ++;
184+ }
164185 }
165186
166187 bh .consume (count );
167- in .close ();
168188 return count ;
169189 }
170190
171191 @ Benchmark
172192 public int parseSkifeCSV (final Blackhole bh ) throws Exception {
173- final BufferedReader in = getReader ();
174-
175193 final org .skife .csv .CSVReader reader = new org .skife .csv .SimpleReader ();
176194 reader .setSeperator (',' );
177-
178195 final CountingReaderCallback callback = new CountingReaderCallback ();
179- reader .parse (in , callback );
196+
197+ try (final Reader in = getReader ()) {
198+ reader .parse (in , callback );
199+ }
180200
181201 bh .consume (callback );
182- in .close ();
183202 return callback .count ;
184203 }
185204
@@ -194,18 +213,15 @@ public void onRow(final String[] fields) {
194213
195214 @ Benchmark
196215 public int parseSuperCSV (final Blackhole bh ) throws Exception {
197- final BufferedReader in = getReader ();
198-
199- final CsvListReader reader = new CsvListReader (in , CsvPreference .STANDARD_PREFERENCE );
200-
201216 int count = 0 ;
202- List <String > record = null ;
203- while ((record = reader .read ()) != null ) {
204- count ++;
217+
218+ try (final CsvListReader reader = new CsvListReader (getReader (), CsvPreference .STANDARD_PREFERENCE )) {
219+ while (reader .read () != null ) {
220+ count ++;
221+ }
205222 }
206223
207224 bh .consume (count );
208- in .close ();
209225 return count ;
210226 }
211227}
0 commit comments