1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
32 | |
|
33 | |
|
34 | |
public class BenchmarkTripleHandler implements TripleHandler { |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private TripleHandler underlyingHandler; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private final Map<String, StatObject> stats; |
45 | |
|
46 | |
|
47 | |
|
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 | |
|
60 | |
|
61 | |
|
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 | |
|
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 | |
|
154 | |
|
155 | |
public void interimStart() { |
156 | 0 | intStart = System.currentTimeMillis(); |
157 | 0 | } |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void interimStop() { |
163 | 0 | runtime += (System.currentTimeMillis() - intStart); |
164 | 0 | intStart = 0; |
165 | 0 | } |
166 | |
} |
167 | |
|
168 | |
} |
169 | |
|