| 1 | /* __copyright_begin__ |
| 2 | Copyright 2011 Dan Hagberg |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | __copyright_end__ */ |
| 16 | /** |
| 17 | * |
| 18 | */ |
| 19 | package net.digitaltsunami.tmeter.action; |
| 20 | |
| 21 | import java.util.Collection; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.ConcurrentHashMap; |
| 25 | |
| 26 | import net.digitaltsunami.tmeter.Timer; |
| 27 | import net.digitaltsunami.tmeter.TimerBasicStatistics; |
| 28 | |
| 29 | /** |
| 30 | * An implementation of {@link TimerAction} that provides basic statistics for all |
| 31 | * timers processed by this action. Basic statistics may be retrieved for a specific |
| 32 | * task or the entire set of tasks. |
| 33 | * @author dhagberg |
| 34 | * |
| 35 | */ |
| 36 | public class TimerStatsAction extends TimerAction { |
| 37 | |
| 38 | private final ConcurrentHashMap<String, TimerBasicStatistics> statsByTask; |
| 39 | |
| 40 | public TimerStatsAction() { |
| 41 | super(); |
| 42 | statsByTask = new ConcurrentHashMap<String, TimerBasicStatistics>(); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * (non-Javadoc) |
| 47 | * |
| 48 | * @see net.digitaltsunami.tmeter.TimeAction#timerCompleted(net.digitaltsunami.tmeter.Timer) |
| 49 | */ |
| 50 | @Override |
| 51 | protected void processTimer(Timer timer) { |
| 52 | TimerBasicStatistics stat = statsByTask.get(timer.getTaskName()); |
| 53 | if (stat == null) { |
| 54 | // Task not found in list. Create a new entry |
| 55 | stat = new TimerBasicStatistics(timer.getTaskName()); |
| 56 | // Place it in the map |
| 57 | TimerBasicStatistics currentStat = statsByTask.putIfAbsent(timer.getTaskName(), |
| 58 | stat); |
| 59 | // If the same entry was not returned, another thread created |
| 60 | // during setup. Use the existing entry. |
| 61 | if (currentStat != null && currentStat != stat) { |
| 62 | stat = currentStat; |
| 63 | } |
| 64 | } |
| 65 | stat.addTimer(timer); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Clears all accumulated statistics. |
| 70 | * |
| 71 | * @see net.digitaltsunami.tmeter.TimeAction#reset() |
| 72 | */ |
| 73 | @Override |
| 74 | protected void reset() { |
| 75 | statsByTask.clear(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return a live instance of {@link TimerBasicStatistics} for the provided |
| 80 | * task name. Internal values will be modified as this action processes |
| 81 | * timers. |
| 82 | * |
| 83 | * @param taskName |
| 84 | * @return Live instance of {@link TimerBasicStatistics} or null if that |
| 85 | * task has not yet been processed by this action. |
| 86 | */ |
| 87 | public TimerBasicStatistics getTimerStatistics(String taskName) { |
| 88 | return statsByTask.get(taskName); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return a snapshot instance of {@link TimerBasicStatistics} for the |
| 93 | * provided task name. Internal values will be not be modified as this |
| 94 | * action processes timers. |
| 95 | * |
| 96 | * @param taskName |
| 97 | * @return Snapshot of {@link TimerBasicStatistics} for task name or null if |
| 98 | * that task has not yet been processed by this action. |
| 99 | */ |
| 100 | public TimerBasicStatistics getTimerStatisticsSnapshot(String taskName) { |
| 101 | TimerBasicStatistics stat = statsByTask.get(taskName); |
| 102 | return stat == null ? null : stat.getSnapshot(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Return a collection of live instances of {@link TimerBasicStatistics} for |
| 107 | * all tasks processed up to the point of invoking this method. Internal |
| 108 | * values will be modified as this action processes timers. |
| 109 | * |
| 110 | * @return Live instance of {@link TimerBasicStatistics} |
| 111 | */ |
| 112 | public Collection<TimerBasicStatistics> getAllTimerStatistics() { |
| 113 | return new HashSet<TimerBasicStatistics>(statsByTask.values()); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return a collection of snapshot instances of {@link TimerBasicStatistics} |
| 119 | * for all tasks processed up to the point of invoking this method. Internal |
| 120 | * values will be modified as this action processes timers. |
| 121 | * |
| 122 | * @return Snapshot instance of {@link TimerBasicStatistics} |
| 123 | */ |
| 124 | public Collection<TimerBasicStatistics> getAllTimerStatisticsSnapshot() { |
| 125 | // Create a working copy to avoid concurrent modification exception while building copy. |
| 126 | Set<TimerBasicStatistics> workingCopy = new HashSet<TimerBasicStatistics>(statsByTask.values()); |
| 127 | Set<TimerBasicStatistics> snapshotSet = new HashSet<TimerBasicStatistics>(workingCopy.size()); |
| 128 | for(TimerBasicStatistics stat : workingCopy) { |
| 129 | snapshotSet.add(stat.getSnapshot()); |
| 130 | } |
| 131 | return snapshotSet; |
| 132 | } |
| 133 | |
| 134 | } |