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  
20  import org.xml.sax.XMLReader;
21  
22  /***
23   * @author JMcCrindle
24   */
25  public class Benchmark {
26  
27      private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
28              .getLog(Benchmark.class);
29      
30      private String name = null;
31      private XMLReader reader = null;
32      private SourceFactory inputSourceFactory = null;
33      private int count = 0;
34  
35      /***
36       * @param reader
37       * @param source
38       * @param count
39       */
40      public Benchmark(String name, XMLReader reader, SourceFactory inputSourceFactory, int count) {
41          super();
42          this.name = name;
43          this.reader = reader;
44          this.inputSourceFactory = inputSourceFactory;
45          this.count = count;
46      }
47  
48      public long timeParse() throws Throwable {
49          EmptyContentHandler contentHandler = new EmptyContentHandler();
50          reader.setContentHandler(contentHandler);
51  
52          reader.parse(inputSourceFactory.getInputSource());
53  
54          long startTime = System.currentTimeMillis();
55          for(int i = 0; i < count; i++) {
56              reader.parse(inputSourceFactory.getInputSource());
57          }
58          long finishTime = System.currentTimeMillis();
59  
60          return finishTime - startTime;
61      }
62      
63      public long memoryParse() throws Throwable {
64          EmptyContentHandler contentHandler = new EmptyContentHandler();
65          reader.setContentHandler(contentHandler);
66  
67          System.gc();
68          long startMemory = Runtime.getRuntime().freeMemory();
69          reader.parse(inputSourceFactory.getInputSource());
70          long finishMemory = Runtime.getRuntime().freeMemory();
71          long memory = startMemory - finishMemory;
72          
73          log.debug(reader.getClass());
74          
75          return memory;
76      }
77      
78      public void serialize(Outputter serializer, Writer writer) throws Throwable {
79          serializer.setWriter(writer);
80          reader.setContentHandler(serializer.asContentHandler());
81          reader.parse(inputSourceFactory.getInputSource());
82      }
83  
84      public int getCount() {
85          return count;
86      }
87  
88      public String getName() {
89          return name;
90      }
91  
92      public SourceFactory getSourceFactory() {
93          return inputSourceFactory;
94      }
95  
96  }