package listeners; import java.util.HashMap; import java.util.Map; import gov.nasa.jpf.ListenerAdapter; import gov.nasa.jpf.search.Search; import gov.nasa.jpf.vm.MethodInfo; import gov.nasa.jpf.vm.ThreadInfo; import gov.nasa.jpf.vm.VM; /** * This listener prints the methods that are executed most. * * @author Franck van Breugel */ public class Profiler extends ListenerAdapter { private Map methods; /** * Initializes this listener. */ public Profiler() { this.methods = new HashMap(); } /** * Whenever a method is entered, counts it. * * @param vm JPF's vm. * @param thread thread currently running. * @param method method being entered. */ public void methodEntered(VM vm, ThreadInfo thread, MethodInfo method) { if (this.methods.containsKey(method)) { this.methods.put(method, this.methods.get(method) + 1); } else { this.methods.put(method, 1); } } /** * When the search finishes, print the methods that are executed most. * * @param search JPF's search. */ public void searchFinished(Search search) { int max = -1; for (Integer count : this.methods.values()) { max = Math.max(max, count); } System.out.println("Method(s) most executed:"); for (MethodInfo method : this.methods.keySet()) { if (this.methods.get(method) == max) { System.out.printf("%s of %s%n", method.getName(), method.getClassInfo().getName()); } } } }