TestStorageMessage.java
2.67 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
package com.dianping.cat.demo;
import java.util.Random;
import org.junit.Test;
import com.dianping.cat.Cat;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.message.spi.internal.DefaultMessageTree;
public class TestStorageMessage {
private String JDBC_CONNECTION = "jdbc:mysql://%s:3306/%s?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true";
@Test
public void testCross() throws Exception {
String serverIp = "10.10.10.1";
while (true) {
for (int i = 0; i < 2; i++) {
sendCacheMsg("cache-1", "user-" + i, "get", serverIp + i);
sendCacheMsg("cache-1", "user-" + i, "remove", serverIp + i);
sendCacheMsg("cache-1", "user-" + i, "add", serverIp + i);
sendCacheMsg("cache-1", "user-" + i, "mGet", serverIp + i);
sendSQLMsg("sql-1", "user-" + i, "select", serverIp + i);
sendSQLMsg("sql-1", "user-" + i, "insert", serverIp + i);
sendSQLMsg("sql-1", "user-" + i, "delete", serverIp + i);
sendSQLMsg("sql-1", "user-" + i, "update", serverIp + i);
sendCacheMsg("cache-2", "user-" + i, "get", serverIp + i);
sendCacheMsg("cache-2", "user-" + i, "add", serverIp + i);
sendCacheMsg("cache-2", "user-" + i, "remove", serverIp + i);
sendCacheMsg("cache-2", "user-" + i, "mGet", serverIp + i);
sendSQLMsg("sql-2", "user-" + i, "select", serverIp + i);
sendSQLMsg("sql-2", "user-" + i, "update", serverIp + i);
sendSQLMsg("sql-2", "user-" + i, "delete", serverIp + i);
sendSQLMsg("sql-2", "user-" + i, "insert", serverIp + i);
}
Thread.sleep(5);
}
}
private void sendCacheMsg(String name, String domain, String method, String serverIp) throws InterruptedException {
Transaction t = Cat.newTransaction("Cache.memcached-" + name, "oUserAuthLevel:" + method);
Cat.logEvent("Cache.memcached.server", serverIp);
MessageTree tree = Cat.getManager().getThreadLocalMessageTree();
((DefaultMessageTree) tree).setDomain(domain);
Thread.sleep(500 + new Random().nextInt(1000));
t.setStatus(Transaction.SUCCESS);
t.complete();
}
private void sendSQLMsg(String name, String domain, String method, String serverIp) throws InterruptedException {
Transaction t = Cat.newTransaction("SQL", "sql.method");
Cat.logEvent("SQL.Method", method);
Cat.logEvent("SQL.Database", String.format(JDBC_CONNECTION, serverIp, name));
MessageTree tree = Cat.getManager().getThreadLocalMessageTree();
((DefaultMessageTree) tree).setDomain(domain);
Thread.sleep(500 + new Random().nextInt(1000));
int nextInt = new Random().nextInt(3);
if (nextInt % 2 == 0) {
t.setStatus(Transaction.SUCCESS);
} else {
t.setStatus(String.valueOf(nextInt));
}
t.complete();
}
}