1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ package org .apache .commons .csv ;
19+
20+ import java .io .BufferedReader ;
21+ import java .io .FileReader ;
22+ import java .io .IOException ;
23+
24+ /**
25+ * Basic test harness.
26+ *
27+ * Requires test file to be downloaded separately.
28+ *
29+ */
30+ @ SuppressWarnings ("boxing" )
31+ public class PerformanceTest {
32+
33+ private static final String [] PROPS = {
34+ "java.version" , // Java Runtime Environment version
35+ "java.vendor" , // Java Runtime Environment vendor
36+ // "java.vm.specification.version", // Java Virtual Machine specification version
37+ // "java.vm.specification.vendor", // Java Virtual Machine specification vendor
38+ // "java.vm.specification.name", // Java Virtual Machine specification name
39+ "java.vm.version" , // Java Virtual Machine implementation version
40+ // "java.vm.vendor", // Java Virtual Machine implementation vendor
41+ "java.vm.name" , // Java Virtual Machine implementation name
42+ // "java.specification.version", // Java Runtime Environment specification version
43+ // "java.specification.vendor", // Java Runtime Environment specification vendor
44+ // "java.specification.name", // Java Runtime Environment specification name
45+
46+ "os.name" , // Operating system name
47+ "os.arch" , // Operating system architecture
48+ "os.version" , // Operating system version
49+
50+ };
51+
52+ private static int max = 10 ;
53+
54+ private static int num = 0 ; // number of elapsed times recorded
55+ private static long [] elapsedTimes = new long [max ];
56+
57+ private static final CSVFormat format = CSVFormat .DEFAULT .withSurroundingSpacesIgnored (false );
58+
59+ public static void main (String [] args ) throws Exception {
60+ for (String p : PROPS ) {
61+ System .out .println (p +"=" +System .getProperty (p ));
62+ }
63+ System .out .println ("Max count: " +max +"\n " );
64+
65+ testReadBigFile (false );
66+ testReadBigFile (true );
67+ testParseCommonsCSV ();
68+ }
69+
70+ private static BufferedReader getReader () throws IOException {
71+ return new BufferedReader (new FileReader ("worldcitiespop.txt" ));
72+ }
73+
74+ // Container for basic statistics
75+ private static class Stats {
76+ final int count ;
77+ final int fields ;
78+ Stats (int c , int f ) {
79+ count =c ;
80+ fields =f ;
81+ }
82+ }
83+
84+ // Display end stats; store elapsed for average
85+ private static void show (String msg , Stats s , long start ) {
86+ final long elapsed = System .currentTimeMillis () - start ;
87+ System .out .printf ("%-20s: %5dms " + s .count + " lines " + s .fields + " fields%n" ,msg ,elapsed );
88+ elapsedTimes [num ++]=elapsed ;
89+ }
90+
91+ // calculate and show average
92+ private static void show (){
93+ long tot = 0 ;
94+ if (num > 1 ) {
95+ for (int i =1 ; i < num ; i ++) { // skip first test
96+ tot += elapsedTimes [i ];
97+ }
98+ System .out .printf ("%-20s: %5dms%n%n" , "Average(not first)" , (tot /(num -1 )));
99+ }
100+ num =0 ; // ready for next set
101+ }
102+
103+ private static void testReadBigFile (boolean split ) throws Exception {
104+ for (int i = 0 ; i < max ; i ++) {
105+ BufferedReader in = getReader ();
106+ long t0 = System .currentTimeMillis ();
107+ Stats s = readAll (in , split );
108+ in .close ();
109+ show (split ?"file+split" :"file" , s , t0 );
110+ }
111+ show ();
112+ }
113+
114+ private static Stats readAll (BufferedReader in , boolean split ) throws IOException {
115+ int count = 0 ;
116+ int fields = 0 ;
117+ String record ;
118+ while ((record =in .readLine ()) != null ) {
119+ count ++;
120+ fields += split ? record .split ("," ).length : 1 ;
121+ }
122+ return new Stats (count , fields );
123+ }
124+
125+ private static void testParseCommonsCSV () throws Exception {
126+ for (int i = 0 ; i < max ; i ++) {
127+ final BufferedReader reader = getReader ();
128+ CSVParser parser = new CSVParser (reader , format );
129+ long t0 = System .currentTimeMillis ();
130+ Stats s = iterate (parser );
131+ reader .close ();
132+ show ("CSV" , s , t0 );
133+ }
134+ show ();
135+ }
136+
137+ private static Stats iterate (Iterable <CSVRecord > it ) {
138+ int count = 0 ;
139+ int fields = 0 ;
140+ for (CSVRecord record : it ) {
141+ count ++;
142+ fields +=record .size ();
143+ }
144+ return new Stats (count , fields );
145+ }
146+
147+ }
0 commit comments