org.bdware.dogp.client.AliTestClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.dogp.client;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.audit.config.FileStorage;
import org.bdware.doip.cluster.ClusterDoaClient;
import org.bdware.doip.cluster.client.DoipClusterClient;
import org.bdware.doip.codec.JsonDoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.client.DoipMessageCallback;
import org.bdware.sc.util.JsonUtil;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
public class AliTestClient {
AuditIrpClient irpClient;
static Logger LOGGER = LogManager.getLogger(AliTestClient.class);
private EndpointConfig endpointConfig;
private BatchStartConfig config;
public static class BatchStartConfig {
public String testOriginalIdPrefix;
public int testCase;
String routerAgentAddress;
String serverInfo;
String bcoId;
public String bdoId;
int totalCount;
int threadCount;
}
public static void main(String[] args) throws Exception {
LOGGER.info("usage: java -cp ./libs/*:doip-audit-tool.jar org.bdware.dogp.client.AliTestClient ${path} ${0/1}");
String path = "/Users/huaqiancai/BDWare/bdware-address/raw-storage/alitestconf.json";
if (args != null && args.length > 0)
path = args[0];
AliTestClient testClient = new AliTestClient();
testClient.init(path);
testClient.run();
}
private void run() throws Exception {
if (config.testCase == 0)
createDOL();
else
retrieveDOL();
}
ClusterDoaClient clusterDoaClient;
public void init(String path) {
FileStorage storage = new FileStorage(path);
endpointConfig = storage.loadAsEndpointConfig();
config = new Gson().fromJson(storage.load(), BatchStartConfig.class);
clusterDoaClient = new ClusterDoaClient("", endpointConfig);
}
static ExecutorService pool = Executors.newFixedThreadPool(16, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
interface DoipMessageTestCase {
DoipMessage getTestCase(int i);
String getResultFile();
}
AtomicLong respTime = new AtomicLong(0);
public void reqHashDatas(int totalCount, AtomicLong end, AtomicInteger total, AtomicInteger correct, int offset, int count, DoipClusterClient clusterClient, DoipMessageTestCase testCase) {
CreateDOLParam param = new CreateDOLParam();
param.entranceDOL = "tcp://127.0.0.1:21035";
param.originalId = "bdtest/CodeRepository";
param.spaceDOL = "abcdefghijklmn";
for (int i = offset; i < offset + count; i++) {
DoipMessage msg = testCase.getTestCase(i);
long sendTime = System.currentTimeMillis();
try {
clusterClient.sendRawMessage(msg, new DoipMessageCallback() {
@Override
public void onResult(DoipMessage msg2) {
// LOGGER.info(JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(msg2)));
// LOGGER.info(new String(msg2.body.getDataAsJsonString()));
try {
if (msg2.body.getDataAsJsonString().equals("success"))
correct.incrementAndGet();
else {
LOGGER.info("======ERROR=======");
LOGGER.info(JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(msg2)));
LOGGER.info(new String(msg2.body.getDataAsJsonString()));
}
respTime.addAndGet(System.currentTimeMillis() - sendTime);
} finally {
total.incrementAndGet();
if (total.get() == totalCount) {
end.set(System.currentTimeMillis());
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
total.incrementAndGet();
}
}
LOGGER.info("Send Done:" + offset);
}
public void runTestCase(int totalCount, int threadCount, DoipMessageTestCase testCase) throws Exception {
final AtomicInteger total = new AtomicInteger(0);
final AtomicInteger correct = new AtomicInteger(0);
long start = System.currentTimeMillis();
AtomicLong end = new AtomicLong();
String doid = config.bdoId;
DoipClusterClient clusterClient = clusterDoaClient.createOrGetClusterClient(doid);
try {
for (int i = 0; i < threadCount; i++) {
AtomicInteger j = new AtomicInteger(i);
pool.execute(new Runnable() {
@Override
public void run() {
reqHashDatas(totalCount, end, total, correct, (totalCount / threadCount) * j.get(), (totalCount / threadCount), clusterClient, testCase);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
for (; total.get() < totalCount; ) {
int preTotal = total.get();
long startTime = System.currentTimeMillis();
Thread.sleep(2000);
if (total.get() - preTotal > 0)
LOGGER.info("correct/current/total: " + correct.get() + "/" + total.get() + "/" + totalCount
+ " deltaTPS:" + (total.get() - preTotal) * 1000 / (System.currentTimeMillis() - startTime) + " averageResp:" + respTime.get() / total.get());
}
LOGGER.info("correct/current/total: " + correct.get() + "/" + total.get() + "/" + totalCount);
LOGGER.info("dur:" + (end.get() - start) + " tps:" + "" + (correct.get() * 1000D) / (end.get() - start));
JsonObject jo = new JsonObject();
jo.addProperty("duration", end.get() - start);
jo.addProperty("responseTime", respTime);
jo.addProperty("totalCount", total.get());
jo.addProperty("correctCount", correct.get());
FileOutputStream output = new FileOutputStream(testCase.getResultFile());
output.write(jo.toString().getBytes(StandardCharsets.UTF_8));
output.close();
clusterClient.closeAll();
}
public void createDOL() throws Exception {
DoipMessageTestCase testCase = new DoipMessageTestCase() {
@Override
public DoipMessage getTestCase(int i) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(config.bdoId, BasicOperations.Update.getName());
DoipMessage msg = builder.create();
CreateDOLParam param = new CreateDOLParam();
param.entranceDOL = "tcp://127.0.0.1:21035";
param.spaceDOL = "abcdefghijklmn";
param.originalId = config.testOriginalIdPrefix + "/" + i;
param.dol = param.calculateDOL();
msg.header.parameters.attributes = JsonParser.parseString(new Gson().toJson(param)).getAsJsonObject();
msg.header.parameters.attributes.addProperty("address", param.dol);
msg.header.parameters.attributes.addProperty("dol", param.dol);
return msg;
}
@Override
public String getResultFile() {
return "./result-case0.txt";
}
};
runTestCase(config.totalCount, config.threadCount, testCase);
}
public void retrieveDOL() throws Exception {
DoipMessageTestCase testCase = new DoipMessageTestCase() {
@Override
public DoipMessage getTestCase(int i) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(config.bdoId, BasicOperations.Retrieve.getName());
CreateDOLParam param = new CreateDOLParam();
param.entranceDOL = "tcp://127.0.0.1:21035";
param.spaceDOL = "abcdefghijklmn";
param.originalId = config.testOriginalIdPrefix + "/" + i;
param.dol = param.calculateDOL();
builder.addAttributes("originalId", param.originalId);
builder.addAttributes("address", param.dol);
builder.addAttributes("dol", param.dol);
DoipMessage msg = builder.create();
return msg;
}
@Override
public String getResultFile() {
return "./result-case1.txt";
}
};
runTestCase(config.totalCount, config.threadCount, testCase);
}
}