aboutsummaryrefslogtreecommitdiffstats
path: root/tests/product/testScripts/cmdline11/profile/Profile.java
blob: daa1659ca3b157cc1d42ba64a07cd7e7de412a02 (plain)
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
package profile;

import org.aspectj.lang.*;

/**
 * Profile execution time of join points
 * by making a concrete aspect which defines <tt>targets()</tt>,
 * if not <code>register(JoinPoint)</code>
 * and <code>signal(Object, long, long)</code>.
 */
public abstract aspect Profile {

    /** 
     * Identify join points to profile.
     * Those within the lexical extent of Profile
     * or its subtypes will be excluded from profiling.
     */
    protected abstract pointcut targets();

    Object around() : targets() && !within(Profile+) {
        final Object key = register(thisJoinPoint);
        final long startTime = System.currentTimeMillis();
        try {
            return proceed();
        } finally {
            signal(key, startTime, System.currentTimeMillis());
        }
    }
    protected Object register(JoinPoint jp) {
        return Thread.currentThread().getName() + " -- " + jp;
    }
    protected void signal(Object key, long start, long end) {
        long duration = end - start;
        String tag = (duration == 0 ? "none" : duration < 100 ? "fast" : "slow");
        //System.err.println(duration + " " + start + " - " + end + ": " + key);
        System.err.println(tag + ": " + key);
    }
}