Skip to content

Instantly share code, notes, and snippets.

@g4s8
Created August 3, 2017 14:41
Show Gist options
  • Save g4s8/e36cdd9ccdc726e348bb7ecfbaf2d109 to your computer and use it in GitHub Desktop.
Save g4s8/e36cdd9ccdc726e348bb7ecfbaf2d109 to your computer and use it in GitHub Desktop.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.Closeable;
import java.util.*;
public final class Tracer implements Closeable {
private static List<StackTraceElement> cut(final List<StackTraceElement> trace, final int depth) {
List<StackTraceElement> filtered = trace.subList(2, trace.size());
return filtered.size() > depth ? filtered.subList(0, depth) : filtered;
}
public static Tracer record(final int depth) {
final List<StackTraceElement> trace = cut(Arrays.asList(Thread.currentThread().getStackTrace()), depth);
final long millis = System.currentTimeMillis();
return new Tracer(millis, trace);
}
private static final Map<List<StackTraceElement>, List<Long>> timings = new HashMap<>();
private final long start;
private final List<StackTraceElement> trace;
private Tracer(long start, List<StackTraceElement> trace) {
this.start = start;
this.trace = trace;
}
@Override
public void close() {
final long millis = System.currentTimeMillis();
synchronized (Tracer.timings) {
final List<Long> times;
if (!Tracer.timings.containsKey(this.trace)) {
times = new LinkedList<>();
Tracer.timings.put(this.trace, times);
} else {
times = Tracer.timings.get(this.trace);
}
times.add(millis - this.start);
}
}
public static void print() {
synchronized (Tracer.timings) {
timings.entrySet().forEach(kv -> print(kv.getKey(), kv.getValue()));
}
}
private static void print(final List<StackTraceElement> trace, final List<Long> times) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
trace.forEach(tr -> System.out.println(tr.toString()));
final long min = times.stream().mapToLong(Tracer::id).min().getAsLong();
final long max = times.stream().mapToLong(Tracer::id).max().getAsLong();
final double avg = times.stream().mapToLong(Tracer::id).average().getAsDouble();
final int count = times.size();
System.out.printf(Locale.US, "\tSUM: min/max/avg/cnt: %d/%d/%f/%d\n", min, max, avg, count);
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
private static <X> X id(X x) {
return x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment