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.math.BigDecimal;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.TreeSet;
25  
26  /***
27   * @author JMcCrindle
28   */
29  public class BenchmarkResults {
30  
31      private static class ParserSourceCombination {
32          private final String parserName;
33          private final String sourceName;
34          public ParserSourceCombination(String parserName, String sourceName) {
35              this.parserName = parserName;
36              this.sourceName = sourceName;
37          }
38          public boolean equals(Object obj) {
39              // ignoring nulls
40              ParserSourceCombination p = (ParserSourceCombination) obj;
41              return p.parserName.equals(parserName) && p.sourceName.equals(sourceName);
42          }
43          public int hashCode() {
44              // ignoring nulls
45              return parserName.hashCode() + sourceName.hashCode();
46          }
47          public String getParserName() {
48              return parserName;
49          }
50          public String getSourceName() {
51              return sourceName;
52          }
53      }
54      
55      private Map parsers = new HashMap();
56      private List results;
57      private Set sourceNames = new TreeSet(); 
58      private Set parserNames = new TreeSet();
59      private List outputterFactories; 
60      private Map bestTimes = new HashMap();
61      private Map bestMemory = new HashMap();
62      
63      public BenchmarkResults(List results, List outputterFactories) {
64          this.results = results;
65          this.outputterFactories = outputterFactories;
66          for (Iterator iter = results.iterator(); iter.hasNext();) {
67              BenchmarkResult result = (BenchmarkResult) iter.next();
68              String name = result.getBenchmark().getName();
69              parserNames.add(name);
70              Map sources = (Map) parsers.get(name);
71              if(sources == null) {
72                  sources = new HashMap();
73                  parsers.put(name, sources);
74              }
75              String sourceName = result.getBenchmark().getSourceFactory().getName();
76              sources.put(sourceName, result);
77              sourceNames.add(sourceName);
78              if(result.getException() == null) {
79                  BigDecimal time = (BigDecimal) bestTimes.get(sourceName);
80                  if(time == null) {
81                      bestTimes.put(sourceName, result.getParseTimeAverage());
82                  } else {
83                      if(result.getParseTimeAverage().compareTo(time) < 0) {
84                          bestTimes.put(sourceName, result.getParseTimeAverage());
85                      }
86                  }
87                  Number memory = (Number) bestMemory.get(sourceName);
88                  if(memory == null) {
89                      bestMemory.put(sourceName, new Long(result.getParseMemory()));
90                  } else {
91                      if(result.getParseMemory() < memory.longValue()) {
92                          bestMemory.put(sourceName, new Long(result.getParseMemory()));
93                      }
94                  }
95              }
96          }
97      }
98      
99      public boolean isLowestMemory(String parserName, String sourceName) {
100         Map sources = (Map) parsers.get(parserName);
101         BenchmarkResult result = (BenchmarkResult) sources.get(sourceName);
102         if(result.getException() != null) return false;
103         Long value = (Long) bestMemory.get(sourceName);
104         return result.getParseMemory() == value.longValue();
105     }
106     
107     public boolean isLowestTime(String parserName, String sourceName) {
108         Map sources = (Map) parsers.get(parserName);
109         BenchmarkResult result = (BenchmarkResult) sources.get(sourceName);
110         if(result.getException() != null) return false;
111         BigDecimal value = (BigDecimal) bestTimes.get(sourceName);
112         return result.getParseTimeAverage().equals(value);
113     }
114     
115     public Map getParsers() {
116         return parsers;
117     }
118 
119     public List getResults() {
120         return results;
121     }
122     
123     public Set getSourceNames() {
124         return sourceNames;
125     }
126 
127     public Set getParserNames() {
128         return parserNames;
129     }
130     public List getOutputterFactories() {
131         return outputterFactories;
132     }
133     public Map getBestMemory() {
134         return bestMemory;
135     }
136     public Map getBestTimes() {
137         return bestTimes;
138     }
139 }