add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / stats / TaskStats.java
1 package org.apache.lucene.benchmark.byTask.stats;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.benchmark.byTask.tasks.PerfTask;
21
22 /**
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.
26  */
27 public class TaskStats implements Cloneable {
28
29   /** task for which data was collected */
30   private PerfTask task; 
31
32   /** round in which task run started */
33   private int round;
34
35   /** task start time */
36   private long start;
37   
38   /** task elapsed time.  elapsed >= 0 indicates run completion! */
39   private long elapsed = -1;
40   
41   /** max tot mem during task */
42   private long maxTotMem;
43   
44   /** max used mem during task */
45   private long maxUsedMem;
46   
47   /** serial run number of this task run in the perf run */
48   private int taskRunNum;
49   
50   /** number of other tasks that started to run while this task was still running */ 
51   private int numParallelTasks;
52   
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.
57    */
58   private int count;
59
60   /** Number of similar tasks aggregated into this record.   
61    * Used when summing up on few runs/instances of similar tasks.
62    */
63   private int numRuns = 1;
64   
65   /**
66    * Create a run data for a task that is starting now.
67    * To be called from Points.
68    */
69   TaskStats (PerfTask task, int taskRunNum, int round) {
70     this.task = task;
71     this.taskRunNum = taskRunNum;
72     this.round = round;
73     maxTotMem = Runtime.getRuntime().totalMemory();
74     maxUsedMem = maxTotMem - Runtime.getRuntime().freeMemory();
75     start = System.currentTimeMillis();
76   }
77   
78   /**
79    * mark the end of a task
80    */
81   void markEnd (int numParallelTasks, int count) {
82     elapsed = System.currentTimeMillis() - start;
83     long totMem = Runtime.getRuntime().totalMemory();
84     if (totMem > maxTotMem) {
85       maxTotMem = totMem;
86     }
87     long usedMem = totMem - Runtime.getRuntime().freeMemory();
88     if (usedMem > maxUsedMem) {
89       maxUsedMem = usedMem;
90     }
91     this.numParallelTasks = numParallelTasks;
92     this.count = count;
93   }
94   
95   private int[] countsByTime;
96   private long countsByTimeStepMSec;
97
98   public void setCountsByTime(int[] counts, long msecStep) {
99     countsByTime = counts;
100     countsByTimeStepMSec = msecStep;
101   }
102
103   public int[] getCountsByTime() {
104     return countsByTime;
105   }
106
107   public long getCountsByTimeStepMSec() {
108     return countsByTimeStepMSec;
109   }
110
111   /**
112    * @return the taskRunNum.
113    */
114   public int getTaskRunNum() {
115     return taskRunNum;
116   }
117
118   /* (non-Javadoc)
119    * @see java.lang.Object#toString()
120    */
121   @Override
122   public String toString() {
123     StringBuilder res = new StringBuilder(task.getName());
124     res.append(" ");
125     res.append(count);
126     res.append(" ");
127     res.append(elapsed);
128     return res.toString();
129   }
130
131   /**
132    * @return Returns the count.
133    */
134   public int getCount() {
135     return count;
136   }
137
138   /**
139    * @return elapsed time.
140    */
141   public long getElapsed() {
142     return elapsed;
143   }
144
145   /**
146    * @return Returns the maxTotMem.
147    */
148   public long getMaxTotMem() {
149     return maxTotMem;
150   }
151
152   /**
153    * @return Returns the maxUsedMem.
154    */
155   public long getMaxUsedMem() {
156     return maxUsedMem;
157   }
158
159   /**
160    * @return Returns the numParallelTasks.
161    */
162   public int getNumParallelTasks() {
163     return numParallelTasks;
164   }
165
166   /**
167    * @return Returns the task.
168    */
169   public PerfTask getTask() {
170     return task;
171   }
172
173   /**
174    * @return Returns the numRuns.
175    */
176   public int getNumRuns() {
177     return numRuns;
178   }
179
180   /**
181    * Add data from another stat, for aggregation
182    * @param stat2 the added stat data.
183    */
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. 
192     }
193
194     if (countsByTime != null && stat2.countsByTime != null) {
195       if (countsByTimeStepMSec != stat2.countsByTimeStepMSec) {
196         throw new IllegalStateException("different by-time msec step");
197       }
198       if (countsByTime.length != stat2.countsByTime.length) {
199         throw new IllegalStateException("different by-time msec count");
200       }
201       for(int i=0;i<stat2.countsByTime.length;i++) {
202         countsByTime[i] += stat2.countsByTime[i];
203       }
204     }
205   }
206
207   /* (non-Javadoc)
208    * @see java.lang.Object#clone()
209    */
210   @Override
211   public Object clone() throws CloneNotSupportedException {
212     TaskStats c = (TaskStats) super.clone();
213     if (c.countsByTime != null) {
214       c.countsByTime = c.countsByTime.clone();
215     }
216     return c;
217   }
218
219   /**
220    * @return the round number.
221    */
222   public int getRound() {
223     return round;
224   }
225   
226 }