pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / ReportTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
2
3 import java.util.LinkedHashMap;
4
5 import org.apache.lucene.benchmark.byTask.PerfRunData;
6 import org.apache.lucene.benchmark.byTask.stats.Report;
7 import org.apache.lucene.benchmark.byTask.stats.TaskStats;
8 import org.apache.lucene.benchmark.byTask.utils.Format;
9
10 /**
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements.  See the NOTICE file distributed with
13  * this work for additional information regarding copyright ownership.
14  * The ASF licenses this file to You under the Apache License, Version 2.0
15  * (the "License"); you may not use this file except in compliance with
16  * the License.  You may obtain a copy of the License at
17  *
18  *     http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */
26
27 /**
28  * Report (abstract) task - all report tasks extend this task.
29  */
30 public abstract class ReportTask extends PerfTask {
31
32   public ReportTask(PerfRunData runData) {
33     super(runData);
34   }
35
36   /* (non-Javadoc)
37    * @see PerfTask#shouldNeverLogAtStart()
38    */
39   @Override
40   protected boolean shouldNeverLogAtStart() {
41     return true;
42   }
43
44   /* (non-Javadoc)
45    * @see PerfTask#shouldNotRecordStats()
46    */
47   @Override
48   protected boolean shouldNotRecordStats() {
49     return true;
50   }
51
52   /*
53    * From here start the code used to generate the reports. 
54    * Subclasses would use this part to generate reports.
55    */
56   
57   protected static final String newline = System.getProperty("line.separator");
58   
59   /**
60    * Get a textual summary of the benchmark results, average from all test runs.
61    */
62   protected static final String OP =          "Operation  ";
63   protected static final String ROUND =       " round";
64   protected static final String RUNCNT =      "   runCnt";
65   protected static final String RECCNT =      "   recsPerRun";
66   protected static final String RECSEC =      "        rec/s";
67   protected static final String ELAPSED =     "  elapsedSec";
68   protected static final String USEDMEM =     "    avgUsedMem";
69   protected static final String TOTMEM =      "    avgTotalMem";
70   protected static final String COLS[] = {
71       RUNCNT,
72       RECCNT,
73       RECSEC,
74       ELAPSED,
75       USEDMEM,
76       TOTMEM
77   };
78
79   /**
80    * Compute a title line for a report table
81    * @param longestOp size of longest op name in the table
82    * @return the table title line.
83    */
84   protected String tableTitle (String longestOp) {
85     StringBuilder sb = new StringBuilder();
86     sb.append(Format.format(OP,longestOp));
87     sb.append(ROUND);
88     sb.append(getRunData().getConfig().getColsNamesForValsByRound());
89     for (int i = 0; i < COLS.length; i++) {
90       sb.append(COLS[i]);
91     }
92     return sb.toString(); 
93   }
94   
95   /**
96    * find the longest op name out of completed tasks.  
97    * @param taskStats completed tasks to be considered.
98    * @return the longest op name out of completed tasks.
99    */
100   protected String longestOp(Iterable<TaskStats> taskStats) {
101     String longest = OP;
102     for (final TaskStats stat : taskStats) {
103       if (stat.getElapsed()>=0) { // consider only tasks that ended
104         String name = stat.getTask().getName();
105         if (name.length() > longest.length()) {
106           longest = name;
107         }
108       }
109     }
110     return longest;
111   }
112   
113   /**
114    * Compute a report line for the given task stat.
115    * @param longestOp size of longest op name in the table.
116    * @param stat task stat to be printed.
117    * @return the report line.
118    */
119   protected String taskReportLine(String longestOp, TaskStats stat) {
120     PerfTask task = stat.getTask();
121     StringBuilder sb = new StringBuilder();
122     sb.append(Format.format(task.getName(), longestOp));
123     String round = (stat.getRound()>=0 ? ""+stat.getRound() : "-");
124     sb.append(Format.formatPaddLeft(round, ROUND));
125     sb.append(getRunData().getConfig().getColsValuesForValsByRound(stat.getRound()));
126     sb.append(Format.format(stat.getNumRuns(), RUNCNT)); 
127     sb.append(Format.format(stat.getCount() / stat.getNumRuns(), RECCNT));
128     long elapsed = (stat.getElapsed()>0 ? stat.getElapsed() : 1); // assume at least 1ms
129     sb.append(Format.format(2, (float) (stat.getCount() * 1000.0 / elapsed), RECSEC));
130     sb.append(Format.format(2, (float) stat.getElapsed() / 1000, ELAPSED));
131     sb.append(Format.format(0, (float) stat.getMaxUsedMem() / stat.getNumRuns(), USEDMEM)); 
132     sb.append(Format.format(0, (float) stat.getMaxTotMem() / stat.getNumRuns(), TOTMEM));
133     return sb.toString();
134   }
135
136   protected Report genPartialReport(int reported, LinkedHashMap<String,TaskStats> partOfTasks, int totalSize) {
137     String longetOp = longestOp(partOfTasks.values());
138     boolean first = true;
139     StringBuilder sb = new StringBuilder();
140     sb.append(tableTitle(longetOp));
141     sb.append(newline);
142     int lineNum = 0;
143     for (final TaskStats stat : partOfTasks.values()) {
144       if (!first) {
145         sb.append(newline);
146       }
147       first = false;
148       String line = taskReportLine(longetOp,stat);
149       lineNum++;
150       if (partOfTasks.size()>2 && lineNum%2==0) {
151         line = line.replaceAll("   "," - ");
152       }
153       sb.append(line);
154       int[] byTime = stat.getCountsByTime();
155       if (byTime != null) {
156         sb.append(newline);
157         int end = -1;
158         for(int i=byTime.length-1;i>=0;i--) {
159           if (byTime[i] != 0) {
160             end = i;
161             break;
162           }
163         }
164         if (end != -1) {
165           sb.append("  by time:");
166           for(int i=0;i<end;i++) {
167             sb.append(' ').append(byTime[i]);
168           }
169         }
170       }
171     }
172     
173     String reptxt = (reported==0 ? "No Matching Entries Were Found!" : sb.toString());
174     return new Report(reptxt,partOfTasks.size(),reported,totalSize);
175   }
176 }