View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.supercsv.benchmark;
17  
18  import java.io.FileReader;
19  
20  import org.junit.FixMethodOrder;
21  import org.junit.Test;
22  import org.junit.runners.MethodSorters;
23  import org.supercsv.benchmark.cellprocessor.ParseIconColour;
24  import org.supercsv.benchmark.cellprocessor.ParseStopType;
25  import org.supercsv.benchmark.cellprocessor.ParseStopTypeAndName;
26  import org.supercsv.benchmark.model.TransportLocation;
27  import org.supercsv.benchmark.model.TransportLocationStrings;
28  import org.supercsv.cellprocessor.Optional;
29  import org.supercsv.cellprocessor.ParseDouble;
30  import org.supercsv.cellprocessor.constraint.NotNull;
31  import org.supercsv.cellprocessor.ift.CellProcessor;
32  import org.supercsv.prefs.CsvPreference;
33  
34  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
35  
36  /**
37   * Benchmarks each of the Super CSV readers. The CSV file is the first 50,000
38   * rows of a file free for download from the Guardian newspaper website
39   * (http://www.guardian.co.uk/news/datablog/2010/sep/27/uk-transport-national
40   * -public-data-repository#data). The existing header was replaced with a
41   * CsvBeanReader-compatible header for convenience. The file contains no
42   * embedded newlines.
43   * 
44   * @author James Bassett
45   */
46  @BenchmarkMethodChart(filePrefix = "StandardCsvReadingBenchmark")
47  @FixMethodOrder(MethodSorters.NAME_ASCENDING)
48  public class StandardCsvReadingBenchmarkTest extends AbstractCsvReadingBenchmark {
49  
50  	private static final CsvPreference PREFS = new CsvPreference.Builder(
51  			CsvPreference.STANDARD_PREFERENCE).build();
52  
53  	// CSV file with 50,001 lines (including header)
54  	private static final String CSV_FILE = "Britain's transport infrastructure.csv";
55  
56  	public static final CellProcessor[] PROCESSORS = { new NotNull(), // atcoCode
57  			new NotNull(new ParseDouble()), // easting
58  			new NotNull(new ParseDouble()), // northing
59  			new NotNull(new ParseDouble()), // longitude
60  			new NotNull(new ParseDouble()), // latitude
61  			new NotNull(), // commonName
62  			new Optional(), // identifier
63  			new NotNull(), // direction
64  			new NotNull(), // street
65  			new Optional(), // landmark
66  			new NotNull(), // natGazId
67  			new Optional(), // natGazLocality
68  			new NotNull(new ParseStopType()), // stopType
69  			new NotNull(new ParseStopTypeAndName()), // stopTypeAndName
70  			new NotNull(new ParseIconColour()), // iconColour
71  	};
72  
73  	// the number of data rows to read (max possible is 50,000)
74  	private static final int ROWS = 50000;
75  
76  	/**
77  	 * Times CsvListReader.
78  	 */
79  	@Test
80  	public void testCsvListReader() throws Exception {
81  		timeCsvListReader(new FileReader(CSV_FILE), PREFS, ROWS);
82  	}
83  
84  	/**
85  	 * Times CsvListReader using processors.
86  	 */
87  	@Test
88  	public void testCsvListReaderUsingProcessors() throws Exception {
89  		timeCsvListReaderUsingProcessors(new FileReader(CSV_FILE), PREFS,
90  				PROCESSORS, ROWS);
91  	}
92  
93  	/**
94  	 * Times CsvMapReader.
95  	 */
96  	@Test
97  	public void testCsvMapReader() throws Exception {
98  		timeCsvMapReader(new FileReader(CSV_FILE), PREFS, ROWS);
99  	}
100 
101 	/**
102 	 * Times CsvMapReader using processors.
103 	 */
104 	@Test
105 	public void testCsvMapReaderUsingProcessors() throws Exception {
106 		timeCsvMapReaderUsingProcessors(new FileReader(CSV_FILE), PREFS,
107 				PROCESSORS, ROWS);
108 	}
109 
110 	/**
111 	 * Times CsvBeanReader.
112 	 */
113 	@Test
114 	public void testCsvBeanReader() throws Exception {
115 		timeCsvBeanReader(new FileReader(CSV_FILE), PREFS,
116 				TransportLocationStrings.class, ROWS);
117 	}
118 
119 	/**
120 	 * Times CsvBeanReader using processors.
121 	 */
122 	@Test
123 	public void testCsvBeanReaderUsingProcessors() throws Exception {
124 		timeCsvBeanReaderUsingProcessors(new FileReader(CSV_FILE), PREFS,
125 				TransportLocation.class, PROCESSORS, ROWS);
126 	}
127 
128 	/**
129 	 * Times CsvDozerBeanReader.
130 	 */
131 	@Test
132 	public void testCsvDozerBeanReader() throws Exception {
133 		timeCsvDozerBeanReader(new FileReader(CSV_FILE), PREFS,
134 				TransportLocationStrings.class, ROWS);
135 	}
136 
137 	/**
138 	 * Times CsvDozerBeanReader using processors.
139 	 */
140 	@Test
141 	public void testCsvDozerBeanReaderUsingProcessors() throws Exception {
142 		timeCsvDozerBeanReaderUsingProcessors(new FileReader(CSV_FILE), PREFS,
143 				TransportLocation.class, PROCESSORS, ROWS);
144 	}
145 
146 }