相关推荐:Java 性能测试

1 前言   开始之前,我问自己,为什么要学习jProfiler?   回答之前先让我们来看看什么是jProfiler:JProfiler是一个商业授权的Java剖析工具,由EJ技术有限公司,针对Java EE和Java SE应用程序开发的。它允许两个内存剖面评估内存使用情况和动态分配泄

Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序开发、串口通讯程序(只在最新的预览版中提供),MINA 所支持的功能也在进一步的扩展中。新项目里的web service需要高并发服务能力,tomcat目前最多支持到200个并发。远达不到要求。0.测试环境:client和server:CPU:单核2G,内存:4GB;网络:局域网1.下载mina2.0安装包http://labs.renren.com/apache-mirror//mina/2.0.4/apache-mina-2.0.4-bin.tar.gzhttp://mina.apache.org/downloads.html2.解压后打开eclipse建立工程HelloServer,把mina-core-2.0.4.jar,mina-filter-compression-2.0.4.jar,slf4j-api-1.6.1.jar导入,注意java必须要1.5以上3.写代码我的测试代码: package org.apache.mina.example.httpserver.codec;

import org.apache.mina.core.future.IoFutureListener;

import org.apache.mina.core.service.IoHandler;

import org.apache.mina.core.service.IoHandlerAdapter;

import org.apache.mina.core.session.IdleStatus;

import org.apache.mina.core.session.IoSession;

/**

* An {@link IoHandler} for HTTP.

*

* @author The Apache MINA Project (dev@mina.apache.org)

* @version $Rev: 588178 $, $Date: 2007-10-25 18:28:40 +0900 (?, 25 10? 2007) $

*/

public class ServerHandler extends IoHandlerAdapter {

@Override

public void sessionOpened(IoSession session) {

// set idle time to 60 seconds

session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);

}

@Override

public void messageReceived(IoSession session, Object message) {

// Check that we can service the request context

HttpResponseMessage response = new HttpResponseMessage();

response.setContentType("text/plain");

response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);

response.appendBody("CONNECTED");

// msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);

// byte[] b = new byte[ta.buffer.limit()];

// ta.buffer.rewind().get(b);

// msg.appendBody(b);

// System.out.println("####################");

// System.out.println(" GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+

// ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));

// System.out.println("#################### - status="+ta.state+", index="+message.getIndex());

// Unknown request

// response = new HttpResponseMessage();

// response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);

// response.appendBody(String.format(

// "<html><body><h1>UNKNOWN REQUEST %d</h1></body></html>",

// HttpResponseMessage.HTTP_STATUS_NOT_FOUND));

response = new HttpResponseMessage();

response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);

response.appendBody("<html><body><h1>"+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date())+"</h1></body></html>");

if (response != null) {

session.write(response).addListener(IoFutureListener.CLOSE);

}

}

@Override

public void sessionIdle(IoSession session, IdleStatus status) {

//IoSessionLogger.getLogger(session).info("Disconnecting the idle.");

session.close();

}

@Override

public void exceptionCaught(IoSession session, Throwable cause) {

//IoSessionLogger.getLogger(session).warn(cause);

session.close();

}

} 也可按照官方例子httpserverhttp://mina.apache.org/report/trunk/xref/org/apache/mina/example/httpserver/把代码写好,官方例子是1.x的,我在附件里把例子里的import错误改过来 另外为了好测试,把ServerHandler.java改为(粗体那段是我加的): 4.打成jar包先运行一下工程点击工程右键->export->Runable jar file->选择“server-HelloServer”(若是没有的话先运行一下工程就有了)->finish5.测试把jar上传到linux(192.168.13.120)下执行: 在另外一台机器(192.168.13.105)上测试:1)1个连接,10万次请求

ab -n 100000 http://192.168.13.120:8080/测试结果:

Concurrency Level: 1

Time taken for tests: 117.718673 seconds

Complete requests: 100000

Failed requests: 0

Write errors: 0

Total transferred: 39000000 bytes

HTML transferred: 6400000 bytes

Requests per second: 849.48 [#/sec] (mean)

Time per request: 1.177 [ms] (mean)

Time per request: 1.177 [ms] (mean, across all concurrent requests)

Transfer rate: 323.53 [Kbytes/sec] receivedserver的负载

load average: 0.07, 0.02, 0.002)100个连接并发.10万次请求

ab -n 100000 -c 100 http://192.168.13.120:8080/

Concurrency Level: 100

Time taken for tests: 9.999371 seconds

Complete requests: 100000

Failed requests: 0

Write errors: 0

Total transferred: 39003900 bytes

HTML transferred: 6400640 bytes

Requests per second: 10000.63 [#/sec] (mean)

Time per request: 9.999 [ms] (mean)

Time per request: 0.100 [ms] (mean, across all concurrent requests)

Transfer rate: 3809.14 [Kbytes/sec] receivedserver负载

load average: 0.62, 0.19, 0.063)1000个连接并发,10万次请求

ab -n 100000 -c 1000 http://192.168.13.120:8080/结果:

Concurrency Level: 1000

Time taken for tests: 19.697403 seconds

Complete requests: 100000

Failed requests: 0

Write errors: 0

Total transferred: 39000390 bytes

HTML transferred: 6400064 bytes

Requests per second: 5076.81 [#/sec] (mean)

Time per request: 196.974 [ms] (mean)

Time per request: 0.197 [ms] (mean, across all concurrent requests)

Transfer rate: 1933.55 [Kbytes/sec] received负载:

load average: 1.76, 0.43, 0.14结论:mina2.0在合理的负载下1000个并发,最高能达到5076.81rps(Requests per second),基本上能满足需求end 本文出自 “一方有” 博客,请务必保留此出处http://yifangyou.blog.51cto.com/900206/615931

相关推荐:http server 性能测试

这些日子写了一个http server,需要测试下性能。搜索了下,下面几个可以用来测试server的性能1. httperf 例如 httperf --server 192.168.11.101 --uri '/' --port 8080 --method GET --num-calls 5000 --ra 102. ab 例如 ab -c 10 -

快照源:http://yifangyou.blog.51cto.com/900206/615931/