TestHttp.java
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.dianping.cat.demo;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import org.unidal.helper.Files;
import org.unidal.helper.Threads;
import org.unidal.helper.Threads.Task;
import org.unidal.helper.Urls;
public class TestHttp {
@Test
public void testManyThread() throws Exception {
System.setProperty("devMode", "true");
int size = 20;
CountDownLatch latch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
Threads.forGroup("cat").start(new SendEvent(i, latch));
}
String timestamp = new SimpleDateFormat("MM-dd HH:mm:ss.SSS").format(new Date());
System.out.println(String.format("[%s] [INFO] Press any key to stop server ... ", timestamp));
System.in.read();
}
public static class SendEvent implements Task {
private int m_index;
private CountDownLatch m_latch;
public SendEvent(int index, CountDownLatch latch) {
m_index = index;
m_latch = latch;
}
@Override
public void run() {
try {
m_latch.countDown();
m_latch.await();
for (int i = 0; i < 1000000000; i++) {
try {
InputStream in = Urls.forIO().readTimeout(3000).connectTimeout(3000)
.openStream("http://cat.qa.dianpingoa.com/cat/r/");
String content = Files.forIO().readFrom(in, "utf-8");
System.out.println(" id:" + m_index + " seq" + i + " length:" + content.length());
} catch (Exception e) {
e.printStackTrace();
}
}
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public String getName() {
return "index:" + m_index;
}
@Override
public void shutdown() {
}
}
}