4

Does anyone know how can I create a new Performance Counter (perfmon tool) in Java?

For example: a new performance counter for monitoring the number / duration of user actions.

I created such performance counters in C# and it was quite easy, however I couldn’t find anything helpful for creating it in Java…

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Diana
  • 41
  • 1
  • 2
  • 1
    possible duplicate of [Java app performance counters viewed in Perfmon](http://stackoverflow.com/questions/358129/java-app-performance-counters-viewed-in-perfmon) – Thorbjørn Ravn Andersen Dec 21 '10 at 14:39

3 Answers3

3

If you want to develop your performance counter independently from the main code, you should look at aspect programming (AspectJ, Javassist).

You'll can plug your performance counter on the method(s) you want without modifying the main code.

Benoit Courtine
  • 7,014
  • 31
  • 42
1

Not sure what you are expecting this tool to do but I would create some data structures to record these times and counts like

class UserActionStats {
   int count;
   long durationMS;
   long start = 0;

   public void startAction() {
       start = System.currentTimeMillis();
   }
   public void endAction() {
       durationMS += System.currentTimeMillis() - start;
       count++;
   }
}

A collection for these could look like

private static final Map<String, UserActionStats> map = 
        new HashMap<String, UserActionStats>();

public static UserActionStats forUser(String userName) {
    synchronized(map) {
        UserActionStats uas = map.get(userName);
        if (uas == null)
            map.put(userName, uas = new UserActionStats());
        return uas;
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Shouldn't your durationMS actually be incrementing: start - System.currentTimeMillis(); – jzd Dec 21 '10 at 14:41
  • Thanks, but I don’t think it exactly what I meant. I want to view my performance counter in perfmon tool. A simple measurement of duration or number will not be updated there and will not be stored in the perfmon logs – Diana Dec 21 '10 at 14:45
1

Java does not immediately work with perfmon (but you should see DTrace under Solaris).

Please see this question for suggestions: Java app performance counters viewed in Perfmon

Community
  • 1
  • 1
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347