All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.agent.profiler.ProcessProfiler Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version

package org.snapscript.agent.profiler;

import static org.snapscript.core.Reserved.SCRIPT_EXTENSION;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.snapscript.agent.debug.TraceAdapter;
import org.snapscript.core.Path;
import org.snapscript.core.Scope;
import org.snapscript.core.trace.Trace;

public class ProcessProfiler extends TraceAdapter {
   
   private final Map profilers;
   private final Set resources;
   
   public ProcessProfiler() {
      this.profilers = new ConcurrentHashMap();
      this.resources = new CopyOnWriteArraySet();
   }

   public SortedSet lines(int size) {
      SortedSet results = new TreeSet();
      SortedSet reduced = new TreeSet();
      
      for(String resource : resources){
         ResourceProfiler profiler = profilers.get(resource);
         
         if(profiler != null) {
            profiler.collect(results, size);
         }
      }
      Iterator iterator = results.iterator();
      
      while(iterator.hasNext()) {
         if(size-- <= 0) {
            break;
         }
         ProfileResult result = iterator.next();
         
         if(result != null) {
            long duration = result.getTime();
            
            if(duration > 0) {
               reduced.add(result);
            }
         }
      }
      return reduced;
   }
   
   @Override
   public void before(Scope scope, Trace trace) {
      Path path = trace.getPath();
      String resource = path.getPath();
      ResourceProfiler profiler = profilers.get(resource);
      int line = trace.getLine();
      
      if(profiler == null) {
         String local = resource;
         
         if(!local.endsWith(SCRIPT_EXTENSION)) { // a.b.c
            local = local.replace('.', '/'); // a/b/c
            local = local + SCRIPT_EXTENSION; // a/b/c.snap
         }
         if(!local.startsWith("/")) {
            local = "/" + local; // /a/b/c.snap
         }
         if(resources.add(local)) {
            profiler = new ResourceProfiler(local);
            profilers.put(resource, profiler);
            resources.add(resource);
         }
      }
      if(profiler != null) { // eval(...) could be null
         profiler.enter(line);
      }
   }

   @Override
   public void after(Scope scope, Trace trace) {
      Path path = trace.getPath();
      String resource = path.getPath();
      ResourceProfiler profiler = profilers.get(resource);
      int line = trace.getLine();
      
      if(profiler != null) {
         profiler.exit(line);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy