Skip to content

Commit

Permalink
改进测试数据生成程序的算法-《Java多线程编程实战指南(设计模式篇)》
Browse files Browse the repository at this point in the history
  • Loading branch information
Viscent committed Mar 20, 2016
1 parent 1c81b4d commit 3734698
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,80 @@
本代码可以从下URL下载:
https://github.com/Viscent/javamtp
http://www.broadview.com.cn/27006
*/
*/

package io.github.viscent.mtpattern.ch12.ms.example.testdatagen;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;

public class ChargingRequestFactory extends AbstractRequestFactory {
public ChargingRequestFactory(Map<String, Integer> respDelayConf,
int timestampMaxOffset) {
super(respDelayConf, timestampMaxOffset);
}

public class ChargingRequestFactory implements RequestFactory {
private final AtomicInteger seq=new AtomicInteger(0);
@Override
public SimulatedRequest newRequest() {
return new SimulatedRequest(){
return new SimulatedRequest() {

@Override
public void printLogs(Logger logger) {
final int internalDelay = genInternalDelay();
LogEntry entry = new LogEntry();
long timeStamp = nextRequestTimestamp();
entry.timeStamp = timeStamp;
entry.recordType = "request";

entry.interfaceType = "SOAP";
entry.interfaceName = "Chg";
entry.operationName = "getPrice";
entry.srcDevice = "OSG";
entry.dstDevice = "ESB";

int traceId = seq.getAndIncrement();
int originalTId = traceId;

entry.traceId = "0020" + traceIdFormatter.format(traceId);

logger.printLog(entry);

timeStamp += internalDelay;
entry.timeStamp = timeStamp;

entry.recordType = "request";
entry.operationName = "getPrice";
entry.srcDevice = "ESB";
entry.dstDevice = "BSS";
traceId = traceId + 1;
entry.traceId = "0021" + traceIdFormatter.format(traceId);
logger.printLog(entry);

timeStamp += genResponseDelay("BSS");
entry.timeStamp = timeStamp;
entry.recordType = "response";
entry.operationName = "getPriceRsp";
entry.srcDevice = "BSS";
entry.dstDevice = "ESB";
traceId = traceId + 3;
entry.traceId = "0021" + traceIdFormatter.format(traceId);
logger.printLog(entry);

timeStamp += internalDelay;
entry.timeStamp = timeStamp;
entry.recordType = "response";
entry.operationName = "getLocationRsp";
entry.srcDevice = "ESB";
entry.dstDevice = "OSG";
entry.traceId = "0020" + traceIdFormatter.format(originalTId + 2);
logger.printLog(entry);

}

@Override
public void printLogs(Logger logger) {
LogEntry entry=new LogEntry();
entry.timeStamp=System.currentTimeMillis();
entry.recordType="request";

entry.interfaceType="SOAP";
entry.interfaceName="Chg";
entry.operationName="getPrice";
entry.srcDevice="OSG";
entry.dstDevice="ESB";
DecimalFormat df=new DecimalFormat("0000000");

int traceId=seq.getAndIncrement();
int originalTId=traceId;

entry.traceId="0020"+df.format(traceId);

logger.printLog(entry);

Random rnd = new Random();
entry.timeStamp+=rnd.nextInt(20);
entry.recordType="request";
entry.operationName="getPrice";
entry.srcDevice="ESB";
entry.dstDevice="BSS";
traceId=traceId+1;
entry.traceId="0021"+df.format(traceId);
logger.printLog(entry);


rnd = new Random();
entry.timeStamp+=rnd.nextInt(600);
entry.recordType="response";
entry.operationName="getPriceRsp";
entry.srcDevice="BSS";
entry.dstDevice="ESB";
traceId=traceId+3;
entry.traceId="0021"+df.format(traceId);
logger.printLog(entry);

rnd = new Random();
entry.timeStamp+=rnd.nextInt(650);
entry.recordType="response";
entry.operationName="getLocationRsp";
entry.srcDevice="ESB";
entry.dstDevice="OSG";
entry.traceId="0020"+df.format(originalTId+2);
logger.printLog(entry);


}

public String getInterfaceName() {
return "Charging";
}

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,80 @@
本代码可以从下URL下载:
https://github.com/Viscent/javamtp
http://www.broadview.com.cn/27006
*/
*/

package io.github.viscent.mtpattern.ch12.ms.example.testdatagen;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;

public class LocationRequestFactory extends AbstractRequestFactory {

public LocationRequestFactory(Map<String, Integer> respDelayConf,
int timestampMaxOffset) {
super(respDelayConf, timestampMaxOffset);
}

public class LocationRequestFactory implements RequestFactory {
private final AtomicInteger seq=new AtomicInteger(0);
@Override
public SimulatedRequest newRequest() {
// TODO Auto-generated method stub
return new SimulatedRequest(){
return new SimulatedRequest() {

@Override
public void printLogs(Logger logger) {
final int internalDelay = genInternalDelay();
LogEntry entry = new LogEntry();
long timeStamp = nextRequestTimestamp();
entry.timeStamp = timeStamp;
entry.recordType = "request";

entry.interfaceType = "REST";
entry.interfaceName = "Location";
entry.operationName = "getLocation";
entry.srcDevice = "OSG";
entry.dstDevice = "ESB";

int traceId = seq.getAndIncrement();
int originalTId = traceId;

entry.traceId = "0020" + traceIdFormatter.format(traceId);

logger.printLog(entry);

timeStamp += internalDelay;
entry.timeStamp = timeStamp;
entry.recordType = "request";
entry.operationName = "getLocation";
entry.srcDevice = "ESB";
entry.dstDevice = "NIG";
traceId = traceId + 1;
entry.traceId = "0021" + traceIdFormatter.format(traceId);
logger.printLog(entry);

timeStamp += genResponseDelay("NIG");
entry.timeStamp = timeStamp;
entry.recordType = "response";
entry.operationName = "getLocationRsp";
entry.srcDevice = "NIG";
entry.dstDevice = "ESB";
traceId = traceId + 3;
entry.traceId = "0021" + traceIdFormatter.format(traceId);
logger.printLog(entry);

timeStamp += internalDelay;
entry.timeStamp = timeStamp;
entry.recordType = "response";
entry.operationName = "getLocationRsp";
entry.srcDevice = "ESB";
entry.dstDevice = "OSG";
entry.traceId = "0020" + traceIdFormatter.format(originalTId + 2);
logger.printLog(entry);

}

@Override
public void printLogs(Logger logger) {
LogEntry entry=new LogEntry();
entry.timeStamp=System.currentTimeMillis();
entry.recordType="request";

entry.interfaceType="REST";
entry.interfaceName="Location";
entry.operationName="getLocation";
entry.srcDevice="OSG";
entry.dstDevice="ESB";
DecimalFormat df=new DecimalFormat("0000000");

int traceId=seq.getAndIncrement();
int originalTId=traceId;

entry.traceId="0020"+df.format(traceId);

logger.printLog(entry);

Random rnd = new Random();
entry.timeStamp+=rnd.nextInt(20);
entry.recordType="request";
entry.operationName="getLocation";
entry.srcDevice="ESB";
entry.dstDevice="NIG";
traceId=traceId+1;
entry.traceId="0021"+df.format(traceId);
logger.printLog(entry);


rnd = new Random();
entry.timeStamp+=rnd.nextInt(100);
entry.recordType="response";
entry.operationName="getLocationRsp";
entry.srcDevice="NIG";
entry.dstDevice="ESB";
traceId=traceId+3;
entry.traceId="0021"+df.format(traceId);
logger.printLog(entry);

rnd = new Random();
entry.timeStamp+=rnd.nextInt(150);
entry.recordType="response";
entry.operationName="getLocationRsp";
entry.srcDevice="ESB";
entry.dstDevice="OSG";
entry.traceId="0020"+df.format(originalTId+2);
logger.printLog(entry);

}

public String getInterfaceName() {
return "Location";
}

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,43 @@
本代码可以从下URL下载:
https://github.com/Viscent/javamtp
http://www.broadview.com.cn/27006
*/
*/

package io.github.viscent.mtpattern.ch12.ms.example.testdatagen;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.SimpleTimeZone;
import java.util.concurrent.atomic.AtomicLong;

public class Logger {
//接口日志输出目标目录

// 接口日志输出目标目录
private static final String LOG_FILE_BASE_DIR = System
.getProperty("java.io.tmpdir") + "/tps/";
private static final String TIME_STAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
private static final String TIME_STAMP_FORMAT1 = "yyyyMMddHHmm";
private int maxLines = 10000;
private AtomicLong linesCount = new AtomicLong(maxLines);
private PrintWriter cachedPwr = null;

public void printLog(LogEntry entry) {
// System.out.println(entry);
// IP address
PrintWriter pwr = this.getWriter();
private final AtomicLong linesCount = new AtomicLong(0);
private volatile PrintWriter cachedPwr = null;
private volatile long now;

public Logger() {
now = System.currentTimeMillis();
try {
cachedPwr = new PrintWriter(new FileWriter(LOG_FILE_BASE_DIR
+ retrieveLogFileName(now)), true);
} catch (IOException e) {
e.printStackTrace();
}
}

public void printLog(LogEntry entry) {
final PrintWriter pwr = cachedPwr;
pwr.print(getUTCTimeStamp(Long.valueOf(entry.timeStamp), TIME_STAMP_FORMAT));
pwr.print('|');

Expand Down Expand Up @@ -72,26 +82,24 @@ public void printLog(LogEntry entry) {
if (count >= maxLines) {
pwr.flush();
pwr.close();
}
}

private PrintWriter getWriter() {

if (linesCount.get() >= maxLines) {
linesCount.set(0);
try {
cachedPwr = new PrintWriter(LOG_FILE_BASE_DIR + retrieveLogFileName());
} catch (FileNotFoundException e) {
long newTimeStamp = System.currentTimeMillis();
if ((newTimeStamp - now) <= 60 * 1000) {
newTimeStamp = now + 60 * 1000;
now = newTimeStamp;
}
cachedPwr = new PrintWriter(new FileWriter(LOG_FILE_BASE_DIR
+ retrieveLogFileName(newTimeStamp)), true);
} catch (IOException e) {
e.printStackTrace();
}
linesCount.set(0);
}
return cachedPwr;
}

private String retrieveLogFileName() {
private String retrieveLogFileName(long timeStamp) {

return "ESB_interface_"
+ getUTCTimeStamp(System.currentTimeMillis(), TIME_STAMP_FORMAT1)
return "ESB_interface_" + getUTCTimeStamp(timeStamp, TIME_STAMP_FORMAT1)
+ ".log";
}

Expand All @@ -104,4 +112,4 @@ private static String getUTCTimeStamp(long timeStamp, String format) {
String tempTs = sdf.format(calendar.getTime());
return tempTs;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

public interface SimulatedRequest {
public void printLogs(Logger logger);
public String getInterfaceName();
}
Loading

0 comments on commit 3734698

Please sign in to comment.