Profiling a Java Spring application

前端 未结 8 1241
再見小時候
再見小時候 2020-12-12 20:11

I have a Spring application that I believe has some bottlenecks, so I\'d like to run it with a profiler to measure what functions take how much time. Any recommendations to

8条回答
  •  悲哀的现实
    2020-12-12 20:25

    A bit modified version of Yuri's answer on top (the selected answer) that auto calculates the totals of durations and arranges them desc. The totals gets printed in the end only. Might save you 10 mns.

        @Component
        @Aspect
        public class SystemArchitecture
        {
    
             @Pointcut("execution(* erp..*.*(..))")
             public void businessController()
             {
             }
    
             @Pointcut("execution(* TestMain..*.*(..))")
             public void theEnd()
             {
             }
        }
    
    
    
        @Component
        @Aspect
        public class TimeExecutionProfiler
        {
    
            static Hashtable  ht  = new Hashtable();
    
            @Around("profiler.SystemArchitecture.businessController()")
            public Object profile(ProceedingJoinPoint pjp) throws Throwable
            {
                long start = System.nanoTime();
                Object output = pjp.proceed();
                long elapsedTime = System.nanoTime() - start;
                String methodName = pjp.getSignature().toString();
                if (ht.get(methodName) == null)
                {
                    ht.put(methodName, elapsedTime);
                }
                else
                {
                    ht.put(methodName, ht.get(methodName) + elapsedTime);
                }
                // System.out.println(methodName + " : " + elapsedTime + " milliseconds.");
    
                return output;
            }
    
            @After("profiler.SystemArchitecture.theEnd()")
            public void profileMemory()
            {
                List keys = Arrays.asList(ht.keySet().toArray());
                java.util.Collections.sort(keys, new Comparator()
                {
    
                    @Override
                    public int compare(Object arg0, Object arg1)
                    {
                        return ht.get(arg1).compareTo(ht.get(arg0));
                    }
                });
    
                System.out.println("totals Used:");
                for (Object name : keys)
                {
                    System.out.println("--" + name + " : " + (ht.get(name) / 1000000));
                }
                System.out.println("JVM memory in use = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
            }
        }
    
        

    提交回复
    热议问题