001: package org.apache.lucene.benchmark.stats;
002:
003: /**
004: * Copyright 2005 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.LinkedHashMap;
020: import java.util.Vector;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: /**
025: * This class holds series of TimeData related to a single test run. TimeData
026: * values may contribute to different measurements, so this class provides also
027: * some useful methods to separate them.
028: *
029: * @author Andrzej Bialecki <ab@getopt.org>
030: */
031: public class TestRunData {
032: private String id;
033:
034: /** Start and end time of this test run. */
035: private long start = 0L, end = 0L;
036:
037: private LinkedHashMap data = new LinkedHashMap();
038:
039: public TestRunData() {
040: }
041:
042: public TestRunData(String id) {
043: this .id = id;
044: }
045:
046: public LinkedHashMap getData() {
047: return data;
048: }
049:
050: public String getId() {
051: return id;
052: }
053:
054: public void setId(String id) {
055: this .id = id;
056: }
057:
058: public long getEnd() {
059: return end;
060: }
061:
062: public long getStart() {
063: return start;
064: }
065:
066: /** Mark the starting time of this test run. */
067: public void startRun() {
068: start = System.currentTimeMillis();
069: }
070:
071: /** Mark the ending time of this test run. */
072: public void endRun() {
073: end = System.currentTimeMillis();
074: }
075:
076: /** Add a data point. */
077: public void addData(TimeData td) {
078: td.recordMemUsage();
079: Vector v = (Vector) data.get(td.name);
080: if (v == null) {
081: v = new Vector();
082: data.put(td.name, v);
083: }
084: v.add(td.clone());
085: }
086:
087: /** Get a list of all available types of data points. */
088: public Collection getLabels() {
089: return data.keySet();
090: }
091:
092: /** Get total values from all data points of a given type. */
093: public TimeData getTotals(String label) {
094: Vector v = (Vector) data.get(label);
095: if (v == null) {
096: return null;
097: }
098: TimeData res = new TimeData("TOTAL " + label);
099: for (int i = 0; i < v.size(); i++) {
100: TimeData td = (TimeData) v.get(i);
101: res.count += td.count;
102: res.elapsed += td.elapsed;
103: }
104: return res;
105: }
106:
107: /** Get total values from all data points of all types.
108: * @return a list of TimeData values for all types.
109: */
110: public Vector getTotals() {
111: Collection labels = getLabels();
112: Vector v = new Vector();
113: Iterator it = labels.iterator();
114: while (it.hasNext()) {
115: TimeData td = getTotals((String) it.next());
116: v.add(td);
117: }
118: return v;
119: }
120:
121: /** Get memory usage stats for a given data type. */
122: public MemUsage getMemUsage(String label) {
123: Vector v = (Vector) data.get(label);
124: if (v == null) {
125: return null;
126: }
127: MemUsage res = new MemUsage();
128: res.minFree = Long.MAX_VALUE;
129: res.minTotal = Long.MAX_VALUE;
130: long avgFree = 0L, avgTotal = 0L;
131: for (int i = 0; i < v.size(); i++) {
132: TimeData td = (TimeData) v.get(i);
133: if (res.maxFree < td.freeMem) {
134: res.maxFree = td.freeMem;
135: }
136: if (res.maxTotal < td.totalMem) {
137: res.maxTotal = td.totalMem;
138: }
139: if (res.minFree > td.freeMem) {
140: res.minFree = td.freeMem;
141: }
142: if (res.minTotal > td.totalMem) {
143: res.minTotal = td.totalMem;
144: }
145: avgFree += td.freeMem;
146: avgTotal += td.totalMem;
147: }
148: res.avgFree = avgFree / v.size();
149: res.avgTotal = avgTotal / v.size();
150: return res;
151: }
152:
153: /** Return a string representation. */
154: public String toString() {
155: StringBuffer sb = new StringBuffer();
156: Collection labels = getLabels();
157: Iterator it = labels.iterator();
158: while (it.hasNext()) {
159: String label = (String) it.next();
160: sb.append(id).append("-").append(label).append(" ").append(
161: getTotals(label).toString(false)).append(" ");
162: sb.append(
163: getMemUsage(label)
164: .toScaledString(1024 * 1024, "MB")).append(
165: "\n");
166: }
167: return sb.toString();
168: }
169: }
|