more cleanup

This commit is contained in:
Sander Hautvast 2018-04-06 16:44:13 +02:00
parent 47a88fac4a
commit ec23bbd64c
3 changed files with 26 additions and 12 deletions

View file

@ -15,10 +15,17 @@ import java.util.List;
public class Agent { public class Agent {
public static final String MESSAGE = "Perfix agent active";
public static void premain(String agentArgs, Instrumentation inst) { public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Perfix agent active"); System.out.println(MESSAGE);
startListeningOnSocket(); startListeningOnSocket();
instrumentCode(inst);
}
private static void instrumentCode(Instrumentation inst) {
List<String> includes = determineIncludes(); List<String> includes = determineIncludes();
inst.addTransformer((classLoader, resource, aClass, protectionDomain, uninstrumentedByteCode) -> { inst.addTransformer((classLoader, resource, aClass, protectionDomain, uninstrumentedByteCode) -> {
@ -30,7 +37,6 @@ public class Agent {
} }
} catch (Exception ex) { } catch (Exception ex) {
//suppress //suppress
} }
} }
return uninstrumentedByteCode; return uninstrumentedByteCode;

View file

@ -5,7 +5,7 @@ public class Method {
private final String name; private final String name;
private long t1; private long t1;
public Method(String name) { private Method(String name) {
t0 = System.nanoTime(); t0 = System.nanoTime();
this.name = name; this.name = name;
} }

View file

@ -8,27 +8,35 @@ import java.util.concurrent.atomic.LongAdder;
public class Registry { public class Registry {
static final Map<String, List<Method>> methods = new ConcurrentHashMap<>(); private static final Map<String, List<Method>> methods = new ConcurrentHashMap<>();
private static final double NANO_2_MILLI = 1000000D; private static final double NANO_2_MILLI = 1000000D;
private static final String HEADER1 = "Invoked methods, by duration desc:";
private static final String HEADER2 = "Method name;#Invocations;Total duration;Average Duration";
private static final String FOOTER = "----------------------------------------";
static void add(Method method) { static void add(Method method) {
methods.computeIfAbsent(method.getName(), key -> new ArrayList<>()).add(method); methods.computeIfAbsent(method.getName(), key -> new ArrayList<>()).add(method);
} }
public static void report(PrintStream out) { public static void report(PrintStream out) {
out.println("Invoked methods, by duration desc:"); out.println(HEADER1);
out.println("Method name;#Invocations;Total duration;Average Duration"); out.println(HEADER2);
sortedMethodsByDuration().forEach((k, report) -> { sortedMethodsByDuration().entrySet().stream()
out.println(report.name + ";" + report.invocations + ";" + (long)(report.totalDuration / NANO_2_MILLI) + ";" + (long)(report.average() / NANO_2_MILLI)); .map(entry -> createReportLine(entry.getValue()))
}); .forEach(out::println);
out.println("----------------------------------------"); out.println(FOOTER);
}
private static String createReportLine(Report report) {
return report.name + ";"
+ report.invocations + ";"
+ (long) (report.totalDuration / NANO_2_MILLI) + ";"
+ (long) (report.average() / NANO_2_MILLI);
} }
private static SortedMap<Long, Report> sortedMethodsByDuration() { private static SortedMap<Long, Report> sortedMethodsByDuration() {
SortedMap<Long, Report> sortedByTotal = new ConcurrentSkipListMap<>(Comparator.reverseOrder()); SortedMap<Long, Report> sortedByTotal = new ConcurrentSkipListMap<>(Comparator.reverseOrder());
methods.forEach((name, measurements) -> { methods.forEach((name, measurements) -> {
// long totalDuration = measurements.stream().mapToLong(Method::getDuration).sum();
LongAdder totalDuration = new LongAdder(); LongAdder totalDuration = new LongAdder();
measurements.stream() measurements.stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)