From a8cce319dbb22fe70fbd8cb1ac81bf328096e5e3 Mon Sep 17 00:00:00 2001 From: Wojciech Baran Date: Thu, 5 Jul 2018 14:06:42 +0200 Subject: [PATCH] Fixed the wrong calculation of methodExecutionTime --- .../newagent/MethodExecutionTimeHelper.java | 47 +++++++++++++++++++ .../james/newagent/james/GroovyJames.java | 11 ++--- 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 james-agent/src/main/java/com/tomtom/james/newagent/MethodExecutionTimeHelper.java diff --git a/james-agent/src/main/java/com/tomtom/james/newagent/MethodExecutionTimeHelper.java b/james-agent/src/main/java/com/tomtom/james/newagent/MethodExecutionTimeHelper.java new file mode 100644 index 0000000..13353a7 --- /dev/null +++ b/james-agent/src/main/java/com/tomtom/james/newagent/MethodExecutionTimeHelper.java @@ -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> startTimeStack = ThreadLocal.withInitial(new Supplier>() { + @Override + public ArrayDeque get() { + return new ArrayDeque(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; + } +} diff --git a/james-agent/src/main/java/com/tomtom/james/newagent/james/GroovyJames.java b/james-agent/src/main/java/com/tomtom/james/newagent/james/GroovyJames.java index e18de64..106a408 100644 --- a/james-agent/src/main/java/com/tomtom/james/newagent/james/GroovyJames.java +++ b/james-agent/src/main/java/com/tomtom/james/newagent/james/GroovyJames.java @@ -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 + "\", "); @@ -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 + "\", "); @@ -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); } }