View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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.saxbenchmark;
17  
18  import java.awt.Color;
19  import java.io.File;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.jfree.chart.ChartFactory;
24  import org.jfree.chart.ChartUtilities;
25  import org.jfree.chart.JFreeChart;
26  import org.jfree.chart.plot.PlotOrientation;
27  import org.jfree.data.DefaultCategoryDataset;
28  
29  /***
30   * @author jamie
31   */
32  public class ChartResultsWriter {
33      
34      private File folder;
35      private int width = 700;
36      private int height = 350;
37      
38      public ChartResultsWriter(String folderName) {
39          this.folder = new File(folderName);
40      }
41      
42      public ChartResultsWriter(File folder) {
43          this.folder = folder;
44      }
45      
46      public void writeMemoryResults(BenchmarkResults results) throws Exception {
47          
48          folder.mkdirs();
49  
50          DefaultCategoryDataset memoryDataSet = new DefaultCategoryDataset();
51          DefaultCategoryDataset timeDataSet = new DefaultCategoryDataset();
52  
53          for (Iterator parserNameIterator = results.getParserNames().iterator(); parserNameIterator.hasNext();) {
54              String parserName = (String) parserNameIterator.next();
55              for (Iterator sourceNameIterator = results.getSourceNames().iterator(); sourceNameIterator.hasNext();) {
56                  String sourceName = (String) sourceNameIterator.next();
57                  BenchmarkResult result = (BenchmarkResult) ((Map) results.getParsers().get(parserName)).get(sourceName);
58                  memoryDataSet.addValue(result.getParseMemory(), parserName, sourceName);
59                  timeDataSet.addValue(result.getParseTimeAverage(), parserName, sourceName);
60              }
61          }
62          
63          JFreeChart memoryChart = ChartFactory.createBarChart(
64              "Memory",
65              "Memory", "Total",
66              memoryDataSet,
67              PlotOrientation.VERTICAL,
68              true,
69              true,
70              false
71          );
72          
73          memoryChart.setBackgroundPaint(Color.white);
74          
75          ChartUtilities.saveChartAsPNG(new File(folder, "memory.png"), memoryChart, width, height);
76  
77          JFreeChart timeChart = ChartFactory.createBarChart(
78                  "Time",
79                  "Time", "Total",
80                  timeDataSet,
81                  PlotOrientation.VERTICAL,
82                  true,
83                  true,
84                  false
85              );
86              
87              timeChart.setBackgroundPaint(Color.white);
88              
89              ChartUtilities.saveChartAsPNG(new File(folder, "time.png"), timeChart, width, height);
90      }
91      
92      public int getHeight() {
93          return height;
94      }
95      public void setHeight(int height) {
96          this.height = height;
97      }
98      public int getWidth() {
99          return width;
100     }
101     public void setWidth(int width) {
102         this.width = width;
103     }
104 }