source: trunk/java-utils/src/main/java/de/ugoe/cs/util/StopWatch.java @ 1236

Last change on this file since 1236 was 1128, checked in by pharms, 12 years ago
  • added a watch for performance measurements
File size: 9.7 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util;
16
17import java.io.PrintStream;
18import java.text.DecimalFormat;
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23
24/**
25 * <p>
26 * This is a simple implementation for a stop watch that can be used for performance measures.
27 * A stop watch can be used to measure several splits. Each split is started and stopped using the
28 * same id provided to the {@link #start(String)} and {@link #stop(String)} methods. The measured
29 * durations can be retrieved afterwards using {@link #getDuration(String)}.
30 * {@link #dumpStatistics(PrintStream)} is a convenience method useful to effectively dump all
31 * information for the different splits.
32 * </p>
33 *
34 * @author Patrick Harms
35 */
36public class StopWatch {
37   
38    /**
39     * the splits hold internally
40     */
41    private Map<String, Split> mSplits = new HashMap<String, Split>();
42
43    /**
44     * starts a split with the given id. If the split with the id is already started, an
45     * {@link IllegalStateException} is thrown.
46     *
47     * @param id the id of the split to be started
48     *
49     * @throws IllegalStateException if the split is already started
50     */
51    public void start(String id) throws IllegalStateException {
52        Split split = mSplits.get(id);
53       
54        if (split == null) {
55            split = new Split(id);
56            mSplits.put(id, split);
57        }
58       
59        split.start();
60    }
61   
62    /**
63     * stops a split with the given id. If the split with the id is already stopped, an
64     * {@link IllegalStateException} is thrown. If no split with the given id exists, an
65     * {@link IllegalArgumentException} is thrown.
66     *
67     * @param id the id of the split to be stopped
68     *
69     * @throws IllegalStateException    if the split is not running
70     * @throws IllegalArgumentException if the split with the given id does not exist
71     */
72    public void stop(String id) throws IllegalStateException, IllegalArgumentException {
73        Split split = mSplits.get(id);
74       
75        if (split == null) {
76            throw new IllegalArgumentException("split with id " + id + " does not exist");
77        }
78       
79        split.stop();
80    }
81   
82    /**
83     * returns the duration of a split with the given id. If the split with the id is currently
84     * running, it is stopped. If no split with the given id exists, an
85     * {@link IllegalArgumentException} is thrown.
86     *
87     * @param id the id of the split for which the duration shall be returned
88     *
89     * @return the duration measured for the split
90     *
91     * @throws IllegalArgumentException if the split with the given id does not exist
92     */
93    public long getDuration(String id) throws IllegalArgumentException {
94        Split split = mSplits.get(id);
95       
96        if (split == null) {
97            throw new IllegalArgumentException("split with id " + id + " does not exist");
98        }
99       
100        if (split.isRunning()) {
101            split.stop();
102        }
103       
104        return split.getDuration();
105    }
106
107    /**
108     * resets the stop watch and clears all splits
109     */
110    public void reset() {
111        mSplits.clear();
112    }
113   
114    /**
115     * dumps the statistics about the splits. Splits still running are stopped. The method checks
116     * if the longest split also covers the other splits. If so, it considers this split as a
117     * kind of overall duration and dumps the proportion of all other splits to this split in
118     * percentage.
119     *
120     * @param out the stream to dump the statistics to
121     */
122    public void dumpStatistics(PrintStream out) {
123        if (mSplits.size() <= 0) {
124            throw new IllegalStateException("no splits registered that could be dumped");
125        }
126       
127        Map<String, Long> durations = new HashMap<String, Long>();
128       
129        // get durations
130        for (String id : mSplits.keySet()) {
131            durations.put(id, getDuration(id));
132        }
133       
134        // sort by duration
135        List<String> sortedIds = new LinkedList<String>();
136        int maxIdLength = 0;
137       
138        for (Map.Entry<String, Long> entry : durations.entrySet()) {
139            boolean added = false;
140            for (int i = 0; i < sortedIds.size(); i++) {
141                if (durations.get(sortedIds.get(i)) >= entry.getValue()) {
142                    sortedIds.add(i, entry.getKey());
143                    added = true;
144                    break;
145                }
146            }
147           
148            if (!added) {
149                sortedIds.add(entry.getKey());
150            }
151           
152            maxIdLength = Math.max(maxIdLength, entry.getKey().length());
153        }
154       
155        // get longest duration and check whether it spans all other entries
156        String id = sortedIds.get(sortedIds.size() - 1);
157        Split longestWatch = mSplits.get(id);
158        boolean longestDurationCoversOthers = true;
159       
160        for (Map.Entry<String, Split> watch : mSplits.entrySet()) {
161            if ((watch.getValue().getFirstStart() < longestWatch.getFirstStart()) ||
162                (watch.getValue().getLastStop() > longestWatch.getLastStop()))
163            {
164                longestDurationCoversOthers = false;
165                break;
166            }
167        }
168       
169        // no finally start the dumping
170        out.println();
171        out.println("Watch Statistics");
172        out.println("================");
173
174        for (String sortedId : sortedIds) {
175            out.print(sortedId);
176           
177            for (int i = sortedId.length(); i <= maxIdLength; i++) {
178                out.print(' ');
179            }
180           
181            out.print(": ");
182           
183            out.print(durations.get(sortedId));
184            out.print(" ms");
185           
186            out.print(" (");
187            out.print(mSplits.get(sortedId).getNoOfStarts());
188            out.print(" starts");
189           
190            //out.print(", ");
191            //out.print(1000 * durations.get(sortedId) / mSplits.get(sortedId).getNoOfStarts());
192            //out.print(" ms per 1000 starts");
193
194            if (longestDurationCoversOthers) {
195                out.print(", ");
196                out.print(DecimalFormat.getPercentInstance().format
197                              ((double) durations.get(sortedId) / longestWatch.getDuration()));
198                out.print(" of overall duration");
199            }
200           
201            out.println(')');
202        }
203       
204        out.println();
205    }
206   
207    /**
208     * internally used to store splits
209     */
210    private static class Split {
211       
212        /**
213         * the id of the split
214         */
215        private String id;
216       
217        /**
218         * the system time of the first start of the split
219         */
220        private long firstStart = -1;
221       
222        /**
223         * the system time of the last start of the split
224         */
225        private long lastStart = -1;
226       
227        /**
228         * the system time of the last stop of the split
229         */
230        private long lastStop = -1;
231       
232        /**
233         * the duration so far for the split (excluding the time since the last start)
234         */
235        private long duration = 0;
236       
237        /**
238         * the number of starts or the splits
239         */
240        private long noOfStarts = 0;
241       
242        /**
243         * initializes the split with its id
244         */
245        private Split(String id) {
246            this.id = id;
247        }
248       
249        /**
250         * starts the split if it is not already started
251         */
252        private void start() throws IllegalStateException {
253            if (lastStart > -1) {
254                throw new IllegalStateException("split with id " + id + " already running");
255            }
256           
257            lastStart = System.currentTimeMillis();
258           
259            if (firstStart < 0) {
260                firstStart = lastStart;
261            }
262           
263            noOfStarts++;
264        }
265       
266        /**
267         * checks if the split is currently running
268         */
269        private boolean isRunning() {
270            return (lastStart > -1);
271        }
272       
273        /**
274         * stops the split if it is not already stopped
275         */
276        private void stop() throws IllegalStateException {
277            if (lastStart < 0) {
278                throw new IllegalStateException("split with id " + id + " not running");
279            }
280           
281            lastStop = System.currentTimeMillis();
282            duration += lastStop - lastStart;
283            lastStart = -1;
284        }
285       
286        /**
287         * returns the fist start of the split
288         */
289        private long getFirstStart() {
290            return firstStart;
291        }
292
293        /**
294         * returns the last stop of the split
295         */
296        private long getLastStop() {
297            return lastStop;
298        }
299
300        /**
301         * returns the duration of the split measured so far excluding the time since the last
302         * start if the split is currently started
303         */
304        private long getDuration() {
305            return duration;
306        }
307
308        /**
309         * returns the number of starts for the split
310         */
311        private long getNoOfStarts() {
312            return noOfStarts;
313        }
314    }
315}
Note: See TracBrowser for help on using the repository browser.