alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Profiler.java)

This example Scala source code file (Profiler.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

hashmap, integer, map, methodcalltrace, override, profiler, string

The Profiler.java Scala example source code

/* 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);
        }

}

Other Scala source code examples

Here is a short list of links related to this Scala Profiler.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.