001: package org.apache.lucene.benchmark.byTask.tasks;
002:
003: import java.util.Iterator;
004: import java.util.LinkedHashMap;
005:
006: import org.apache.lucene.benchmark.byTask.PerfRunData;
007: import org.apache.lucene.benchmark.byTask.stats.Report;
008: import org.apache.lucene.benchmark.byTask.stats.TaskStats;
009: import org.apache.lucene.benchmark.byTask.utils.Format;
010:
011: /**
012: * Licensed to the Apache Software Foundation (ASF) under one or more
013: * contributor license agreements. See the NOTICE file distributed with
014: * this work for additional information regarding copyright ownership.
015: * The ASF licenses this file to You under the Apache License, Version 2.0
016: * (the "License"); you may not use this file except in compliance with
017: * the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing, software
022: * distributed under the License is distributed on an "AS IS" BASIS,
023: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
024: * See the License for the specific language governing permissions and
025: * limitations under the License.
026: */
027:
028: /**
029: * Report (abstract) task - all report tasks extend this task.
030: */
031: public abstract class ReportTask extends PerfTask {
032:
033: public ReportTask(PerfRunData runData) {
034: super (runData);
035: }
036:
037: /* (non-Javadoc)
038: * @see PerfTask#shouldNeverLogAtStart()
039: */
040: protected boolean shouldNeverLogAtStart() {
041: return true;
042: }
043:
044: /* (non-Javadoc)
045: * @see PerfTask#shouldNotRecordStats()
046: */
047: protected boolean shouldNotRecordStats() {
048: return true;
049: }
050:
051: /*
052: * From here start the code used to generate the reports.
053: * Subclasses would use this part to generate reports.
054: */
055:
056: protected static final String newline = System
057: .getProperty("line.separator");
058:
059: /**
060: * Get a textual summary of the benchmark results, average from all test runs.
061: */
062: protected static final String OP = "Operation ";
063: protected static final String ROUND = " round";
064: protected static final String RUNCNT = " runCnt";
065: protected static final String RECCNT = " recsPerRun";
066: protected static final String RECSEC = " rec/s";
067: protected static final String ELAPSED = " elapsedSec";
068: protected static final String USEDMEM = " avgUsedMem";
069: protected static final String TOTMEM = " avgTotalMem";
070: protected static final String COLS[] = { RUNCNT, RECCNT, RECSEC,
071: ELAPSED, USEDMEM, TOTMEM };
072:
073: /**
074: * Compute a title line for a report table
075: * @param longestOp size of longest op name in the table
076: * @return the table title line.
077: */
078: protected String tableTitle(String longestOp) {
079: StringBuffer sb = new StringBuffer();
080: sb.append(Format.format(OP, longestOp));
081: sb.append(ROUND);
082: sb
083: .append(getRunData().getConfig()
084: .getColsNamesForValsByRound());
085: for (int i = 0; i < COLS.length; i++) {
086: sb.append(COLS[i]);
087: }
088: return sb.toString();
089: }
090:
091: /**
092: * find the longest op name out of completed tasks.
093: * @param taskStats completed tasks to be considered.
094: * @return the longest op name out of completed tasks.
095: */
096: protected String longestOp(Iterator taskStats) {
097: String longest = OP;
098: while (taskStats.hasNext()) {
099: TaskStats stat = (TaskStats) taskStats.next();
100: if (stat.getElapsed() >= 0) { // consider only tasks that ended
101: String name = stat.getTask().getName();
102: if (name.length() > longest.length()) {
103: longest = name;
104: }
105: }
106: }
107: return longest;
108: }
109:
110: /**
111: * Compute a report line for the given task stat.
112: * @param longestOp size of longest op name in the table.
113: * @param stat task stat to be printed.
114: * @return the report line.
115: */
116: protected String taskReportLine(String longestOp, TaskStats stat) {
117: PerfTask task = stat.getTask();
118: StringBuffer sb = new StringBuffer();
119: sb.append(Format.format(task.getName(), longestOp));
120: String round = (stat.getRound() >= 0 ? "" + stat.getRound()
121: : "-");
122: sb.append(Format.formatPaddLeft(round, ROUND));
123: sb.append(getRunData().getConfig().getColsValuesForValsByRound(
124: stat.getRound()));
125: sb.append(Format.format(stat.getNumRuns(), RUNCNT));
126: sb.append(Format.format(stat.getCount() / stat.getNumRuns(),
127: RECCNT));
128: long elapsed = (stat.getElapsed() > 0 ? stat.getElapsed() : 1); // assume at least 1ms
129: sb.append(Format.format(1,
130: (float) (stat.getCount() * 1000.0 / elapsed), RECSEC));
131: sb.append(Format.format(2, (float) stat.getElapsed() / 1000,
132: ELAPSED));
133: sb.append(Format.format(0, (float) stat.getMaxUsedMem()
134: / stat.getNumRuns(), USEDMEM));
135: sb.append(Format.format(0, (float) stat.getMaxTotMem()
136: / stat.getNumRuns(), TOTMEM));
137: return sb.toString();
138: }
139:
140: protected Report genPartialReport(int reported,
141: LinkedHashMap partOfTasks, int totalSize) {
142: String longetOp = longestOp(partOfTasks.values().iterator());
143: boolean first = true;
144: StringBuffer sb = new StringBuffer();
145: sb.append(tableTitle(longetOp));
146: sb.append(newline);
147: int lineNum = 0;
148: for (Iterator it = partOfTasks.values().iterator(); it
149: .hasNext();) {
150: TaskStats stat = (TaskStats) it.next();
151: if (!first) {
152: sb.append(newline);
153: }
154: first = false;
155: String line = taskReportLine(longetOp, stat);
156: lineNum++;
157: if (partOfTasks.size() > 2 && lineNum % 2 == 0) {
158: line = line.replaceAll(" ", " - ");
159: }
160: sb.append(line);
161: }
162: String reptxt = (reported == 0 ? "No Matching Entries Were Found!"
163: : sb.toString());
164: return new Report(reptxt, partOfTasks.size(), reported,
165: totalSize);
166: }
167:
168: }
|