summaryrefslogtreecommitdiff
path: root/src/partest/scala/tools/partest/instrumented/Profiler.java
blob: e267e197e703e340daa48676e7946619573da5c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* NEST (New Scala Test)
 * Copyright 2007-2013 LAMP/EPFL
 * @author Grzegorz Kossakowski
 */

package scala.tools.partest.instrumented;

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

/**
 * A simple profiler class that counts method invocations. It is being used in byte-code instrumentation by inserting
 * call to {@link Profiler#methodCalled(String, String, String)} at the beginning of every instrumented class.
 *
 * WARANING: This class is INTERNAL implementation detail and should never be used directly. It's made public only
 * because it must be universally accessible for instrumentation needs. If you want to profile your test use
 * {@link Instrumentation} instead.
 */
public class Profiler {
	
	private static boolean isProfiling = false;
	private static Map<MethodCallTrace, Integer> counts = new HashMap<MethodCallTrace, Integer>();
	
	static public class MethodCallTrace {
	  final String className;
	  final String methodName;
	  final String methodDescriptor;
	  
	  public MethodCallTrace(final String className, final String methodName, final String methodDescriptor) {
	    this.className = className;
	    this.methodName = methodName;
	    this.methodDescriptor = methodDescriptor;
	  }
	  
	  @Override
	  public boolean equals(Object obj) {
	    if (!(obj instanceof MethodCallTrace)) {
	      return false;
	    } else {
	      MethodCallTrace that = (MethodCallTrace) obj;
	      return that.className.equals(className) && that.methodName.equals(methodName) && that.methodDescriptor.equals(methodDescriptor);
	    }
	  }
	  @Override
	  public int hashCode() {
	    return className.hashCode() ^ methodName.hashCode() ^ methodDescriptor.hashCode();
	  }
	}
	
	public static void startProfiling() {
		isProfiling = true;
	}
	
	public static void stopProfiling() {
		isProfiling = false;
	}
	
	public static boolean isProfiling() {
	  return isProfiling;
	}

	public static void resetProfiling() {
	  counts = new HashMap<MethodCallTrace, Integer>();
	}
	
	public static void methodCalled(final String className, final String methodName, final String methodDescriptor) {
		if (isProfiling) {
		  MethodCallTrace trace = new MethodCallTrace(className, methodName, methodDescriptor);
			Integer counter = counts.get(trace);
			if (counter == null) {
				counts.put(trace, 1);
			} else {
				counts.put(trace, counter+1);
			}
		}
	}
	
	public static Map<MethodCallTrace, Integer> getStatistics() {
	  return new HashMap<MethodCallTrace, Integer>(counts);
	}

}