-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBigDataTest.java
More file actions
92 lines (75 loc) · 2.36 KB
/
BigDataTest.java
File metadata and controls
92 lines (75 loc) · 2.36 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.msgpack.rpc;
import org.msgpack.*;
import org.msgpack.rpc.dispatcher.*;
import org.msgpack.rpc.loop.*;
import java.util.*;
import junit.framework.*;
import org.junit.Test;
import org.msgpack.type.Value;
import org.msgpack.type.ValueFactory;
public class BigDataTest extends TestCase {
private static String getBigString() {
StringBuilder sb = new StringBuilder(1024 * 1024); // 1M
Random random = new Random();
for(int i = 0;i < 1024 * 1024;i++){
sb.append( (char)('a' + random.nextInt(26)));
}
return sb.toString();
}
private static Value BIG_DATA = ValueFactory.createRawValue(getBigString());
public static class BigDataDispatcher implements Dispatcher {
public void dispatch(Request request) {
assertEquals(BIG_DATA,request.getArguments().asArrayValue().get(0) );
request.sendResult(BIG_DATA);
}
}
@Test
public void testSyncBigDataLoad() throws Exception {
MessagePack messagePack = new MessagePack();
EventLoop loop = EventLoop.start(messagePack);
Server svr = new Server(loop);
Client c = new Client("127.0.0.1", 19851, loop);
c.setRequestTimeout(10);
try {
svr.serve(new BigDataDispatcher());
svr.listen(19851);
int num = 5;
long start = System.currentTimeMillis();
for(int i=0; i < num; i++) {
Value result = c.callApply("test", new Object[]{BIG_DATA});
assertEquals(BIG_DATA, result);
}
long finish = System.currentTimeMillis();
double result = num / ((double)(finish - start) / 1000);
System.out.println("sync: "+result+" calls per sec");
} finally {
svr.close();
c.close();
loop.shutdown();
}
}
@Test
public void testAsyncBigDataLoad() throws Exception {
EventLoop loop = EventLoop.start();
Server svr = new Server(loop);
Client c = new Client("127.0.0.1", 19852, loop);
c.setRequestTimeout(100);//
try {
svr.serve(new BigDataDispatcher());
svr.listen(19852);
int num = 10;
long start = System.currentTimeMillis();
for(int i=0; i < num-1; i++) {
c.notifyApply("test", new Object[]{BIG_DATA});
}
c.callApply("test", new Object[]{BIG_DATA});
long finish = System.currentTimeMillis();
double result = num / ((double)(finish - start) / 1000);
System.out.println("async: "+result+" calls per sec");
} finally {
svr.close();
c.close();
loop.shutdown();
}
}
}