Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GiraphTimer |
|
| 1.0;1 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
16 | * limitations under the License. | |
17 | */ | |
18 | ||
19 | package org.apache.giraph.metrics; | |
20 | ||
21 | import com.google.common.collect.ImmutableMap; | |
22 | ||
23 | import java.util.concurrent.TimeUnit; | |
24 | ||
25 | /** | |
26 | * A timer to record duration of an event in a given TimeUnit. | |
27 | * GiraphTimer is actually just a single-value Yammer Gauge that has some | |
28 | * methods to make timing things easier. | |
29 | */ | |
30 | public class GiraphTimer extends ValueGauge<Long> { | |
31 | /** Mapping from TimeUnit to abbreviation used for printing */ | |
32 | 0 | private static final ImmutableMap<TimeUnit, String> TIME_UNIT_TO_ABBREV = |
33 | 0 | ImmutableMap.<TimeUnit, String>builder(). |
34 | 0 | put(TimeUnit.DAYS, "days"). |
35 | 0 | put(TimeUnit.HOURS, "hours"). |
36 | 0 | put(TimeUnit.MICROSECONDS, "us"). |
37 | 0 | put(TimeUnit.MILLISECONDS, "ms"). |
38 | 0 | put(TimeUnit.MINUTES, "mins"). |
39 | 0 | put(TimeUnit.NANOSECONDS, "ns"). |
40 | 0 | put(TimeUnit.SECONDS, "secs").build(); |
41 | ||
42 | /** dimension to measure things by */ | |
43 | private TimeUnit timeUnit; | |
44 | ||
45 | /** | |
46 | * Create new timer, add it to the registry. | |
47 | * | |
48 | * @param registry GiraphMetricsRegistry to add timer to | |
49 | * @param name String name of timer | |
50 | * @param timeUnit TimeUnit to measure in | |
51 | */ | |
52 | public GiraphTimer(GiraphMetricsRegistry registry, String name, | |
53 | TimeUnit timeUnit) { | |
54 | 0 | super(registry, name); |
55 | 0 | this.timeUnit = timeUnit; |
56 | 0 | set(0L); |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * Get TimeUnit used. | |
61 | * | |
62 | * @return TimeUnit being used. | |
63 | */ | |
64 | public TimeUnit getTimeUnit() { | |
65 | 0 | return timeUnit; |
66 | } | |
67 | ||
68 | /** | |
69 | * Begin timing an event. | |
70 | * | |
71 | * @return GiraphTimerContext. Use stop() to end timing the event. | |
72 | */ | |
73 | public GiraphTimerContext time() { | |
74 | 0 | return new GiraphTimerContext(this); |
75 | } | |
76 | ||
77 | /** | |
78 | * Set value from a given TimeUnit, converting to our TimeUnit. | |
79 | * | |
80 | * @param value long measurement taken. | |
81 | * @param valueTimeUnit TimeUnit measurement is in. | |
82 | * @return this | |
83 | */ | |
84 | public GiraphTimer set(long value, TimeUnit valueTimeUnit) { | |
85 | 0 | set(timeUnit.convert(value, valueTimeUnit)); |
86 | 0 | return this; |
87 | } | |
88 | ||
89 | /** | |
90 | * Get abbreviated string of TimeUnit. | |
91 | * | |
92 | * @return TimeUnit abbreviation. | |
93 | */ | |
94 | public String getTimeUnitAbbrev() { | |
95 | 0 | return TIME_UNIT_TO_ABBREV.get(timeUnit); |
96 | } | |
97 | ||
98 | /** | |
99 | * Get string representation of value | |
100 | * | |
101 | * @return String value and abbreviated time unit | |
102 | */ | |
103 | public String valueAndUnit() { | |
104 | 0 | return "" + value() + " " + getTimeUnitAbbrev(); |
105 | } | |
106 | } |