Java Examples¶
Basic Measurement¶
Given Main.java:
public class Main {
static double compute(int n) {
double sum = 0;
for (int i = 0; i < n; i++)
sum += Math.sin(i) * Math.cos(i);
return sum;
}
public static void main(String[] args) {
int n = args.length > 0 ? Integer.parseInt(args[0]) : 1000000;
System.out.println(compute(n));
}
}
codegreen measure java Main.java -- 1000000
codegreen measure java Main.java -g fine --export-plot energy.html
How It Works for Java¶
- Tree-sitter parses the Java source and identifies method definitions
- Checkpoint calls are injected at method enter/exit points
- The instrumented source is compiled with
javac - The compiled class runs with the CodeGreen Java runtime on the classpath
- Energy data is collected via the NEMB C++ backend
Known Limitations¶
- Java filename must match the public class name
- JNI bridge for NEMB has known issues with some JVM configurations
- Best results with simple single-file programs