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.io.Writer;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  /***
24   * @author JMcCrindle
25   */
26  public class SaxBenchmark {
27      
28      private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
29              .getLog(SaxBenchmark.class);
30  
31      private BenchmarkFactory benchmarkFactory = null;
32      private WriterFactory writerFactory = null;
33      private OutputterFactorySupplier outputterFactorySupplier = null;
34      
35      public BenchmarkResults run() throws Throwable {
36  
37          List results = new ArrayList();
38          
39          List benchmarks = benchmarkFactory.getBenchmarks();
40          List outputterFactories = outputterFactorySupplier.getOutputterFactories();
41          for (Iterator iter = benchmarks.iterator(); iter.hasNext();) {
42              Benchmark benchmark = (Benchmark) iter.next();
43              BenchmarkResult result = new BenchmarkResult();
44              result.setBenchmark(benchmark);
45              try {
46                  long parseTime = benchmark.timeParse();
47                  result.setParseTime(parseTime);
48                  long parseMemory = benchmark.memoryParse();
49                  result.setParseMemory(parseMemory);
50              } catch (Throwable e) {
51                  log.warn(e, e);
52                  result.setException(e);
53              }
54              for (Iterator iterator = outputterFactories.iterator(); iterator
55                      .hasNext();) {
56                  OutputterFactory outputterFactory = (OutputterFactory) iterator.next();
57                  Writer writer = null;
58                  try {
59                      writer = writerFactory.getWriter(benchmark, outputterFactory);
60                      benchmark.serialize(outputterFactory.getHtmlWriter(), writer);
61                  } catch (Throwable e) {
62                      log.warn(e, e);
63                  } finally {
64                      if(writer != null) {
65                          writer.flush();
66                          writer.close();
67                      }
68                  }
69              }
70              results.add(result);
71          }
72  
73          return new BenchmarkResults(results, outputterFactories);
74  
75      }
76  
77      public void setBenchmarkFactory(BenchmarkFactory factory) {
78          this.benchmarkFactory = factory;
79      }
80      public WriterFactory getWriterFactory() {
81          return writerFactory;
82      }
83      public void setWriterFactory(WriterFactory writerFactory) {
84          this.writerFactory = writerFactory;
85      }
86      public OutputterFactorySupplier getOutputterFactorySupplier() {
87          return outputterFactorySupplier;
88      }
89      public void setOutputterFactorySupplier(
90              OutputterFactorySupplier outputterFactorySupplier) {
91          this.outputterFactorySupplier = outputterFactorySupplier;
92      }
93  }