1 package org.apache.lucene.benchmark.byTask.stats;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.benchmark.byTask.tasks.PerfTask;
23 * Statistics for a task run.
24 * <br>The same task can run more than once, but, if that task records statistics,
25 * each run would create its own TaskStats.
27 public class TaskStats implements Cloneable {
29 /** task for which data was collected */
30 private PerfTask task;
32 /** round in which task run started */
35 /** task start time */
38 /** task elapsed time. elapsed >= 0 indicates run completion! */
39 private long elapsed = -1;
41 /** max tot mem during task */
42 private long maxTotMem;
44 /** max used mem during task */
45 private long maxUsedMem;
47 /** serial run number of this task run in the perf run */
48 private int taskRunNum;
50 /** number of other tasks that started to run while this task was still running */
51 private int numParallelTasks;
53 /** number of work items done by this task.
54 * For indexing that can be number of docs added.
55 * For warming that can be number of scanned items, etc.
56 * For repeating tasks, this is a sum over repetitions.
60 /** Number of similar tasks aggregated into this record.
61 * Used when summing up on few runs/instances of similar tasks.
63 private int numRuns = 1;
66 * Create a run data for a task that is starting now.
67 * To be called from Points.
69 TaskStats (PerfTask task, int taskRunNum, int round) {
71 this.taskRunNum = taskRunNum;
73 maxTotMem = Runtime.getRuntime().totalMemory();
74 maxUsedMem = maxTotMem - Runtime.getRuntime().freeMemory();
75 start = System.currentTimeMillis();
79 * mark the end of a task
81 void markEnd (int numParallelTasks, int count) {
82 elapsed = System.currentTimeMillis() - start;
83 long totMem = Runtime.getRuntime().totalMemory();
84 if (totMem > maxTotMem) {
87 long usedMem = totMem - Runtime.getRuntime().freeMemory();
88 if (usedMem > maxUsedMem) {
91 this.numParallelTasks = numParallelTasks;
95 private int[] countsByTime;
96 private long countsByTimeStepMSec;
98 public void setCountsByTime(int[] counts, long msecStep) {
99 countsByTime = counts;
100 countsByTimeStepMSec = msecStep;
103 public int[] getCountsByTime() {
107 public long getCountsByTimeStepMSec() {
108 return countsByTimeStepMSec;
112 * @return the taskRunNum.
114 public int getTaskRunNum() {
119 * @see java.lang.Object#toString()
122 public String toString() {
123 StringBuilder res = new StringBuilder(task.getName());
128 return res.toString();
132 * @return Returns the count.
134 public int getCount() {
139 * @return elapsed time.
141 public long getElapsed() {
146 * @return Returns the maxTotMem.
148 public long getMaxTotMem() {
153 * @return Returns the maxUsedMem.
155 public long getMaxUsedMem() {
160 * @return Returns the numParallelTasks.
162 public int getNumParallelTasks() {
163 return numParallelTasks;
167 * @return Returns the task.
169 public PerfTask getTask() {
174 * @return Returns the numRuns.
176 public int getNumRuns() {
181 * Add data from another stat, for aggregation
182 * @param stat2 the added stat data.
184 public void add(TaskStats stat2) {
185 numRuns += stat2.getNumRuns();
186 elapsed += stat2.getElapsed();
187 maxTotMem += stat2.getMaxTotMem();
188 maxUsedMem += stat2.getMaxUsedMem();
189 count += stat2.getCount();
190 if (round != stat2.round) {
191 round = -1; // no meaning if aggregating tasks of different round.
194 if (countsByTime != null && stat2.countsByTime != null) {
195 if (countsByTimeStepMSec != stat2.countsByTimeStepMSec) {
196 throw new IllegalStateException("different by-time msec step");
198 if (countsByTime.length != stat2.countsByTime.length) {
199 throw new IllegalStateException("different by-time msec count");
201 for(int i=0;i<stat2.countsByTime.length;i++) {
202 countsByTime[i] += stat2.countsByTime[i];
208 * @see java.lang.Object#clone()
211 public Object clone() throws CloneNotSupportedException {
212 TaskStats c = (TaskStats) super.clone();
213 if (c.countsByTime != null) {
214 c.countsByTime = c.countsByTime.clone();
220 * @return the round number.
222 public int getRound() {