Permalink
Browse files

better test code

  • Loading branch information...
1 parent d4b594e commit bf2e5f0965e349c34edf53a2d87434f9b79402dc @shenfeng committed Feb 2, 2012
Showing with 63 additions and 50 deletions.
  1. +1 −1 conf/shenfeng.me
  2. +61 −48 test/java/me/shenfeng/MakeupIdelConnection.java
  3. +1 −1 test/java/me/shenfeng/PerformanceBench.java
View
@@ -2,7 +2,7 @@ server {
server_name shenfeng.me;
access_log /var/log/nginx/blog.access.log;
location /d {
- proxy_pass http://127.0.0.1:9090/;
+ proxy_pass http://127.0.0.1:9090/d;
access_log off;
}
location /dict {
@@ -2,63 +2,85 @@
import java.io.IOException;
import java.net.InetSocketAddress;
+import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
-import java.util.ArrayList;
import java.util.Iterator;
-import java.util.List;
import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
+
+// for i in `seq 21 37`; do sudo ifconfig eth0:$i 192.168.1.$i up ; done
+
+// /etc/security/limits.conf
+// * - nofile 999999
+
+// sudo sysctl -w net.ipv4.ip_local_port_range="1025 65535"
+
+// cat /proc/net/sockstat
public class MakeupIdelConnection {
+ final static int STEPS = 10;
+ final static int connectionPerIP = 50000;
+
public static void main(String[] args) throws IOException {
- int totalIdleConnection = 5000;
final Selector selector = Selector.open();
- final AtomicBoolean running = new AtomicBoolean(true);
-
- InetSocketAddress addrs[] = {
- new InetSocketAddress("127.0.0.1", 9090),
- new InetSocketAddress("192.168.1.114", 9090) };
+ InetSocketAddress locals[] = {
+ // new InetSocketAddress("127.0.0.1", 9090),
+ // new InetSocketAddress("192.168.1.114", 9090),
+ // new InetSocketAddress("192.168.1.21", 9090),
+ new InetSocketAddress("192.168.1.22", 9090),
+ new InetSocketAddress("192.168.1.23", 9090),
+ new InetSocketAddress("192.168.1.24", 9090),
+ new InetSocketAddress("192.168.1.25", 9090),
+ new InetSocketAddress("192.168.1.26", 9090),
+ new InetSocketAddress("192.168.1.27", 9090),
+ new InetSocketAddress("192.168.1.28", 9090),
+ new InetSocketAddress("192.168.1.29", 9090),
+ new InetSocketAddress("192.168.1.30", 9090),
+ new InetSocketAddress("192.168.1.31", 9090),
+ new InetSocketAddress("192.168.1.32", 9090),
+ new InetSocketAddress("192.168.1.33", 9090),
+ new InetSocketAddress("192.168.1.34", 9090),
+ new InetSocketAddress("192.168.1.35", 9090),
+ new InetSocketAddress("192.168.1.36", 9090),
+ new InetSocketAddress("192.168.1.37", 9090),
- final List<SocketChannel> all = new ArrayList<SocketChannel>(
- totalIdleConnection * addrs.length);
+ };
- // sudo echo 1025 65535 > /proc/sys/net/ipv4/ip_local_port_range
- // sudo sysctl -w net.ipv4.ip_local_port_range="1025 65535"
- // cat /proc/net/sockstat
+ // InetSocketAddress remote = new InetSocketAddress("192.168.1.114",
+ // 9090);
- for (int i = 0; i < totalIdleConnection; ++i) {
- for (InetSocketAddress addr : addrs) {
- SocketChannel ch = SocketChannel.open();
- ch.socket().setReuseAddress(true);
- ch.configureBlocking(false);
- ch.register(selector, SelectionKey.OP_CONNECT,
- SelectAttachment.next());
- ch.connect(addr);
- all.add(ch);
+ long start = System.currentTimeMillis();
+ int connected = 0;
+ int currentConnectionPerIP = 0;
+ while (true) {
+ if (System.currentTimeMillis() - start > 1000 * 60 * 10) {
+ break;
}
- }
- Timer timer = new Timer();
- timer.schedule(new TimerTask() {
- public void run() {
- running.set(false);
- selector.wakeup();
- }
- }, TimeUnit.SECONDS.toMillis(15));
+ for (int i = 0; i < connectionPerIP / STEPS
+ && currentConnectionPerIP < connectionPerIP; ++i, ++currentConnectionPerIP) {
+ for (InetSocketAddress addr : locals) {
+ SocketChannel ch = SocketChannel.open();
+ ch.configureBlocking(false);
- int connected = 0;
+ Socket s = ch.socket();
+ s.setReuseAddress(true);
+ // s.bind(addr);
- while (running.get()) {
- int select = selector.select();
+ ch.register(selector, SelectionKey.OP_CONNECT);
+ ch.connect(addr);
+ }
+ }
+
+ int select = selector.select(1000 * 10); // 10s
if (select > 0) {
+ System.out.println("select return: " + select
+ + " events ; current connection per ip: "
+ + currentConnectionPerIP);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
@@ -69,24 +91,15 @@ public void run() {
if (ch.finishConnect()) {
++connected;
if (connected
- % (totalIdleConnection * addrs.length / 10) == 0) {
+ % (connectionPerIP * locals.length / 10) == 0) {
System.out.println("connected: " + connected);
}
+ key.interestOps(SelectionKey.OP_READ);
}
}
}
selectedKeys.clear();
}
}
- System.out.println("close all connection");
- for (SocketChannel ch : all) {
- try {
- ch.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println("close all connection ok");
- timer.cancel(); // shutdown jvm
}
}
@@ -58,7 +58,7 @@ public static String readLine(ByteBuffer buffer) {
public static void main(String[] args) throws IOException {
- int concurrency = 1024 * 5;
+ int concurrency = 1024 * 3;
long totalByteReceive = 0;
int total = 200000;
int remaining = total;

0 comments on commit bf2e5f0

Please sign in to comment.