如何进行Java EE性能测试与调优
- - ITeye博客性能测试不同于功能测试,不是对与错的检验,而是快与慢的衡量. 在进行真正的性能测试之前要先搞清楚目标:. 在确定的硬件条件下,可以支持的并发数越大越好,响应时间越快越好. 具体需要达到的并发数是多大,要求的响应时间是多快,由产品经理来提出. 在确定的硬件条件下,测试得到最大并发数和相应的响应时间之后.
import org.slf4j.Logger; public class TraceUtil { final Logger logger; final long threshold = 1000; private long begin; private long offtime = 0; private String threadInfo; private String targetId; public TraceUtil(Logger logger, Thread thread, String targetId, long begin) { this.logger = logger; this.threadInfo = thread.getId() + "-" + thread.toString(); this.targetId = targetId; this.begin = begin; } public void trace(String targetEvent) { long duration = System.currentTimeMillis() - begin; long increment = duration - offtime; offtime = duration; float percentage = (float) increment / (float) duration * 100; if (duration > threshold && percentage > 20) { logger.error( "Response time is too large: [{}], {}/{} ({}), {}, {}", new String[] { threadInfo + "", increment + "", duration + "", percentage + "%", targetEvent, targetId }); } } }
<%@ page import="java.lang.management.*, java.util.*" %> <%! Map cpuTimes = new HashMap(); Map cpuTimeFetch = new HashMap(); %> <% out.println("Threads Monitoring"); long cpus = Runtime.getRuntime().availableProcessors(); ThreadMXBean threads = ManagementFactory.getThreadMXBean(); threads.setThreadContentionMonitoringEnabled(true); long now = System.currentTimeMillis(); ThreadInfo[] t = threads.dumpAllThreads(false, false); for (int i = 0; i < t.length; i++) { long id = t[i].getThreadId(); Long idObj = new Long(id); long current = 0; if (cpuTimes.get(idObj) != null) { long prev = ((Long) cpuTimes.get(idObj)).longValue(); current = threads.getThreadCpuTime(t[i].getThreadId()); long catchTime = ((Long) cpuTimeFetch.get(idObj)).longValue(); double percent = (double)(current - prev) / (double)((now - catchTime) * cpus * 1000); if (percent > 0 && prev > 0) { out.println("<li>" + t[i].getThreadName()+"#"+t[i].getThreadId() + " Time: " + percent + " (" + prev + ", " + current + ") "); String locked = t[i].getLockInfo()==null?"":t[i].getLockInfo().getClassName(); out.println(" Blocked: (" + t[i].getBlockedTime() + ", " + t[i].getBlockedCount() + ", " + locked + ")</li>"); } } cpuTimes.put(idObj, new Long(current)); cpuTimeFetch.put(idObj, new Long(now)); } %>