Skip to content

Commit

Permalink
work on adding timing instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mebigfatguy committed Nov 18, 2024
1 parent 25df941 commit c9ac170
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/*.*" kind="src" path="etc"/>
<classpathentry kind="src" path="src/main/aj"/>
<classpathentry kind="src" output="target/classes/samples" path="src/samples/java"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" output="target/classes/test" path="src/test/java"/>
Expand Down Expand Up @@ -49,5 +50,8 @@
<classpathentry kind="lib" path="lib/jakarta.xml.bind-api-2.3.3.jar"/>
<classpathentry kind="lib" path="lib/akka-stream_2.12-2.6.4.jar"/>
<classpathentry kind="lib" path="lib/scala-library-2.12.10.jar"/>
<classpathentry kind="lib" path="lib/aspectjrt-1.9.22.1.jar"/>
<classpathentry kind="lib" path="lib/aspectjtools-1.9.22.1.jar"/>
<classpathentry kind="lib" path="lib/aspectjweaver-1.9.22.1.jar"/>
<classpathentry kind="output" path="target/classes/main"/>
</classpath>
23 changes: 22 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<property file="version.properties" />

<property name="src.dir" value="${basedir}/src/main/java" />
<property name="aspect.dir" value="${basedir}/src/main/aj" />
<property name="lib.dir" value="${basedir}/lib" />
<property name="etc.dir" value="${basedir}/etc" />
<property name="test.dir" value="${basedir}/src/test/java" />
Expand Down Expand Up @@ -117,7 +118,6 @@
<pathelement location="${lib.dir}/hamcrest-core-${hamcrest-core.version}.jar" />
<pathelement location="${spotbugs.dir}/lib/dom4j-1.6.1.jar" />
<pathelement location="${spotbugs.dir}/lib/jaxen-1.1.6.jar" />

</path>
<path id="sb-contrib.samples.classpath">
<pathelement location="${lib.dir}/akka-actor_2.12-${akka-actor_2.12.version}.jar" />
Expand Down Expand Up @@ -158,6 +158,11 @@
<pathelement location="${lib.dir}/jersey-media-multipart-${jersey-media-multipart.version}.jar" />
<pathelement location="${lib.dir}/scala-library-${scala-library.version}.jar" />
</path>
<path id="instrument.classpath">
<pathelement location="${lib.dir}/aspectjrt-${aspectjrt.version}.jar" />
<pathelement location="${lib.dir}/aspectjtools-${aspectjtools.version}.jar" />
<pathelement location="${lib.dir}/aspectjweaver-${aspectjweaver.version}.jar" />
</path>
</target>

<target name="validate_xml" depends="-init" unless="skip-validate" description="validates the xml files">
Expand Down Expand Up @@ -249,6 +254,22 @@
</jar>
</target>

<target name="instrument" depends="jar">
<delete file="${target.dir}/${ant.project.name}-aj-${sb-contrib.version}.jar"/>
<delete file="${target.dir}/${ant.project.name}-instrumented-${sb-contrib.version}.jar"/>
<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties" classpathref="instrument.classpath" />
<path id="iajc.classpath">
<path refId="sb-contrib.classpath"/>
<path refId="instrument.classpath"/>
</path>
<iajc inpath="${target.dir}/${ant.project.name}-${sb-contrib.version}.jar" outjar="${target.dir}/${ant.project.name}-aj-${sb-contrib.version}.jar" source="${javac.source}" target="${javac.target}" classpathref="iajc.classpath" sourceRoots="${aspect.dir}"/>
<zip destfile="${target.dir}/${ant.project.name}-instrumented-${sb-contrib.version}.jar">
<zipfileset src="${target.dir}/${ant.project.name}-aj-${sb-contrib.version}.jar" includes="**/*" prefix=""/>
<zipfileset src="${lib.dir}/aspectjrt-${aspectjrt.version}.jar" includes="**/*.class" prefix=""/>
</zip>
<delete file="${target.dir}/${ant.project.name}-aj-${sb-contrib.version}.jar"/>
</target>

<target name="html" depends="-init" description="generates dynamic html">
<xslt basedir="${etc.dir}" destdir="${htdocs.dir}" style="${etc.dir}/bugdescriptions.xsl" in="${etc.dir}/messages.xml" out="${htdocs.dir}/bugdescriptions.html" />
</target>
Expand Down
46 changes: 46 additions & 0 deletions src/main/aj/com/mebigfatguy/fbcontrib/instrument/Timing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mebigfatguy.fbcontrib.instrument;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import edu.umd.cs.findbugs.ba.ClassContext;

@Aspect
public class Timing {

private static final Map<String, TimingInfo> timingInfo = new HashMap<>();

@Around("execution(* com.mebigfatguy.fbcontrib.detect.*.visitClassContext(..)) && args(classContext)")
public Object timings(ProceedingJoinPoint joinPoint,ClassContext classContext) throws Throwable {
long start = System.currentTimeMillis();
try {
Object result = joinPoint.proceed();
return result;
} finally {
long delta = System.currentTimeMillis() - start;
String detector = joinPoint.getSignature().getDeclaringTypeName();
TimingInfo info = timingInfo.get(detector);
if (info == null) {
info = new TimingInfo();
timingInfo.put(detector, info);
}
info.numberOfCalls++;
info.totalTime += delta;
if (delta > info.longestTime) {
info.longestTime = delta;
info.longestInput = classContext.getJavaClass().getClassName();
}
}
}

class TimingInfo {
long numberOfCalls;
long totalTime;
long longestTime;
String longestInput;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/mebigfatguy/fbcontrib/FBContrib.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private FBContrib() {
public static void main(final String[] args) {
JOptionPane.showMessageDialog(null,
"To use fb-contrib, copy this jar file into your local SpotBugs plugin directory, and use SpotBugs as usual.\n\nfb-contrib is a trademark of MeBigFatGuy.com",
"fb-contrib: copyright 2005-2019", JOptionPane.INFORMATION_MESSAGE);
"fb-contrib: copyright 2005-2024", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

0 comments on commit c9ac170

Please sign in to comment.