zoukankan      html  css  js  c++  java
  • btrace 笔记

    转载请注明原链接地址 http://www.cnblogs.com/dongxiao-yang/p/6134393.html

    btrace 是一个可以不用重启线上java业务查问题的神器,记一下自己折腾半天写的几个demo

    java业务代码

    package com.youku.data.test.kfktest;
    
    /**
     * Hello world!
     *
     */
    public class App {
    	public static void main(String[] args) throws InterruptedException {
    		System.out.println("Hello World!");
    
    		int[] a = new int[] {
    				20,
    				21,
    				21,
    				21,
    				22,
    				19,
    				24,
    				27,
    				20,
    				34,
    				23,
    				20,
    				17,
    				21,
    				20,
    				18,
    				19,
    				20,
    				24,
    				20,
    				23,
    				22,
    				27
    				 };
    		
    		//int s=0;
    		BtraceTest bt = new BtraceTest();
    		while(true)
    		{
    			for(int i:a)
    			{
    				bt.getret(i);
    				Thread.sleep(100);
    			}
    			
    			Thread.sleep(500);
    			
    		}
    		
    	
    		
    		//System.out.println(s);
    
    	}
    }
    

      

    package com.youku.data.test.kfktest;
    
    public class BtraceTest {
    
    	private long ret = 0;
    
    	public long getret(int num) {
    		ret += num;
    
    		// Thread.sleep(10);
    
    		method1();
    		method2();
    		method3();
    		return ret;
    
    	}
    
    	private void method1() {
    		String a = "asdfadsfa" + "sdfasdfdsf";
    	}
    
    	private void method2() {
    		double a = 1000 / 3.14;
    	}
    
    	private void method3() {
    		float a = (float) (1000 / 3.14);
    	}
    
    }
    

      

    btrace 脚本

    /* BTrace Script Template */
    import com.sun.btrace.annotations.*;
    import static com.sun.btrace.BTraceUtils.*;
    
    @BTrace
    public class TracingScript {
    	/* put your code here */
      @TLS private static long startTime = 0;  
         
       @OnMethod(  
          clazz="com.youku.data.test.kfktest.BtraceTest",  
          method="getret"  
       )   
       public static void startExecute(){  
         startTime = timeNanos();  
       } 
    
    
        @OnMethod(
            clazz="com.youku.data.test.kfktest.BtraceTest",
            method="getret",
            location=@Location(Kind.RETURN)
        )
    public static void traceExecute(@Duration long duration,@ProbeMethodName String pmn){  
        long time = timeNanos() - startTime;  
         println(strcat("duration(nanos): ", str(duration)));
         println(strcat("execute time(nanos): ", str(time)));  
        
         println(strcat("ProbeMethodName(pmn): ", str(pmn)));   
       } 
    }
    

      

    package com.sun.btrace.samples;
    
    import com.sun.btrace.BTraceUtils;
    import com.sun.btrace.Profiler;
    import com.sun.btrace.annotations.*;
    
    
    @BTrace class Profiling {
        @Property
        Profiler swingProfiler = BTraceUtils.Profiling.newProfiler();
        
        @OnMethod(clazz="com.youku.data.test.kfktest.BtraceTest", method="/.*/")
        void entry(@ProbeMethodName(fqn=true) String probeMethod) { 
            BTraceUtils.Profiling.recordEntry(swingProfiler, probeMethod);
        }
        
        @OnMethod(clazz="com.youku.data.test.kfktest.BtraceTest", method="/.*/", location=@Location(value=Kind.RETURN))
        void exit(@ProbeMethodName(fqn=true) String probeMethod, @Duration long duration) { 
            BTraceUtils.Profiling.recordExit(swingProfiler, probeMethod, duration);
        }
        
        @OnTimer(5000)
        void timer() {
            BTraceUtils.Profiling.printSnapshot("Swing performance profile", swingProfiler);
        }
    }
    

      

    脚本启动命令

    ./bin/btrace <pid>  /Users/dongxiaoyang/Desktop/<btracescript.java>

    参考资料 

    用BTrace排查性能问题

    Btrace入门到熟练小工完全指南

    使用jvisualvm.exe 的Btrace插件介绍/使用教程

    BTrace User's Guide

     

  • 相关阅读:
    struts2基础
    hibernate框架基础
    Django的模板层
    HBuilder无法连接夜神模拟器的解决办法
    Django的视图层
    Django的路由层
    Django简介
    http协议
    web应用
    Mongodb之增删改查
  • 原文地址:https://www.cnblogs.com/dongxiao-yang/p/6134393.html
Copyright © 2011-2022 走看看