Esempio n. 1
0
  /** Resets all the event measurements. */
  public static final void reset() {

    if (!_isEnabled) {
      return;
    }

    Enumeration eventNames = _events.keys();
    while (eventNames.hasMoreElements()) {
      String eventName = (String) eventNames.nextElement();
      EventProfile eventProfile = (EventProfile) _events.get(eventName);
      eventProfile.reset();
    }
  }
Esempio n. 2
0
  /**
   * Prints the collected event measurements to the specified writer.
   *
   * @param writer Writer.
   */
  public static final void print(PrintWriter writer) {
    if (!_isEnabled) {
      return;
    }

    Iterator profiles = _events.values().iterator();
    while (profiles.hasNext()) {
      EventProfile eventProfile = (EventProfile) profiles.next();
      StringBuffer msg = new StringBuffer();
      msg.append(eventProfile.getEvent() + ":\n");
      msg.append("  count = " + eventProfile.getCount());
      msg.append("  total = " + eventProfile.getTotalTime() + " (ms)");
      msg.append("  average = " + eventProfile.getAverageTime() + " (ms)");

      if (_isMemoryEnabled) {
        msg.append("\n");
        msg.append("  memory consumed = " + eventProfile.getTotalConsumedMemory() + " (bytes)");
        msg.append("  memory collected = " + eventProfile.getTotalCollectedMemory() + " (bytes)");
      }

      msg.append("\n");
      writer.println(msg.toString());
    }

    writer.flush();
  }
Esempio n. 3
0
  /**
   * Designates the ending of an event being measured.
   *
   * @param event Event description.
   * @return Event duration (in milliseconds).
   */
  public static final long end(String event) {

    if (!_isEnabled) {
      return 0;
    }

    long elapsedTime = 0;
    long endTime = System.currentTimeMillis();

    long endMemory = 0;
    if (_isMemoryEnabled) {
      endMemory = Runtime.getRuntime().freeMemory();
    }

    Stack threadStack = getThreadStack();

    synchronized (threadStack) {
      ThreadTrace nextTrace = (ThreadTrace) threadStack.peek();
      if (!nextTrace.getEvent().equals(event)) {
        System.err.println("Begin/end mismatch occurred at stack trace:");
        printStackTrace(new PrintWriter(System.err, true));
        System.err.println("Profiler has been disabled.");
        enable(false);
        return 0;
      }

      ThreadTrace trace = (ThreadTrace) threadStack.pop();
      EventProfile eventProfile = getEventProfile(trace.getEvent());

      elapsedTime = endTime - trace.getStartTime();
      eventProfile.update(elapsedTime);

      if (_isMemoryEnabled) {
        long memory = trace.getStartMemory() - endMemory;
        eventProfile.updateMemory(memory);
      }
    }

    return elapsedTime;
  }