View Javadoc

1   package net.sf.flatpack.examples.csvperformancetest;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileReader;
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   import net.sf.flatpack.DataError;
10  import net.sf.flatpack.DataSet;
11  import net.sf.flatpack.DefaultParserFactory;
12  import net.sf.flatpack.Parser;
13  
14  /*
15   * Created on Dec 1, 2005
16   *
17   * TODO To change the template for this generated file go to
18   * Window - Preferences - Java - Code Style - Code Templates
19   */
20  
21  /**
22   * @author zepernick
23   * 
24   * TODO To change the template for this generated type comment go to Window -
25   * Preferences - Java - Code Style - Code Templates
26   */
27  public class CSVPerformanceTest {
28  
29      public static void main(final String[] args) {
30  
31          Map settings = null;
32  
33          try {
34  
35              settings = readSettings();
36              final String filename = (String) settings.get("csvFile");
37              final String verbose = (String) settings.get("verbose");
38  
39              call(filename, Boolean.valueOf(verbose).booleanValue(), true);
40          } catch (final Exception ex) {
41              ex.printStackTrace();
42          }
43  
44      }
45  
46      public static void call(final String filename, final boolean verbose, final boolean traverse) throws Exception, InterruptedException {
47          String[] colNames = null;
48          // delimited by a comma
49          // text qualified by double quotes
50          // ignore first record
51          System.out.println("Parsing....");
52          final Parser pzparser = DefaultParserFactory.getInstance().newDelimitedParser(new File(filename), ',', '"');
53          long timeStarted = System.currentTimeMillis();
54          final DataSet ds = pzparser.parse();
55          long timeFinished = System.currentTimeMillis();
56  
57          String timeMessage = "";
58  
59          if (timeFinished - timeStarted < 1000) {
60              timeMessage = (timeFinished - timeStarted) + " Milleseconds...";
61          } else {
62              timeMessage = ((float) ((timeFinished - timeStarted) / 1000.0)) + " Seconds...";
63          }
64  
65          System.out.println("");
66          System.out.println("********FILE PARSED IN: " + timeMessage + " ******");
67  
68          if (traverse) {
69              if (verbose) {
70                  Thread.sleep(2000); // sleep for a couple seconds to the message
71                  // above can be read
72              }
73              timeStarted = System.currentTimeMillis();
74              colNames = ds.getColumns();
75              int rowCount = 0;
76              final int colCount = colNames.length;
77              while (ds.next()) {
78                  rowCount++;
79                  for (int i = 0; i < colNames.length; i++) {
80                      final String string = ds.getString(colNames[i]);
81  
82                      if (verbose) {
83                          System.out.println("COLUMN NAME: " + colNames[i] + " VALUE: " + string);
84                      }
85                  }
86  
87                  if (verbose) {
88                      System.out.println("===========================================================================");
89                  }
90              }
91              timeFinished = System.currentTimeMillis();
92  
93              if (timeFinished - timeStarted < 1000) {
94                  timeMessage = (timeFinished - timeStarted) + " Milleseconds...";
95              } else {
96                  timeMessage = ((float) ((timeFinished - timeStarted) / 1000.0)) + " Seconds...";
97              }
98  
99              System.out.println("");
100             System.out.println("********Traversed Data In: " + timeMessage + " (rows: " + rowCount + " Col:" + colCount + ") ******");
101 
102         }
103 
104         if (ds.getErrors() != null && ds.getErrors().size() > 0) {
105             System.out.println("FOUND ERRORS IN FILE....");
106             for (int i = 0; i < ds.getErrors().size(); i++) {
107                 final DataError de = (DataError) ds.getErrors().get(i);
108                 System.out.println("Error: " + de.getErrorDesc() + " Line: " + de.getLineNo());
109             }
110         }
111 
112     }
113 
114     private static Map readSettings() throws Exception {
115         final Map result = new HashMap();
116         FileReader fr = null;
117         BufferedReader br = null;
118         String line = null;
119 
120         try {
121             fr = new FileReader("settings.properties");
122             br = new BufferedReader(fr);
123 
124             while ((line = br.readLine()) != null) {
125                 if (line.trim().length() == 0 || line.startsWith("#") || line.indexOf("=") == -1) {
126                     continue;
127                 }
128 
129                 result.put(line.substring(0, line.indexOf("=")), line.substring(line.indexOf("=") + 1));
130             }
131         } finally {
132             if (fr != null) {
133                 fr.close();
134             }
135             if (br != null) {
136                 br.close();
137             }
138         }
139 
140         return result;
141 
142     }
143 
144 }