模拟HTTP POST 请求
使用httpclient
[1]
或者netty-http-client[5]
HTTP POST GZIP DATA
String foo = "value";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(foo.getBytes("UTF-8"));
} finally {
if (gzos != null) try { gzos.close(); } catch (IOException ignore) {};
}
byte[] fooGzippedBytes = baos.toByteArray();
MultipartEntity entity = new MultipartEntity();
entity.addPart("foo", new InputStreamBody(new ByteArrayInputStream(fooGzippedBytes), "foo.txt"));
HttpPost post = new HttpPost("http://example.com/some");
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
// ...
final byte[] compressedData = compress(data);
final ByteArrayEntity bEntity = new ByteArrayEntity(compressedData);
bEntity.setChunked(true);
httppost.addHeader("Byte-Encoding", "gzip");
httppost.addHeader("Data-length", String.valueOf(compressedData.length));
httppost.setEntity(bEntity);
final HttpResponse response = httpclient.execute(httppost);
两者的不同是上面示例使用了
MultipartEntity
而下面的使用
MultipartEntity
在具体情境中可能要区分两者的区别。在本次测试中使用byte的。
[2]
多线程还是多路复用
当使用多线程的时候,即使Java新起了这么多的线程但是底层IO还是无法开启这么多。所以还是选择多路复用。
使用WRK测试
/root/wrk-master/wrk -c500 -t8 -d30s http://127.0.0.1:8080/test/-s /root/wrk-master/scripts/post.lua
结果查看
cat catalina.out |grep 16:04:22|wc -l 这是查看QPS的
遇到的问题
1. Java测试程序本身并不能真正做到新起2000个线程?
[3][4]
测试工具
- jmeter
- apache ab http://httpd.apache.org/docs/2.2/programs/ab.html
- wrk https://github.com/wg/wrk
参考资料
[5]: https://github.com/timboudreau/netty-http-client