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 static org.junit.Assert.assertEquals;
19  
20  import java.io.Writer;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.io.CsvBeanWriter;
26  import org.supercsv.io.CsvListWriter;
27  import org.supercsv.io.CsvMapWriter;
28  import org.supercsv.io.ICsvBeanWriter;
29  import org.supercsv.io.ICsvListWriter;
30  import org.supercsv.io.ICsvMapWriter;
31  import org.supercsv.io.dozer.CsvDozerBeanWriter;
32  import org.supercsv.io.dozer.ICsvDozerBeanWriter;
33  import org.supercsv.prefs.CsvPreference;
34  
35  import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
36  
37  /**
38   * Provides methods to benchmark all of Super CSV's writers.
39   * <p>
40   * The benchmarks use JUnitBenchmarks
41   * (http://labs.carrotsearch.com/junit-benchmarks-tutorial.html). As shown on
42   * the website, you can generate a Google charts graph from the results.
43   * <p>
44   * No assertions were required to prevent code being optimized away, but they
45   * were added to ensure the correct number of rows were written.
46   * 
47   * @author James Bassett
48   */
49  public class AbstractCsvWritingBenchmark extends AbstractBenchmark {
50  
51  	/**
52  	 * Times CsvListWriter.
53  	 * 
54  	 * @param writer
55  	 *            the writer
56  	 * @param preference
57  	 *            the preferences
58  	 * @param header
59  	 *            the header
60  	 * @param data
61  	 *            the data to write
62  	 * @param rows
63  	 *            the expected number of rows
64  	 * @throws Exception
65  	 */
66  	public void timeCsvListWriter(final Writer writer,
67  			final CsvPreference preference, final String[] header,
68  			final List<List<Object>> data, final int rows) throws Exception {
69  
70  		ICsvListWriter listWriter = null;
71  		try {
72  			listWriter = new CsvListWriter(writer, preference);
73  			listWriter.writeHeader(header);
74  
75  			for (List<?> row : data) {
76  				listWriter.write(row);
77  			}
78  
79  			assertEquals(rows + 1, listWriter.getRowNumber());
80  
81  		} finally {
82  			listWriter.close();
83  		}
84  
85  	}
86  
87  	/**
88  	 * Times CsvListWriter using processors.
89  	 * 
90  	 * @param writer
91  	 *            the writer
92  	 * @param preference
93  	 *            the preferences
94  	 * @param header
95  	 *            the header
96  	 * @param data
97  	 *            the data to write
98  	 * @param processors
99  	 *            the cell processors
100 	 * @param rows
101 	 *            the expected number of rows
102 	 * @throws Exception
103 	 */
104 	public void timeCsvListWriterUsingProcessors(final Writer writer,
105 			final CsvPreference preference, final String[] header,
106 			final List<List<Object>> data, final CellProcessor[] processors,
107 			final int rows) throws Exception {
108 
109 		ICsvListWriter listWriter = null;
110 		try {
111 			listWriter = new CsvListWriter(writer, preference);
112 			listWriter.writeHeader(header);
113 
114 			for (List<?> row : data) {
115 				listWriter.write(row, processors);
116 			}
117 
118 			assertEquals(rows + 1, listWriter.getRowNumber());
119 
120 		} finally {
121 			listWriter.close();
122 		}
123 
124 	}
125 
126 	/**
127 	 * Times CsvMapWriter.
128 	 * 
129 	 * @param writer
130 	 *            the writer
131 	 * @param preference
132 	 *            the preferences
133 	 * @param header
134 	 *            the header
135 	 * @param data
136 	 *            the data to write
137 	 * @param rows
138 	 *            the expected number of rows
139 	 * @throws Exception
140 	 */
141 	public void timeCsvMapWriter(final Writer writer,
142 			final CsvPreference preference, final String[] header,
143 			final List<Map<String, Object>> data, final int rows)
144 			throws Exception {
145 
146 		ICsvMapWriter mapWriter = null;
147 		try {
148 			mapWriter = new CsvMapWriter(writer, preference);
149 			mapWriter.writeHeader(header);
150 
151 			for (Map<String, ?> map : data) {
152 				mapWriter.write(map, header);
153 			}
154 
155 			assertEquals(rows + 1, mapWriter.getRowNumber());
156 
157 		} finally {
158 			mapWriter.close();
159 		}
160 	}
161 
162 	/**
163 	 * Times CsvMapWriter using processors.
164 	 * 
165 	 * @param writer
166 	 *            the writer
167 	 * @param preference
168 	 *            the preferences
169 	 * @param header
170 	 *            the header
171 	 * @param data
172 	 *            the data to write
173 	 * @param processors
174 	 *            the cell processors
175 	 * @param rows
176 	 *            the expected number of rows
177 	 * @throws Exception
178 	 */
179 	public void timeCsvMapWriterUsingProcessors(final Writer writer,
180 			final CsvPreference preference, final String[] header,
181 			final List<Map<String, Object>> data,
182 			final CellProcessor[] processors, final int rows) throws Exception {
183 
184 		ICsvMapWriter mapWriter = null;
185 		try {
186 			mapWriter = new CsvMapWriter(writer, preference);
187 			mapWriter.writeHeader(header);
188 
189 			for (Map<String, ?> map : data) {
190 				mapWriter.write(map, header, processors);
191 			}
192 
193 			assertEquals(rows + 1, mapWriter.getRowNumber());
194 
195 		} finally {
196 			mapWriter.close();
197 		}
198 	}
199 
200 	/**
201 	 * Times CsvBeanWriter.
202 	 * 
203 	 * @param writer
204 	 *            the writer
205 	 * @param preference
206 	 *            the preferences
207 	 * @param header
208 	 *            the header
209 	 * @param data
210 	 *            the data to write
211 	 * @param rows
212 	 *            the expected number of rows
213 	 * @throws Exception
214 	 */
215 	public void timeCsvBeanWriter(final Writer writer,
216 			final CsvPreference preference, final String[] header,
217 			final List<?> data, final int rows) throws Exception {
218 		ICsvBeanWriter beanWriter = null;
219 		try {
220 			beanWriter = new CsvBeanWriter(writer, preference);
221 			beanWriter.writeHeader(header);
222 
223 			for (Object o : data) {
224 				beanWriter.write(o, header);
225 			}
226 
227 			assertEquals(rows + 1, beanWriter.getRowNumber());
228 
229 		} finally {
230 			beanWriter.close();
231 		}
232 	}
233 
234 	/**
235 	 * Times CsvBeanWriter using processors.
236 	 * 
237 	 * @param writer
238 	 *            the writer
239 	 * @param preference
240 	 *            the preferences
241 	 * @param header
242 	 *            the header
243 	 * @param data
244 	 *            the data to write
245 	 * @param processors
246 	 *            the cell processors
247 	 * @param rows
248 	 *            the expected number of rows
249 	 * @throws Exception
250 	 */
251 	public void timeCsvBeanWriterUsingProcessors(final Writer writer,
252 			final CsvPreference preference, final String[] header,
253 			final List<?> data, final CellProcessor[] processors, final int rows)
254 			throws Exception {
255 
256 		ICsvBeanWriter beanWriter = null;
257 		try {
258 			beanWriter = new CsvBeanWriter(writer, preference);
259 			beanWriter.writeHeader(header);
260 
261 			for (Object o : data) {
262 				beanWriter.write(o, header, processors);
263 			}
264 
265 			assertEquals(rows + 1, beanWriter.getRowNumber());
266 
267 		} finally {
268 			beanWriter.close();
269 		}
270 	}
271 
272 	/**
273 	 * Times CsvDozerBeanWriter.
274 	 * 
275 	 * @param writer
276 	 *            the writer
277 	 * @param preference
278 	 *            the preferences
279 	 * @param header
280 	 *            the header
281 	 * @param beanClass
282 	 *            the type of the bean being written
283 	 * @param data
284 	 *            the data to write
285 	 * @param rows
286 	 *            the expected number of rows
287 	 * @throws Exception
288 	 */
289 	public void timeCsvDozerBeanWriter(final Writer writer,
290 			final CsvPreference preference, final String[] header,
291 			final Class<?> beanClass, final List<?> data, final int rows)
292 			throws Exception {
293 
294 		ICsvDozerBeanWriter dozerBeanWriter = null;
295 		try {
296 			dozerBeanWriter = new CsvDozerBeanWriter(writer, preference);
297 			dozerBeanWriter.writeHeader(header);
298 			dozerBeanWriter.configureBeanMapping(beanClass, header);
299 
300 			for (Object o : data) {
301 				dozerBeanWriter.write(o);
302 			}
303 
304 			assertEquals(rows + 1, dozerBeanWriter.getRowNumber());
305 
306 		} finally {
307 			dozerBeanWriter.close();
308 		}
309 	}
310 
311 	/**
312 	 * Times CsvDozerBeanWriter using processors.
313 	 * 
314 	 * @param writer
315 	 *            the writer
316 	 * @param preference
317 	 *            the preferences
318 	 * @param header
319 	 *            the header
320 	 * @param beanClass
321 	 *            the type of the bean being written
322 	 * @param data
323 	 *            the data to write
324 	 * @param processors
325 	 *            the cell processors
326 	 * @param rows
327 	 *            the expected number of rows
328 	 * @throws Exception
329 	 */
330 	public void timeCsvDozerBeanWriterUsingProcessors(final Writer writer,
331 			final CsvPreference preference, final String[] header,
332 			final Class<?> beanClass, final List<?> data,
333 			final CellProcessor[] processors, final int rows) throws Exception {
334 		ICsvDozerBeanWriter dozerBeanWriter = null;
335 		try {
336 			dozerBeanWriter = new CsvDozerBeanWriter(writer, preference);
337 			dozerBeanWriter.writeHeader(header);
338 			dozerBeanWriter.configureBeanMapping(beanClass, header);
339 
340 			for (Object o : data) {
341 				dozerBeanWriter.write(o, processors);
342 			}
343 
344 			assertEquals(rows + 1, dozerBeanWriter.getRowNumber());
345 
346 		} finally {
347 			dozerBeanWriter.close();
348 		}
349 	}
350 
351 }