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

org.snapscript.studio.agent.profiler.TraceProfiler Maven / Gradle / Ivy

The newest version!
package org.snapscript.studio.agent.profiler;

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

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

import org.snapscript.common.Cache;
import org.snapscript.common.CopyOnWriteCache;
import org.snapscript.core.module.Path;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.trace.Trace;
import org.snapscript.studio.agent.debug.TraceAdapter;

public class TraceProfiler extends TraceAdapter {
   
   private final Cache profilers;
   private final Set resources;
   
   public TraceProfiler() {
      this.profilers = new CopyOnWriteCache();
      this.resources = new CopyOnWriteArraySet();
   }

   public SortedSet lines(int size) {
      SortedSet results = new TreeSet();
      SortedSet reduced = new TreeSet();
      
      for(String resource : resources){
         ResourceProfiler profiler = profilers.fetch(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 traceBefore(Scope scope, Trace trace) {
      Path path = trace.getPath();
      String resource = path.getPath();
      ResourceProfiler profiler = profilers.fetch(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
         }
         profiler = profilers.fetch(local);        
         
         if(resources.add(local)) {
            if(profiler == null) {
               profiler = new ResourceProfiler(local);
            }
            profilers.cache(local, profiler);
            profilers.cache(resource, profiler);
            resources.add(resource);
         }
      }
      if(profiler != null) { // eval(...) could be null
         profiler.enter(line);
      }
   }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy