AppSimulator.java
2.42 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
package com.dianping.cat.message;
import static com.dianping.cat.message.Message.SUCCESS;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import com.dianping.cat.Cat;
@RunWith(JUnit4.class)
public class AppSimulator extends CatTestCase {
@Test
public void simulateHierarchyTransaction() throws Exception {
MessageProducer cat = Cat.getProducer();
Transaction t = cat.newTransaction("URL", "WebPage");
String id1 = cat.createMessageId();
String id2 = cat.createMessageId();
try {
// do your business here
t.addData("k1", "v1");
t.addData("k2", "v2");
t.addData("k3", "v3");
Thread.sleep(5);
cat.logEvent("Type1", "Name1", SUCCESS, "data1");
cat.logEvent("Type2", "Name2", SUCCESS, "data2");
cat.logEvent("RemoteCall", "Service1", SUCCESS, id1);
createChildThreadTransaction(id1, cat.createMessageId(), cat.createMessageId());
cat.logEvent("Type3", "Name3", SUCCESS, "data3");
cat.logEvent("RemoteCall", "Service1", SUCCESS, id2);
createChildThreadTransaction(id2, cat.createMessageId(), cat.createMessageId(), cat.createMessageId());
cat.logEvent("Type4", "Name4", SUCCESS, "data4");
cat.logEvent("Type5", "Name5", SUCCESS, "data5");
t.setStatus(SUCCESS);
} catch (Exception e) {
t.setStatus(e);
} finally {
t.complete();
}
}
protected void createChildThreadTransaction(final String id, final String... childIds) {
Thread thread = new Thread() {
@Override
public void run() {
MessageProducer cat = Cat.getProducer();
Transaction t = cat.newTransaction("Service", "service-" + (int) (Math.random() * 10));
// override the message id
Cat.getManager().getThreadLocalMessageTree().setMessageId(id);
try {
// do your business here
t.addData("service data here");
Thread.sleep(5);
cat.logEvent("Type1", "Name1", SUCCESS, "data1");
cat.logEvent("Type2", "Name2", SUCCESS, "data2");
for (String childId : childIds) {
cat.logEvent("RemoteCall", "Service1", SUCCESS, childId);
createChildThreadTransaction(childId);
}
cat.logEvent("Type4", "Name4", SUCCESS, "data4");
cat.logEvent("Type5", "Name5", SUCCESS, "data5");
t.setStatus(SUCCESS);
} catch (Exception e) {
t.setStatus(e);
} finally {
t.complete();
}
}
};
thread.start();
// wait for it to complete
try {
thread.join();
} catch (InterruptedException e) {
// ignore it
}
}
}