Skip to content

Commit

Permalink
feat(example): support benchmark for register model
Browse files Browse the repository at this point in the history
1. support benchmark for register model

Closes openmessaging#289
  • Loading branch information
TheR1sing3un committed May 25, 2023
1 parent 3a34a5d commit d781d09
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2017-2022 The DLedger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.openmessaging.storage.dledger.example.register;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Parameters(commandDescription = "Benchmark the RegisterDLedger")
public class BenchmarkCommand extends BaseCommand {

private static Logger logger = LoggerFactory.getLogger(BenchmarkCommand.class);

@Parameter(names = {"--group", "-g"}, description = "Group of this server")
private String group = "default";

@Parameter(names = {"--peers", "-p"}, description = "Peer info of this server")
private String peers = "n0-localhost:20911";

@Parameter(names = {"--clientNum", "-c"}, description = "Client number")
private int clientNum = 10;

@Parameter(names = {"--ops", "-o"}, description = "Operation num per client")
private long opsPerClient = 10000;

@Parameter(names = {"--benchmarkType", "-t"}, description = "Benchmark type, [Write] or [Read]")
private RegisterBenchmark.BenchmarkType benchmarkType = RegisterBenchmark.BenchmarkType.Write;

@Override
public void doCommand() {
RegisterBenchmark benchmark = new RegisterBenchmark(group, peers, clientNum, opsPerClient, benchmarkType);
try {
benchmark.start();
} catch (Exception e) {
logger.error("benchmark error", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void main(String[] args) {
commands.put("server", new ServerCommand());
commands.put("write", new WriteCommand());
commands.put("read", new ReadCommand());
commands.put("benchmark", new BenchmarkCommand());
JCommander.Builder builder = JCommander.newBuilder();
commands.forEach((k, v) -> builder.addCommand(k, v));
JCommander jc = builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2017-2022 The DLedger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.openmessaging.storage.dledger.example.register;

import io.openmessaging.storage.dledger.example.register.protocol.RegisterReadResponse;
import io.openmessaging.storage.dledger.example.register.protocol.RegisterWriteResponse;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RegisterBenchmark {

private static Logger logger = LoggerFactory.getLogger(RegisterBenchmark.class);

private String group;

private String peers;

private int clientNum;

private long operationNumPerClient;

private BenchmarkType benchmarkType;

public enum BenchmarkType {
Write,
Read,
}

public RegisterBenchmark(String group, String peers, int clientNum, long operationNumPerClient,
BenchmarkType benchmarkType) {
this.group = group;
this.peers = peers;
this.clientNum = clientNum;
this.operationNumPerClient = operationNumPerClient;
this.benchmarkType = benchmarkType;
}

public void start() throws Exception {

final long totalOperation = clientNum * operationNumPerClient;

final CyclicBarrier barrier = new CyclicBarrier(clientNum + 1);

final String operationType = benchmarkType.name();

for (int i = 0; i < clientNum; i++) {
new Thread() {
@Override
public void run() {
long success = 0;
long failNum = 0;
RegisterDLedgerClient client = new RegisterDLedgerClient(group, peers);
client.startup();
try {
barrier.await();
while (success < operationNumPerClient) {
int code = 200;
if (benchmarkType == BenchmarkType.Read) {
// Read
RegisterReadResponse resp = client.read(13);
code = resp.getCode();
} else {
// Write
RegisterWriteResponse resp = client.write(13, 13);
code = resp.getCode();
}
if (code == 200) {
success++;
} else {
failNum++;
}
}
barrier.await();
client.shutdown();
} catch (Exception e) {
logger.error("client {} error", operationType, e);
} finally {
logger.info("client {} finished, need {} total: {}, success: {}, fail: {}",
operationType, operationType, operationNumPerClient, success, failNum);
client.shutdown();
}
}
}.start();
}
barrier.await();
StopWatch stopWatch = StopWatch.createStarted();
barrier.await();
final long cost = stopWatch.getTime(TimeUnit.MILLISECONDS);
final long tps = Math.round(totalOperation * 1000 / cost);
logger.info("Test type: {}, client num : {}, operation num per client: {}, total operation num: {}, cost: {}, tps: {}",
operationType, clientNum, operationNumPerClient, totalOperation, cost, tps);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void doCommand() {
}
}

private DLedgerConfig buildDLedgerConfig() throws Exception {
private DLedgerConfig buildDLedgerConfig() {
DLedgerConfig dLedgerConfig = new DLedgerConfig();
dLedgerConfig.setGroup(this.group);
dLedgerConfig.setSelfId(this.selfId);
Expand Down

0 comments on commit d781d09

Please sign in to comment.