Coverage Report - org.apache.any23.writer.BenchmarkTripleHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
BenchmarkTripleHandler
0%
0/57
0%
0/18
1.833
BenchmarkTripleHandler$1
N/A
N/A
1.833
BenchmarkTripleHandler$StatObject
0%
0/10
N/A
1.833
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *  http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.any23.writer;
 19  
 
 20  
 import org.apache.any23.extractor.ExtractionContext;
 21  
 import org.openrdf.model.Resource;
 22  
 import org.openrdf.model.URI;
 23  
 import org.openrdf.model.Value;
 24  
 
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 import java.util.Map.Entry;
 28  
 
 29  
 
 30  
 /**
 31  
  * {@link TripleHandler} decorator useful to
 32  
  * perform benchmarking.
 33  
  */
 34  
 public class BenchmarkTripleHandler implements TripleHandler {
 35  
 
 36  
     /**
 37  
      * Decorated.
 38  
      */
 39  
     private TripleHandler underlyingHandler;
 40  
 
 41  
     /**
 42  
      * Collected statistics. 
 43  
      */
 44  
     private final Map<String, StatObject> stats;
 45  
 
 46  
     /**
 47  
      * Constructor.
 48  
      */
 49  0
     public BenchmarkTripleHandler(TripleHandler tripleHandler) {
 50  0
         if(tripleHandler == null) {
 51  0
             throw new NullPointerException("tripleHandler cannot be null.");
 52  
         }
 53  0
         underlyingHandler = tripleHandler;
 54  0
         stats = new HashMap<String, StatObject>();
 55  0
         stats.put("SUM", new StatObject());
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Returns the report as a human readable string.
 60  
      *
 61  
      * @return a human readable report.
 62  
      */
 63  
     public String report() {
 64  0
         StringBuilder sb = new StringBuilder();
 65  0
         StatObject sum = stats.get("SUM");
 66  
 
 67  0
         sb.append("\n>Summary: ");
 68  0
         sb.append("\n   -total calls: ").append(sum.methodCalls);
 69  0
         sb.append("\n   -total triples: ").append(sum.triples);
 70  0
         sb.append("\n   -total runtime: ").append(sum.runtime).append(" ms!");
 71  0
         if (sum.runtime != 0)
 72  0
             sb.append("\n   -tripls/ms: ").append(sum.triples / sum.runtime);
 73  0
         if (sum.methodCalls != 0)
 74  0
             sb.append("\n   -ms/calls: ").append(sum.runtime / sum.methodCalls);
 75  
 
 76  0
         stats.remove("SUM");
 77  
 
 78  0
         for (Entry<String, StatObject> ent : stats.entrySet()) {
 79  0
             sb.append("\n>Extractor: "       ).append(ent.getKey());
 80  0
             sb.append("\n   -total calls: "  ).append(ent.getValue().methodCalls);
 81  0
             sb.append("\n   -total triples: ").append(ent.getValue().triples);
 82  0
             sb.append("\n   -total runtime: ").append(ent.getValue().runtime).append(" ms!");
 83  0
             if (ent.getValue().runtime != 0)
 84  0
                 sb.append("\n   -tripls/ms: "  ).append(ent.getValue().triples / ent.getValue().runtime);
 85  0
             if (ent.getValue().methodCalls != 0)
 86  0
                 sb.append("\n   -ms/calls: "   ).append(ent.getValue().runtime / ent.getValue().methodCalls);
 87  
 
 88  
         }
 89  
 
 90  0
         return sb.toString();
 91  
     }
 92  
 
 93  
     public void startDocument(URI documentURI) throws TripleHandlerException {
 94  0
         underlyingHandler.startDocument(documentURI);
 95  0
     }
 96  
 
 97  
     public void close() throws TripleHandlerException {
 98  0
         underlyingHandler.close();
 99  0
     }
 100  
 
 101  
     public void closeContext(ExtractionContext context) throws TripleHandlerException {
 102  0
         if (stats.containsKey(context.getExtractorName())) {
 103  0
             stats.get(context.getExtractorName()).interimStop();
 104  0
             stats.get("SUM").interimStop();
 105  
         }
 106  0
         underlyingHandler.closeContext(context);
 107  0
     }
 108  
 
 109  
     public void openContext(ExtractionContext context) throws TripleHandlerException {
 110  0
         if (!stats.containsKey(context.getExtractorName())) {
 111  0
             stats.put(context.getExtractorName(), new StatObject());
 112  
         }
 113  0
         stats.get(context.getExtractorName()).methodCalls++;
 114  0
         stats.get(context.getExtractorName()).interimStart();
 115  0
         stats.get("SUM").methodCalls++;
 116  0
         stats.get("SUM").interimStart();
 117  0
         underlyingHandler.openContext(context);
 118  0
     }
 119  
 
 120  
     public void receiveTriple(Resource s, URI p, Value o, URI g, ExtractionContext context)
 121  
     throws TripleHandlerException {
 122  0
         if (!stats.containsKey(context.getExtractorName())) {
 123  0
             stats.put(context.getExtractorName(), new StatObject());
 124  
         }
 125  0
         stats.get(context.getExtractorName()).triples++;
 126  0
         stats.get("SUM").triples++;
 127  0
         underlyingHandler.receiveTriple(s, p, o, g, context);
 128  0
     }
 129  
 
 130  
     public void receiveNamespace(String prefix, String uri, ExtractionContext context) throws TripleHandlerException {
 131  0
         underlyingHandler.receiveNamespace(prefix, uri, context);
 132  0
     }
 133  
 
 134  
     public void endDocument(URI documentURI) throws TripleHandlerException {
 135  0
         underlyingHandler.endDocument(documentURI);
 136  0
     }
 137  
 
 138  
     public void setContentLength(long contentLength) {
 139  0
         underlyingHandler.setContentLength(contentLength);
 140  0
     }
 141  
 
 142  
     /**
 143  
      * A single statistics.
 144  
      */
 145  0
     private class StatObject {
 146  
 
 147  0
         int methodCalls = 0;
 148  0
         int triples     = 0;
 149  0
         long runtime    = 0;
 150  0
         long intStart   = 0;
 151  
 
 152  
         /**
 153  
          * Takes the start time.
 154  
          */
 155  
         public void interimStart() {
 156  0
             intStart = System.currentTimeMillis();
 157  0
         }
 158  
 
 159  
         /**
 160  
          * Takes the stop time.
 161  
          */
 162  
         public void interimStop() {
 163  0
             runtime += (System.currentTimeMillis() - intStart);
 164  0
             intStart = 0;
 165  0
         }
 166  
     }
 167  
 
 168  
 }
 169