1、Java企业应用-性能优化原则,方法与策略Outline1.Performance Basics and Methodology2.Fundamentals of Performance Tuning Profiling Driven Optimization JVM Tuning GC JIT3.Optimization Strategy for JavaEE4.RecapRecall Littles lawL=*WIn queueing theory,the long-term average number ofcustomers in a stable system,L,is equal
2、 to the long-termaverage arrival rate,multiplied by the average time acustomer spends in the system,Wsource:https:/en.wikipedia.org/wiki/Little%27s_lawThroughput and RTMeanNumberInSystem=MeanThroughput*MeanResponseTime.Throughput and RT are related Decreasing RT”almost always”improves Throughput Thr
3、oughput improving doesnt necessarily mean RTdecreasing.Performance tuning and cost saving More higher throughput/lower RT but without addingnew hardwaresource:https:/en.wikipedia.org/wiki/Little%27s_lawApproaches to PerformanceApp Algorithm optimization Profiling-drivenAppServerJava VM Upgrade(JDK&O
4、S)JVM(OS)Tuning SpecializationOperating SystemHardware Use new hardware Use cheaper hardwareJavas viewApproaches:a)Outside in approach(performance baseline)b)Layered approach(“Bottom up”or“Top down”)c)A hybrid of both a),b)Amdahls Scaling Law1Speedup=1+F:is fraction of workthat is serialN:is number
5、ofthreadssource:https:/en.wikipedia.org/wiki/Amdahl%27s_law Reduce the amount of serial work performedCosts Reduce Scaling1.Potential contributors to F:.Synchronization(synchronized&j.u.c.Lock)data structures need to be thread.JConsole(MXBean).Java Mission Control.JProfilersafe communication overhea
6、d between.HealthCenter&jucProfilerthreads.Infamous“stop the world”(aka STW)inJVM2.Cost incurred when the N gets increased.Thread context switchProfiling:Sampling vs Instrumentsamplingmethod callbarfoobarfoomainmainInstrumentbarfoomainAvailable Technology:BCI,JVMTi,javax.management,System.currentTime
7、Millis()Sampling vs Instrument.Sampling Lower overhead(determined by sampling interval)Discover unknown code Non intrusive No execution path Periodicity Bias.Instrument Wall time(estimate IO time)Full execution path Configuration on what methods to instrument Generally more data to be collectedSafep
8、oint Bias.Stack trace sampling happens only when thegiven thread at a safepoint The hot loop may not get profiled anymore.Use following tools instead Java Mission Control Honest Profiler(githup)ZProfiler(alipay internal profiling tool)Tools for Diagnostics Most of them could be found in JAVA_HOEM/bi
9、n Good reference:Troubleshooting Guide for JavaSE 6 withHotSpotVMBasics of JVM TuningGuild for GC Tuning Select the right GC algorithm parallel old,CMS and G1 collector Rule of thumb:GC overhead is ideally 10%Choose the right heap sizesource:Charlie Hunt,Binu John JavaTM performance Configure the ap
10、propriate GG parametersJIT and common optimization Important concepts Profiler guided optimization(PGO)Optimization decisions are made dynamically Mix mode execution Some common optimization Inlining Intrinsic Monomorphic dispatch Liskov substitution principleSubtypes MUST be substitutablefor their
11、base typesJIT Profiling with JITWatch JITWatch:a graphical visualization and analysis tool forunderstanding the JITEnabled by:-XX:+UnlockDiagnosticVMOptions-XX:+TraceClassLoading-XX:+LogCompilation-XX:+PrintAssemblyTypical distributed JEE architectureJava EE ContainerJava EE ApplicationRemote Bean v
12、ia RMIREST Service via HTTPDB via JDBCSystem AcloudSystem BcloudSystem CcloudThe problem.Add communication cost RPC serialization/deserialization.Can not shift resources towardsdemand.Can not share the underlying Javaartifacts(such as JIT)Multitenancy for JavaEEAppApp1 App2 App3 AppxAppServerAppServ
13、erJava VMJava VMOperating SystemHardwareOperating SystemHardware Run multiple Java EE applications(as tenants)into same Java EE containerHigh Density Cloud for JavaEEThe JavaEE applications developedseparately can be deployedseamlessly into the same container.devOps Orchestrate JavaEE application at
14、scale Infrastructure Multi-tenant JavaEE container Virtualized JVMsource:https:/Tomcat/JDK extended for PaaS AliTomcat:run multiple apps side-by-sideApp1App3 AppxApp2safelyAliTomcat(Multi-tenant)AJDK(Virtualized)OS AJDK allows for collocation of multiple JEEapps(as tenant)in a single instance of JVM
15、:Isolate application from one another.Share metadata aggressively andtransparently,such as:Hardware bytecodes of methods GC JIT:Alibaba/Alipay JDK,based on OpenJDKAAE:Alibaba Application Engine Scaling tenant application with AAE spread application evenly across hosts but pack applications on the si
16、ngle JVM as mush as possible,based on its resource capacity:CPU usage Memory(monitoring GC)DC-1DC-2AAEgFaedacbaABABBenefitsCPU100%(100*30%+100*70%)/200=50%50%ABA+BBenefits(Cont.)Eliminate the unnecessary RPC Minimize the cost caused by object serialization/de-serialization Share underlying Java artifacts as much as possible GC JIT HeapCompared with DockerDDCCPUCPUBAABCMemMemOSOSHardwareHardwareCPU overcommit via dockershared memory viaMultitenancySummary What we covered:Performance basics&methodology Performance tuning Profiling Tuning from JVM perspective Multitenancy for JavaEE