Skip to content

Commit 655f627

Browse files
vjkoskelaBrandonArp
authored andcommitted
Minor optimizations in periodic statistics sink. Aside, we need to replace the hprof based perf testing not because its being deprecated with jdk9, but because it's not particularly precise. (#73)
1 parent 68a6eb5 commit 655f627

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/main/java/com/arpnetworking/tsdcore/sinks/PeriodicStatisticsSink.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
import com.arpnetworking.tsdcore.model.AggregatedData;
2626
import com.arpnetworking.tsdcore.model.PeriodicData;
2727
import com.fasterxml.jackson.annotation.JacksonInject;
28+
import com.google.common.collect.ImmutableMap;
29+
import com.google.common.collect.ImmutableSet;
2830
import com.google.common.collect.Maps;
2931
import com.google.common.collect.Sets;
3032
import net.sf.oval.constraint.Min;
3133
import net.sf.oval.constraint.NotNull;
34+
import org.joda.time.Period;
3235

3336
import java.util.Collections;
3437
import java.util.Map;
@@ -40,6 +43,7 @@
4043
import java.util.concurrent.atomic.AtomicLong;
4144
import java.util.concurrent.atomic.AtomicReference;
4245
import java.util.concurrent.atomic.LongAccumulator;
46+
import javax.annotation.Nullable;
4347

4448
/**
4549
* Aggregates and periodically logs metrics about the aggregated data being
@@ -83,8 +87,8 @@ public void recordAggregateData(final PeriodicData periodicData) {
8387
.append(periodicData.getDimensions().getHost()).append(".")
8488
.append(periodicData.getDimensions().getService()).append(".")
8589
.append(metricName).append(".")
86-
.append(datum.getStatistic()).append(".")
87-
.append(periodicData.getPeriod())
90+
.append(datum.getStatistic().getName()).append(".")
91+
.append(getPeriodAsString(periodicData.getPeriod()))
8892
.toString();
8993

9094
final String serviceMetric = new StringBuilder()
@@ -165,6 +169,17 @@ private Set<String> createConcurrentSet(final Set<String> existingSet) {
165169
return Collections.newSetFromMap(new ConcurrentHashMap<>(initialCapacity));
166170
}
167171

172+
private static String getPeriodAsString(final Period period) {
173+
// TODO(ville): This is the only use of period serialization in MAD (weird, eh?)
174+
// However, we should consider generalizing and moving this to commons.
175+
176+
final @Nullable String periodAsString = CACHED_PERIOD_STRINGS.get(period);
177+
if (periodAsString == null) {
178+
return period.toString();
179+
}
180+
return periodAsString;
181+
}
182+
168183
// NOTE: Package private for testing
169184
/* package private */ PeriodicStatisticsSink(final Builder builder, final ScheduledExecutorService executor) {
170185
super(builder);
@@ -212,6 +227,27 @@ private PeriodicStatisticsSink(final Builder builder) {
212227

213228
private static final Logger LOGGER = LoggerFactory.getLogger(PeriodicStatisticsSink.class);
214229
private static final int EXECUTOR_TIMEOUT_IN_SECONDS = 30;
230+
private static final ImmutableMap<Period, String> CACHED_PERIOD_STRINGS;
231+
232+
static {
233+
final ImmutableSet<Period> periods = ImmutableSet.<Period>builder()
234+
.add(Period.seconds(1))
235+
.add(Period.minutes(1))
236+
.add(Period.minutes(2))
237+
.add(Period.minutes(5))
238+
.add(Period.minutes(10))
239+
.add(Period.minutes(15))
240+
.add(Period.hours(1))
241+
.add(Period.days(1))
242+
.add(Period.months(1))
243+
.add(Period.years(1))
244+
.build();
245+
final ImmutableMap.Builder<Period, String> builder = ImmutableMap.builder();
246+
for (final Period period : periods) {
247+
builder.put(period, period.toString());
248+
}
249+
CACHED_PERIOD_STRINGS = builder.build();
250+
}
215251

216252
private final class MetricsLogger implements Runnable {
217253

0 commit comments

Comments
 (0)