TestBusinessMessage.java
6.14 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.dianping.cat.demo;
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import org.unidal.helper.Threads;
import org.unidal.helper.Threads.Task;
import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.message.spi.internal.DefaultMessageTree;
public class TestBusinessMessage {
private static final String Puma = "PumaServer";
private static final String PayOrder = "PayOrder";
@Test
public void testMutilThead() throws Exception {
int total = 10;
CountDownLatch latch = new CountDownLatch(total);
for (int i = 0; i < 10; i++) {
Threads.forGroup("cat").start(new CatThread(latch, i));
}
Thread.sleep(10000);
}
public class CatThread implements Task {
private CountDownLatch m_latch;
private int m_count;
public CatThread(CountDownLatch latch, int count) {
m_latch = latch;
m_count = count;
}
@Override
public void run() {
m_latch.countDown();
try {
m_latch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
Transaction transaction = Cat.newTransaction("test", "test" + m_count);
transaction.setStatus(Message.SUCCESS);
transaction.complete();
}
}
@Override
public String getName() {
return "cat-test-thread";
}
@Override
public void shutdown() {
}
}
@Test
public void testEvent() throws Exception {
for (int i = 0; i < 1000; i++) {
Cat.logError(new NullPointerException());
}
Thread.sleep(10000);
}
@Test
public void testMetric() throws Exception {
for (int i = 0; i < 1000; i++) {
Transaction t = Cat.newTransaction("URL", "/index");
Cat.logEvent("RemoteLink", "sina", Event.SUCCESS, "http://sina.com.cn/");
t.addData("channel=channel" + i % 5);
Cat.logMetricForCount("Receipt Verify Success");
Cat.logMetricForCount("Receipt Verify Success 2", 2);
Cat.logMetricForDuration("Receipt Verify Druation", 10);
Cat.logMetricForSum("sum Value", 20);
Cat.logMetricForSum("sum Value2", 20, 2);
t.complete();
}
Thread.sleep(1000);
}
@Test
public void test() throws Exception {
for (int i = 0; i < 1000; i++) {
Transaction t = Cat.newTransaction("URL", "/index");
Cat.logEvent("RemoteLink", "sina", Event.SUCCESS, "http://sina.com.cn/");
t.addData("channel=channel" + i % 5);
Cat.logMetricForCount("Receipt Verify Success");
Cat.logMetricForCount("Receipt Verify Success 2", 2);
Cat.logMetricForDuration("Receipt Verify Druation", 10);
Cat.logMetricForSum("sum Value", 20);
Cat.logMetricForSum("sum Value2", 20, 2);
t.complete();
}
for (int i = 0; i < 900; i++) {
Transaction t = Cat.newTransaction("URL", "/detail");
MessageTree tree = (MessageTree) Cat.getManager().getThreadLocalMessageTree();
tree.setDomain(Puma);
t.addData("channel=channel" + i % 5);
t.complete();
}
for (int i = 0; i < 500; i++) {
Transaction t = Cat.newTransaction("URL", "/order/submitOrder");
MessageTree tree = (MessageTree) Cat.getManager().getThreadLocalMessageTree();
tree.setDomain(PayOrder);
Cat.logMetric("order", "quantity", 1, "channel", "channel" + i % 5);
Cat.logMetric("payment.pending", "amount", i, "channel", "channel" + i % 5);
Cat.logMetric("payment.success", "amount", i, "channel", "channel" + i % 5);
t.addData("channel=channel" + i % 5);
t.complete();
}
for (int i = 0; i < 1000; i++) {
Transaction t = Cat.newTransaction("URL", "t");
Cat.logEvent("RemoteLink", "sina", Event.SUCCESS, "http://sina.com.cn/");
t.complete();
}
for (int i = 0; i < 900; i++) {
Transaction t = Cat.newTransaction("URL", "e");
t.complete();
}
for (int i = 0; i < 500; i++) {
Transaction t = Cat.newTransaction("URL", "home");
Cat.logMetric("order", "quantity", 1, "channel", "channel" + i % 5);
Cat.logMetric("payment.pending", "amount", i, "channel", "channel" + i % 5);
Cat.logMetric("payment.success", "amount", i, "channel", "channel" + i % 5);
t.complete();
}
Thread.sleep(1000);
}
@Test
public void test2() throws Exception {
while (true) {
for (int i = 0; i < 1000; i++) {
Transaction t = Cat.newTransaction("URL", "/index");
Cat.logEvent("RemoteLink", "sina", Event.SUCCESS, "http://sina.com.cn/");
t.addData("channel=channel" + i % 5);
t.complete();
}
for (int i = 0; i < 900; i++) {
Transaction t = Cat.newTransaction("URL", "/detail");
t.addData("channel=channel" + i % 5);
t.complete();
}
for (int i = 0; i < 500; i++) {
Transaction t = Cat.newTransaction("URL", "/order/submitOrder");
Cat.logMetric("order", "quantity", 1, "channel", "channel" + i % 5);
Cat.logMetric("payment.pending", "amount", i, "channel", "channel" + i % 5);
Cat.logMetric("payment.success", "amount", i, "channel", "channel" + i % 5);
t.addData("channel=channel" + i % 5);
t.complete();
}
Thread.sleep(1000);
break;
}
}
@Test
public void test3() throws InterruptedException {
for (int i = 0; i < 500; i++) {
Transaction t = Cat.newTransaction("test", "test");
Cat.logMetricForCount("MemberCardSuccess");
Cat.logMetricForCount("MemberCardFail", 2);
MessageTree tree = Cat.getManager().getThreadLocalMessageTree();
((DefaultMessageTree) tree).setDomain("MobileMembercardMainApiWeb");
t.complete();
}
Thread.sleep(100000);
}
public void sample() {
String pageName = "";
String serverIp = "";
Transaction t = Cat.newTransaction("URL", pageName); // 创建一个Transaction
try {
// 记录一个事件
Cat.logEvent("URL.Server", serverIp, Event.SUCCESS, "ip=" + serverIp + "&...");
// 记录一个业务指标,记录订单次数
Cat.logMetricForCount("OrderCount");
// 记录一个业务指标,记录支付次数
Cat.logMetricForCount("PayCount");
yourBusiness();// 自己业务代码
t.setStatus(Transaction.SUCCESS);// 设置状态
} catch (Exception e) {
t.setStatus(e);// 设置错误状态
} finally {
t.complete();// 结束Transaction
}
}
private void yourBusiness() {
}
}