Commits

Jonathan Gilbert Gilbert  committed fc0823c

added profiler endpoints

  • Participants
  • Parent commits 37b5018

Comments (0)

Files changed (6)

File profiler/pom.xml

     <build>
         <plugins>
             <plugin>
-                <groupId>org.scala-tools</groupId>
-                <artifactId>maven-scala-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>compile</phase>
-                        <goals>
-                            <goal>compile</goal>
-                        </goals>
-                    </execution>
-                </executions>
-            </plugin>
-            <plugin>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <configuration>
                     <argLine>-javaagent:${basedir}/../agent/target/agent-${project.version}-all.jar  -verbose:class -Dmangley.dump.classfile=true</argLine>
                 </configuration>
             </plugin>
             <plugin>
-                <artifactId>maven-compiler-plugin</artifactId>
+				<groupId>org.scala-tools</groupId>
+				<artifactId>maven-scala-plugin</artifactId>
+				<executions>
+					<execution>
+						<id>scala-compile-first</id>
+						<phase>process-resources</phase>
+						<goals>
+						    <goal>add-source</goal>
+							<goal>compile</goal>
+						</goals>
+					</execution>
+					<execution>
+						<id>scala-test-compile</id>
+						<phase>process-test-resources</phase>
+						<goals>
+							<goal>testCompile</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<executions>
+					<execution>
+						<phase>compile</phase>
+						<goals>
+							<goal>compile</goal>
+						</goals>
+					</execution>
+				</executions>
                 <configuration>
                     <source>1.5</source>
                     <target>1.5</target>
                 </configuration>
-            </plugin>
+			</plugin>
             <!-- this tells maven to copy the javaagent jar into your target/ directory -->
             <!-- where surefire can see it -->
             <plugin>

File profiler/src/main/java/org/trillica/mangley/profiler/util/BoundedSortedQueue.java

+package org.trillica.mangley.profiler.util;
+
+import java.util.*;
+
+public class BoundedSortedQueue<E> implements Queue<E>
+{
+    private Queue<E> delegate;
+    private int bound;
+
+    public BoundedSortedQueue(Queue<E> delegate, int bound)
+    {
+        this.delegate = delegate;
+        this.bound = bound;
+        if(delegate.size() > bound)
+            throw new IllegalArgumentException("Map size > bound");
+    }
+
+    public void trim(){
+        int overflow = delegate.size() - bound;
+        for (int i = 0; i < overflow; i++) {
+             delegate.poll();
+        }
+
+    }
+
+    public boolean add(E e) {
+        boolean rv = delegate.add(e);
+        trim();
+        return rv;
+    }
+
+    public boolean offer(E e) {
+        return delegate.offer(e);
+    }
+
+    public E remove() {
+        return delegate.remove();
+    }
+
+    public E poll() {
+        return delegate.poll();
+    }
+
+    public E element() {
+        return delegate.element();
+    }
+
+    public E peek() {
+        return delegate.peek();
+    }
+
+    public int size() {
+        return delegate.size();
+    }
+
+    public boolean isEmpty() {
+        return delegate.isEmpty();
+    }
+
+    public boolean contains(Object o) {
+        return delegate.contains(o);
+    }
+
+    public Iterator<E> iterator() {
+        return delegate.iterator();
+    }
+
+    public Object[] toArray() {
+        return delegate.toArray();
+    }
+
+    public <T> T[] toArray(T[] ts) {
+        return delegate.toArray(ts);
+    }
+
+    public boolean remove(Object o) {
+        return delegate.remove(o);
+    }
+
+    public boolean containsAll(Collection<?> objects) {
+        return delegate.containsAll(objects);
+    }
+
+    public boolean addAll(Collection<? extends E> es) {
+        boolean rv = delegate.addAll(es);
+        trim();
+        return rv;
+    }
+
+    public boolean removeAll(Collection<?> objects) {
+        return delegate.removeAll(objects);
+    }
+
+    public boolean retainAll(Collection<?> objects) {
+        return delegate.retainAll(objects);
+    }
+
+    public void clear() {
+        delegate.clear();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        return delegate.equals(o);
+    }
+
+    @Override
+    public int hashCode() {
+        return delegate.hashCode();
+    }
+}

File profiler/src/main/scala/org/trillica/mangley/profiler/server/SimpleHttpProfilerServer.scala

 package org.trillica.mangley.profiler.server
 
-import context.{AllInvocationCountContext, InvocationCountHandler}
+import context.{HeadInvocationsHandler, AllInvocationCountContext, InvocationCountHandler}
 import java.io.Writer
 import com.sun.net.httpserver.{HttpServer}
 import java.net.{InetSocketAddress}
     val server = HttpServer.create(new InetSocketAddress(19191), 0)
     server.createContext("/count", new InvocationCountHandler)
     server.createContext("/all", new AllInvocationCountContext)
+    server.createContext("/most", new HeadInvocationsHandler((i, j) => i - j))
+    server.createContext("/least", new HeadInvocationsHandler((i, j) => j - i))
     server.start()
   }
 }

File profiler/src/main/scala/org/trillica/mangley/profiler/server/context/HeadInvocationsHandler.scala

+package org.trillica.mangley.profiler.server.context
+
+import org.json.JSONWriter
+import org.trillica.mangley.MangleState
+import java.io.Writer
+import org.trillica.mangley.profiler.Profiler
+import scala.collection.JavaConversions._
+import org.trillica.mangley.profiler.util.{BoundedSortedQueue}
+import java.util.{PriorityQueue, Comparator, TreeMap}
+
+class HeadInvocationsHandler(compare:(Int, Int) => Int) extends SimpleGetHttpHandler {
+
+  def handle(arg: String) = {
+
+    val count = Integer.parseInt(arg.substring(1));//remove the /
+
+    val results = new BoundedSortedQueue[(String, Int)](new PriorityQueue[(String,Int)](count, new Comparator[(String,Int)](){
+      def compare(p1: (String, Int), p2: (String, Int)) = HeadInvocationsHandler.this.compare(p1._2, p2._2)
+    }), count)
+
+    MangleState.foreach((idx: Int, name: String) => {
+      results.add((name, Profiler.invocationCount(idx)))
+    })
+
+    out: Writer => {
+
+      val json = new JSONWriter(out)
+
+      json.`object`()
+
+      for ((name, count) <- results) {
+        json.key(name)
+        json.value(count)
+      }
+
+      json.endObject
+    }
+  }
+
+
+}

File profiler/src/main/scala/org/trillica/mangley/profiler/server/context/MostInvocationsHandler.scala

-package org.trillica.mangley.profiler.server.context
-
-import org.json.JSONWriter
-import org.trillica.mangley.MangleState
-import java.io.Writer
-
-class MostInvocationsHandler extends SimpleGetHttpHandler {
-
-    def handle(arg: String) = {
-
-    out: Writer => {
-
-      val json = new JSONWriter(out)
-
-      json.`object`()
-
-      MangleState.foreach((idx: Int, name: String) => {
-        json.key(name)
-        json.value(Profiler.invocationCount(idx))
-      })
-
-      json.endObject
-
-    }
-  }
-}

File profiler/src/main/scala/org/trillica/mangley/profiler/server/context/SimpleGetHttpHandler.scala

         responsePumper(out)
 
         out.close()
-        exchange.close()
       }
     } catch {
       case e => {
         exchange.sendResponseHeaders(500, 0)
+
       }
     }
+    finally {
+      exchange.close()
+    }
   }
 
   def handle(arg: String): (Writer => Unit)