Skip to content

Commit d5e72da

Browse files
committed
改进测试数据生成程序的算法-《Java多线程编程实战指南(设计模式篇)》
1 parent 3734698 commit d5e72da

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
授权声明:
3+
本源码系《Java多线程编程实战指南(设计模式篇)》一书(ISBN:978-1-7-121-27006-2,以下称之为“原书”)的配套源码,
4+
欲了解本代码的更多细节,请参考原书。
5+
本代码仅为原书的配套说明之用,并不附带任何承诺(如质量保证和收益)。
6+
以任何形式将本代码之部分或者全部用于营利性用途需经版权人书面同意。
7+
将本代码之部分或者全部用于非营利性用途需要在代码中保留本声明。
8+
任何对本代码的修改需在代码中以注释的形式注明修改人、修改时间以及修改内容。
9+
本代码可以从下URL下载:
10+
https://github.com/Viscent/javamtp
11+
http://www.broadview.com.cn/27006
12+
*/
13+
package io.github.viscent.mtpattern.ch12.ms.example.testdatagen;
14+
15+
import java.text.DecimalFormat;
16+
import java.util.Date;
17+
import java.util.Map;
18+
import java.util.Random;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
public abstract class AbstractRequestFactory implements RequestFactory {
22+
protected final Map<String, Integer> respDelayConf;
23+
protected final AtomicInteger seq = new AtomicInteger(0);
24+
protected final Random reqTimeRnd = new Random();
25+
protected final Random respDelayRnd = new Random();
26+
/**
27+
* 部件内部操作延时随机生成器
28+
*/
29+
protected final Random internalDealyRnd = new Random();
30+
protected final long startTimestamp = new Date().getTime();
31+
private long nowTimestamp = startTimestamp;
32+
protected final DecimalFormat traceIdFormatter = new DecimalFormat("0000000");
33+
34+
private final int maxRequestPerTimeslice;
35+
/**
36+
* 单位时间(10s)内产生的请求数
37+
*/
38+
private int requestGeneratedInTimeSlice = 0;
39+
40+
public AbstractRequestFactory(Map<String, Integer> respDelayConf,
41+
int maxRequestPerTimeslice) {
42+
super();
43+
this.respDelayConf = respDelayConf;
44+
this.maxRequestPerTimeslice = maxRequestPerTimeslice;
45+
}
46+
47+
public long nextRequestTimestamp() {
48+
long requestTimestamp;
49+
50+
requestTimestamp = nowTimestamp + reqTimeRnd.nextInt((10 - 1) * 1000)
51+
/ 1000;
52+
if ((++requestGeneratedInTimeSlice) >= maxRequestPerTimeslice) {
53+
nowTimestamp = nowTimestamp + (10 * 1000);
54+
requestGeneratedInTimeSlice = 0;
55+
56+
}
57+
58+
return requestTimestamp;
59+
}
60+
61+
public int genResponseDelay(String externalDevice) {
62+
return respDelayRnd.nextInt(respDelayConf.get(externalDevice));
63+
}
64+
65+
public int genInternalDelay() {
66+
return internalDealyRnd.nextInt(respDelayConf.get("ESB"));
67+
}
68+
69+
}

0 commit comments

Comments
 (0)