Skip to content

Commit

Permalink
Merge pull request #16 from wbarantt/wbaran/fixed_wrong_calculation_o…
Browse files Browse the repository at this point in the history
…f_execution_time

Fixed the wrong calculation of methodExecutionTime
  • Loading branch information
pdebicki authored Jul 6, 2018
2 parents baebc14 + a8cce31 commit a564fc8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017 TomTom International B.V.
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

package com.tomtom.james.newagent;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.function.Supplier;

public final class MethodExecutionTimeHelper {

private static ThreadLocal<ArrayDeque<Long>> startTimeStack = ThreadLocal.withInitial(new Supplier<ArrayDeque<Long>>() {
@Override
public ArrayDeque<Long> get() {
return new ArrayDeque<Long>(8);
}
});

public static long executionStarted() {
final long startTime = System.nanoTime();
startTimeStack.get().push(startTime);
return startTime;
}

public static long getStartTime() {
long startTime = startTimeStack.get().peek();
return startTime;
}

public static long executionFinished() {
long startTime = startTimeStack.get().pop();
return startTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ private String escapeScriptString(String script) {
}

protected void insertBefore(CtMethod method, ExtendedInformationPoint informationPoint) throws CannotCompileException {
method.addLocalVariable("_startTime", CtClass.longType);
StringBuilder s = new StringBuilder("");
s.append(" _startTime = System.nanoTime();"); // we put nanoTime into local variable ...
s.append(" com.tomtom.james.newagent.GlobalValueStore.put(\"" + informationPoint + "\", _startTime); "); // and into the GlobalValueStore in case of Exception, because in finally block local variable is not visible
s.append(" com.tomtom.james.newagent.MethodExecutionTimeHelper.executionStarted();");
method.insertBefore(s.toString());
}

protected void insertAfter(CtMethod method, ExtendedInformationPoint informationPoint) throws CannotCompileException {
String script = escapeScriptString(informationPoint.getScript().get());
StringBuilder s = new StringBuilder();
s.append(" com.tomtom.james.informationpoint.advice.ContextAwareAdvice.onExit( _startTime, \n");
s.append(" com.tomtom.james.informationpoint.advice.ContextAwareAdvice.onExit( com.tomtom.james.newagent.MethodExecutionTimeHelper.getStartTime(), \n");
s.append("\"" + informationPoint.getClassName() + "\", ");
s.append("\"" + informationPoint.getMethodName() + "\", ");
s.append("\"" + script + "\", ");
Expand All @@ -59,8 +57,7 @@ protected void insertAfter(CtMethod method, ExtendedInformationPoint information
protected void addCatch(ClassPool pool, CtMethod method, ExtendedInformationPoint informationPoint) throws CannotCompileException, NotFoundException {
String script = escapeScriptString(informationPoint.getScript().get());
StringBuilder s = new StringBuilder();
s.append(" long _methodStartTime = com.tomtom.james.newagent.GlobalValueStore.get(\"" + informationPoint + "\"); ");
s.append(" com.tomtom.james.informationpoint.advice.ContextAwareAdvice.onExit( _methodStartTime, ");
s.append(" com.tomtom.james.informationpoint.advice.ContextAwareAdvice.onExit( com.tomtom.james.newagent.MethodExecutionTimeHelper.getStartTime(), ");
s.append("\"" + informationPoint.getClassName() + "\", ");
s.append("\"" + informationPoint.getMethodName() + "\", ");
s.append("\"" + script + "\", ");
Expand All @@ -83,7 +80,7 @@ protected void addCatch(ClassPool pool, CtMethod method, ExtendedInformationPoin

// finally block
StringBuilder f = new StringBuilder("");
f.append(" com.tomtom.james.newagent.GlobalValueStore.getAndRemove(\"" + informationPoint + "\"); \n");
f.append(" com.tomtom.james.newagent.MethodExecutionTimeHelper.executionFinished(); \n");
method.insertAfter(f.toString(), true);
}
}

0 comments on commit a564fc8

Please sign in to comment.