org.bdware.dogp.client.AliTestClient07 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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.audit.config.FileStorage;
import org.bdware.doip.cluster.client.DoaClusterClient;
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.doipMessage.DoipResponseCode;
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.Random;
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 AliTestClient07 {
static Logger LOGGER = LogManager.getLogger(AliTestClient07.class);
private BatchStartConfig config;
public static class BatchStartConfig {
public int testCase;
public String routerURI;
public long duration;
public String kvDoId;
public int totalKeys;
public int valueSize;
public int clientCount;
public int clientId;
public String resultFile;
public JsonObject extraConfig;
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/doa-sdk-bundle/doip-audit-tool/input/alitestconf.json";
if (args != null && args.length > 0)
path = args[0];
AliTestClient07 testClient = new AliTestClient07();
testClient.init(path);
testClient.run();
}
String StrForWrite;
private void run() throws Exception {
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < config.valueSize; i++)
sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(r.nextInt(26)));
StrForWrite = sb.toString();
switch (config.testCase) {
case 0:
default:
kv_write();
return;
case 1:
kv_read();
return;
case 2:
kv_search();
return;
case 3:
kv_delete();
return;
// mh_read();
}
}
private void kv_search() throws Exception {
DoipMessageTestCase testCase = new CommonTestCase(config) {
public DoipMessage getTestCase(int i) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(config.kvDoId, BasicOperations.Search.getName());
builder.addAttributes("key", "randomKey_" + (i % config.totalKeys));
DoipMessage msg = builder.create();
return msg;
}
};
runTestCase(config.threadCount, testCase);
}
public void kv_write() throws Exception {
DoipMessageTestCase testCase = new CommonTestCase(config) {
public DoipMessage getTestCase(int i) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(config.kvDoId, BasicOperations.Update.getName());
builder.addAttributes("key", "randomKey_" + (i % config.totalKeys));
builder.setBody(StrForWrite.getBytes());
DoipMessage msg = builder.create();
return msg;
}
};
runTestCase(config.threadCount, testCase);
}
public void kv_read() throws Exception {
DoipMessageTestCase testCase = new CommonTestCase(config) {
@Override
public DoipMessage getTestCase(int i) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(config.kvDoId, BasicOperations.Retrieve.getName());
builder.addAttributes("key", "randomKey_" + (i % config.totalKeys));
DoipMessage msg = builder.create();
return msg;
}
};
runTestCase(config.threadCount, testCase);
}
public void kv_delete() throws Exception {
DoipMessageTestCase testCase = new CommonTestCase(config) {
boolean needContinue = true;
AtomicInteger loaded = new AtomicInteger(0);
@Override
public DoipMessage getTestCase(int i) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(config.kvDoId, BasicOperations.Delete.getName());
builder.addAttributes("key", "randomKey_" + (i % config.totalKeys));
DoipMessage msg = builder.create();
if (loaded.incrementAndGet() == config.totalKeys)
new Thread() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
needContinue = false;
}
}.start();
return msg;
}
};
runTestCase(config.threadCount, testCase);
}
public void init(String path) {
FileStorage storage = new FileStorage(path);
config = new Gson().fromJson(storage.load(), BatchStartConfig.class);
}
static ExecutorService pool = Executors.newFixedThreadPool(8, 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();
boolean needContinue();
void endTestCase();
}
public void sendDoipRequest(AtomicInteger total, AtomicInteger correct, AtomicLong respTime, int offset, DoaClusterClient clusterClient, DoipMessageTestCase testCase) {
for (int i = offset; testCase.needContinue(); i++) {
DoipMessage msg = testCase.getTestCase(i);
long sendTime = System.currentTimeMillis();
try {
clusterClient.sendMessage(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.header.parameters.response == DoipResponseCode.Success)
correct.incrementAndGet();
else {
//MUTE
LOGGER.info("======ERROR=======");
LOGGER.info(JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(msg2)));
LOGGER.info(new String(msg2.body.getDataAsJsonString()));
}
long curr = System.currentTimeMillis();
respTime.addAndGet(curr - sendTime);
} finally {
total.incrementAndGet();
}
}
});
} catch (Exception e) {
e.printStackTrace();
total.incrementAndGet();
}
}
LOGGER.info("Send Done:" + offset);
}
public void runTestCase(int threadCount, DoipMessageTestCase testCase) throws Exception {
final AtomicInteger total = new AtomicInteger(0);
final AtomicInteger correct = new AtomicInteger(0);
final AtomicLong respTime = new AtomicLong(0);
long start = System.currentTimeMillis();
long end = 0;
EndpointConfig endpointConfig = new EndpointConfig();
endpointConfig.routerURI = config.routerURI;
if (config.extraConfig != null)
endpointConfig.extraConfig = config.extraConfig;
DoaClusterClient clusterClient = new DoaClusterClient(endpointConfig);
try {
for (int i = 0; i < threadCount; i++) {
AtomicInteger j = new AtomicInteger(i);
pool.execute(new Runnable() {
@Override
public void run() {
sendDoipRequest(total, correct, respTime, (config.totalKeys / config.clientCount) * config.clientId + (config.totalKeys / (threadCount * config.clientCount)) * j.get(), clusterClient, testCase);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
for (; testCase.needContinue(); ) {
int preTotal = total.get();
long startTime = System.currentTimeMillis();
Thread.sleep(2000);
if (total.get() - preTotal > 0)
LOGGER.info("correct/current: " + correct.get() + "/" + total.get()
+ " deltaTPS:" + (total.get() - preTotal) * 1000 / (System.currentTimeMillis() - startTime) + " averageResp:" + respTime.get() / total.get());
if (startTime - start > config.duration) {
testCase.endTestCase();
}
end = System.currentTimeMillis();
}
LOGGER.info("correct/current/total: " + correct.get() + "/" + total.get());
LOGGER.info("dur:" + (end - start) + " tps:" + "" + (correct.get() * 1000D) / (end - start));
JsonObject jo = new JsonObject();
jo.addProperty("duration", end - 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();
System.exit(0);
}
}