[1119] | 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 | |
---|
| 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
| 16 | |
---|
| 17 | import java.io.PrintStream; |
---|
| 18 | import java.text.DecimalFormat; |
---|
| 19 | import java.util.HashMap; |
---|
| 20 | import java.util.LinkedList; |
---|
| 21 | import java.util.List; |
---|
| 22 | import java.util.Map; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * <p> |
---|
| 26 | * TODO comment |
---|
| 27 | * </p> |
---|
| 28 | * |
---|
| 29 | * @author Patrick Harms |
---|
| 30 | */ |
---|
| 31 | public class StopWatch { |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * |
---|
| 35 | */ |
---|
| 36 | private Map<String, List<Long>> mWatches = new HashMap<String, List<Long>>(); |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * |
---|
| 40 | * |
---|
| 41 | * @param id |
---|
| 42 | */ |
---|
| 43 | public void start(String id) { |
---|
| 44 | List<Long> timeStamps = mWatches.get(id); |
---|
| 45 | |
---|
| 46 | if (timeStamps == null) { |
---|
| 47 | timeStamps = new LinkedList<Long>(); |
---|
| 48 | mWatches.put(id, timeStamps); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | if (timeStamps.size() % 2 == 0) { |
---|
| 52 | timeStamps.add(System.currentTimeMillis()); |
---|
| 53 | } |
---|
| 54 | else { |
---|
| 55 | throw new IllegalStateException("watch with id " + id + " already running"); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * |
---|
| 61 | * |
---|
| 62 | * @param id |
---|
| 63 | */ |
---|
| 64 | public void stop(String id) { |
---|
| 65 | List<Long> timeStamps = mWatches.get(id); |
---|
| 66 | |
---|
| 67 | if (timeStamps == null) { |
---|
| 68 | throw new IllegalArgumentException("watch with id " + id + " does not exist"); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (timeStamps.size() % 2 == 0) { |
---|
| 72 | throw new IllegalStateException("watch with id " + id + " already stopped"); |
---|
| 73 | } |
---|
| 74 | else { |
---|
| 75 | timeStamps.add(System.currentTimeMillis()); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * |
---|
| 81 | * |
---|
| 82 | * @param id |
---|
| 83 | */ |
---|
| 84 | public long getDuration(String id) { |
---|
| 85 | List<Long> timeStamps = mWatches.get(id); |
---|
| 86 | |
---|
| 87 | if (timeStamps == null) { |
---|
| 88 | throw new IllegalArgumentException("watch with id " + id + " does not exist"); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if (timeStamps.size() % 2 != 0) { |
---|
| 92 | stop(id); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | long result = 0; |
---|
| 96 | for (long timeStamp : timeStamps) { |
---|
| 97 | if (result >= 0) { |
---|
| 98 | result -= timeStamp; |
---|
| 99 | } |
---|
| 100 | else { |
---|
| 101 | result += timeStamp; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return result; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * |
---|
| 110 | * @param out |
---|
| 111 | */ |
---|
| 112 | public void dumpStatistics(PrintStream out) { |
---|
| 113 | if (mWatches.size() <= 0) { |
---|
| 114 | throw new IllegalStateException("no watches registered that could be dumped"); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | Map<String, Long> durations = new HashMap<String, Long>(); |
---|
| 118 | |
---|
| 119 | // get durations |
---|
| 120 | for (String id : mWatches.keySet()) { |
---|
| 121 | durations.put(id, getDuration(id)); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // sort by duration |
---|
| 125 | List<String> sortedIds = new LinkedList<String>(); |
---|
| 126 | int maxIdLength = 0; |
---|
| 127 | |
---|
| 128 | for (Map.Entry<String, Long> entry : durations.entrySet()) { |
---|
| 129 | boolean added = false; |
---|
| 130 | for (int i = 0; i < sortedIds.size(); i++) { |
---|
| 131 | if (durations.get(sortedIds.get(i)) >= entry.getValue()) { |
---|
| 132 | sortedIds.add(i, entry.getKey()); |
---|
| 133 | added = true; |
---|
| 134 | break; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | if (!added) { |
---|
| 139 | sortedIds.add(entry.getKey()); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | maxIdLength = Math.max(maxIdLength, entry.getKey().length()); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | // get longest duration and check whether it spans all other entries |
---|
| 146 | String id = sortedIds.get(sortedIds.size() - 1); |
---|
| 147 | List<Long> timeStamps = mWatches.get(id); |
---|
| 148 | long firstTimeStamp = timeStamps.get(0); |
---|
| 149 | long lastTimeStamp = timeStamps.get(timeStamps.size() - 1); |
---|
| 150 | long longestDuration = durations.get(id); |
---|
| 151 | boolean longestDurationCoversOthers = true; |
---|
| 152 | |
---|
| 153 | for (Map.Entry<String, List<Long>> watch : mWatches.entrySet()) { |
---|
| 154 | if ((watch.getValue().get(0) < firstTimeStamp) || |
---|
| 155 | (watch.getValue().get(watch.getValue().size() - 1) > lastTimeStamp)) |
---|
| 156 | { |
---|
| 157 | longestDurationCoversOthers = false; |
---|
| 158 | break; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | // no finally start the dumping |
---|
| 163 | out.println(); |
---|
| 164 | out.println("Watch Statistics"); |
---|
| 165 | out.println("================"); |
---|
| 166 | |
---|
| 167 | for (String sortedId : sortedIds) { |
---|
| 168 | out.print(sortedId); |
---|
| 169 | |
---|
| 170 | for (int i = sortedId.length(); i <= maxIdLength; i++) { |
---|
| 171 | out.print(' '); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | out.print(':'); |
---|
| 175 | out.print(' '); |
---|
| 176 | |
---|
| 177 | out.print(durations.get(sortedId)); |
---|
| 178 | out.print(" ms"); |
---|
| 179 | |
---|
| 180 | if (longestDurationCoversOthers) { |
---|
| 181 | out.print(" ("); |
---|
| 182 | out.print(DecimalFormat.getPercentInstance().format |
---|
| 183 | ((double) durations.get(sortedId) / longestDuration)); |
---|
| 184 | out.print(" of overall duration)"); |
---|
| 185 | } |
---|
| 186 | out.println(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | out.println(); |
---|
| 190 | } |
---|
| 191 | } |
---|