HttpSimpleClient优化
This commit is contained in:
117
src/test/java/org/redkale/test/http/HttpSimpleClientTest.java
Normal file
117
src/test/java/org/redkale/test/http/HttpSimpleClientTest.java
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.test.http;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.boot.Application;
|
||||
import org.redkale.net.AsyncIOGroup;
|
||||
import org.redkale.net.http.HttpServer;
|
||||
import org.redkale.net.http.HttpSimpleClient;
|
||||
import org.redkale.net.http.HttpSimpleRequest;
|
||||
import org.redkale.util.AnyValue;
|
||||
import org.redkale.util.ResourceFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class HttpSimpleClientTest {
|
||||
|
||||
private static int port = 0;
|
||||
|
||||
private static Application application;
|
||||
|
||||
private static ResourceFactory factory;
|
||||
|
||||
private static HttpServer server;
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
HttpSimpleClientTest test = new HttpSimpleClientTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
runServer();
|
||||
//Utility.sleep(50000);
|
||||
final AsyncIOGroup asyncGroup = new AsyncIOGroup(8192, 16);
|
||||
asyncGroup.start();
|
||||
HttpSimpleClient client = HttpSimpleClient.create(asyncGroup);
|
||||
InetSocketAddress addr = new InetSocketAddress("127.0.0.1", port);
|
||||
{
|
||||
HttpSimpleRequest req = HttpSimpleRequest.createPath("/test").param("id", 100);
|
||||
System.out.println(client.getAsync("http://127.0.0.1:" + port + req.getPath() + "?id=100").join());
|
||||
System.out.println(client.sendAsync(addr, req).join());
|
||||
}
|
||||
final int count = 10;
|
||||
{
|
||||
final CountDownLatch cdl = new CountDownLatch(count);
|
||||
for (int i = 100; i < 100 + count; i++) {
|
||||
final int index = i;
|
||||
HttpSimpleRequest req = HttpSimpleRequest.createPath("/test").param("id", index);
|
||||
client.getAsync("http://127.0.0.1:" + port + req.getPath() + "?id=" + index).whenComplete((v, t) -> {
|
||||
cdl.countDown();
|
||||
Assertions.assertEquals("ok-" + index, new String((byte[]) v.getResult()));
|
||||
});
|
||||
}
|
||||
cdl.await();
|
||||
System.out.println("结束并发1");
|
||||
}
|
||||
{
|
||||
final CountDownLatch cdl = new CountDownLatch(count);
|
||||
for (int i = 100; i < 100 + count; i++) {
|
||||
final int index = i;
|
||||
HttpSimpleRequest req = HttpSimpleRequest.createPath("/test").param("id", index);
|
||||
client.sendAsync(addr, req).whenComplete((v, t) -> {
|
||||
cdl.countDown();
|
||||
System.out.println("输出: " + new String((byte[]) v.getResult()));
|
||||
Assertions.assertEquals("ok-" + index, new String((byte[]) v.getResult()));
|
||||
});
|
||||
}
|
||||
cdl.await();
|
||||
System.out.println("结束并发2");
|
||||
}
|
||||
server.shutdown();
|
||||
}
|
||||
|
||||
private static void runServer() throws Exception {
|
||||
application = Application.create(true);
|
||||
factory = application.getResourceFactory();
|
||||
factory.register("", Application.class, application);
|
||||
final CountDownLatch cdl = new CountDownLatch(1);
|
||||
final AsyncIOGroup asyncGroup = new AsyncIOGroup(8192, 16);
|
||||
asyncGroup.start();
|
||||
new Thread() {
|
||||
{
|
||||
setName("Thread-Server-01");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
AnyValue.DefaultAnyValue conf = new AnyValue.DefaultAnyValue();
|
||||
conf.addValue("host", "0.0.0.0");
|
||||
conf.addValue("port", "" + port);
|
||||
conf.addValue("protocol", "HTTP");
|
||||
conf.addValue("maxbody", "" + (100 * 1024 * 1024));
|
||||
server = new HttpServer(factory);
|
||||
server.init(conf);
|
||||
server.addHttpServlet(new HttpSimpleServlet(), "/test");
|
||||
server.start();
|
||||
port = server.getSocketAddress().getPort();
|
||||
cdl.countDown();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
cdl.await();
|
||||
}
|
||||
}
|
||||
23
src/test/java/org/redkale/test/http/HttpSimpleServlet.java
Normal file
23
src/test/java/org/redkale/test/http/HttpSimpleServlet.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.test.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.redkale.net.http.HttpMapping;
|
||||
import org.redkale.net.http.HttpRequest;
|
||||
import org.redkale.net.http.HttpResponse;
|
||||
import org.redkale.net.http.HttpServlet;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class HttpSimpleServlet extends HttpServlet {
|
||||
|
||||
@HttpMapping(url = "/test")
|
||||
public void test(HttpRequest req, HttpResponse resp) throws IOException {
|
||||
System.out.println("运行到test方法了, id=" + req.getParameter("id"));
|
||||
resp.finish("ok-" + req.getParameter("id", "0"));
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ public class SncpTest {
|
||||
callbean = service.insert(callbean);
|
||||
System.out.println("bean: " + callbean);
|
||||
System.out.println("\r\n\r\n\r\n\r\n---------------------------------------------------");
|
||||
Thread.sleep(200);
|
||||
Utility.sleep(200);
|
||||
final int count = main ? 40 : 11;
|
||||
final CountDownLatch cld = new CountDownLatch(count);
|
||||
final AtomicInteger ai = new AtomicInteger();
|
||||
@@ -142,7 +142,7 @@ public class SncpTest {
|
||||
}
|
||||
return;
|
||||
}
|
||||
Thread.sleep(200);
|
||||
Utility.sleep(200);
|
||||
final CountDownLatch cld2 = new CountDownLatch(1);
|
||||
long s2 = System.currentTimeMillis();
|
||||
final CompletableFuture<String> future = service.queryResultAsync(callbean);
|
||||
|
||||
Reference in New Issue
Block a user