未验证 提交 857d28df 编写于 作者: 【keepal】 提交者: GitHub

[ISSUE #3284]Optimizing benchmark code (#3317)

上级 2cac8662
...@@ -26,6 +26,8 @@ import java.util.TimerTask; ...@@ -26,6 +26,8 @@ import java.util.TimerTask;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option; import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
...@@ -100,22 +102,22 @@ public class BatchProducer { ...@@ -100,22 +102,22 @@ public class BatchProducer {
try { try {
long beginTimestamp = System.currentTimeMillis(); long beginTimestamp = System.currentTimeMillis();
long sendSucCount = statsBenchmark.getSendMessageSuccessCount().get(); long sendSucCount = statsBenchmark.getSendMessageSuccessCount().longValue();
setKeys(keyEnable, msgs, String.valueOf(beginTimestamp / 1000)); setKeys(keyEnable, msgs, String.valueOf(beginTimestamp / 1000));
setTags(tagCount, msgs, sendSucCount); setTags(tagCount, msgs, sendSucCount);
setProperties(propertySize, msgs); setProperties(propertySize, msgs);
SendResult sendResult = producer.send(msgs); SendResult sendResult = producer.send(msgs);
if (sendResult.getSendStatus() == SendStatus.SEND_OK) { if (sendResult.getSendStatus() == SendStatus.SEND_OK) {
statsBenchmark.getSendRequestSuccessCount().incrementAndGet(); statsBenchmark.getSendRequestSuccessCount().increment();
statsBenchmark.getSendMessageSuccessCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageSuccessCount().add(msgs.size());
} else { } else {
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
statsBenchmark.getSendMessageFailedCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageFailedCount().add(msgs.size());
} }
long currentRT = System.currentTimeMillis() - beginTimestamp; long currentRT = System.currentTimeMillis() - beginTimestamp;
statsBenchmark.getSendMessageSuccessTimeTotal().addAndGet(currentRT); statsBenchmark.getSendMessageSuccessTimeTotal().add(currentRT);
long prevMaxRT = statsBenchmark.getSendMessageMaxRT().get(); long prevMaxRT = statsBenchmark.getSendMessageMaxRT().longValue();
while (currentRT > prevMaxRT) { while (currentRT > prevMaxRT) {
boolean updated = statsBenchmark.getSendMessageMaxRT().compareAndSet(prevMaxRT, currentRT); boolean updated = statsBenchmark.getSendMessageMaxRT().compareAndSet(prevMaxRT, currentRT);
if (updated) { if (updated) {
...@@ -125,8 +127,8 @@ public class BatchProducer { ...@@ -125,8 +127,8 @@ public class BatchProducer {
prevMaxRT = statsBenchmark.getSendMessageMaxRT().get(); prevMaxRT = statsBenchmark.getSendMessageMaxRT().get();
} }
} catch (RemotingException e) { } catch (RemotingException e) {
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
statsBenchmark.getSendMessageFailedCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageFailedCount().add(msgs.size());
log.error("[BENCHMARK_PRODUCER] Send Exception", e); log.error("[BENCHMARK_PRODUCER] Send Exception", e);
try { try {
...@@ -134,22 +136,22 @@ public class BatchProducer { ...@@ -134,22 +136,22 @@ public class BatchProducer {
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
statsBenchmark.getSendMessageFailedCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageFailedCount().add(msgs.size());
try { try {
Thread.sleep(3000); Thread.sleep(3000);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
} }
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
statsBenchmark.getSendMessageFailedCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageFailedCount().add(msgs.size());
log.error("[BENCHMARK_PRODUCER] Send Exception", e); log.error("[BENCHMARK_PRODUCER] Send Exception", e);
} catch (MQClientException e) { } catch (MQClientException e) {
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
statsBenchmark.getSendMessageFailedCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageFailedCount().add(msgs.size());
log.error("[BENCHMARK_PRODUCER] Send Exception", e); log.error("[BENCHMARK_PRODUCER] Send Exception", e);
} catch (MQBrokerException e) { } catch (MQBrokerException e) {
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
statsBenchmark.getSendMessageFailedCount().addAndGet(msgs.size()); statsBenchmark.getSendMessageFailedCount().add(msgs.size());
log.error("[BENCHMARK_PRODUCER] Send Exception", e); log.error("[BENCHMARK_PRODUCER] Send Exception", e);
try { try {
Thread.sleep(3000); Thread.sleep(3000);
...@@ -313,17 +315,17 @@ public class BatchProducer { ...@@ -313,17 +315,17 @@ public class BatchProducer {
class StatsBenchmarkBatchProducer { class StatsBenchmarkBatchProducer {
private final AtomicLong sendRequestSuccessCount = new AtomicLong(0L); private final LongAdder sendRequestSuccessCount = new LongAdder();
private final AtomicLong sendRequestFailedCount = new AtomicLong(0L); private final LongAdder sendRequestFailedCount = new LongAdder();
private final AtomicLong sendMessageSuccessTimeTotal = new AtomicLong(0L); private final LongAdder sendMessageSuccessTimeTotal = new LongAdder();
private final AtomicLong sendMessageMaxRT = new AtomicLong(0L); private final AtomicLong sendMessageMaxRT = new AtomicLong(0L);
private final AtomicLong sendMessageSuccessCount = new AtomicLong(0L); private final LongAdder sendMessageSuccessCount = new LongAdder();
private final AtomicLong sendMessageFailedCount = new AtomicLong(0L); private final LongAdder sendMessageFailedCount = new LongAdder();
private final Timer timer = new Timer("BenchmarkTimerThread", true); private final Timer timer = new Timer("BenchmarkTimerThread", true);
...@@ -332,25 +334,25 @@ class StatsBenchmarkBatchProducer { ...@@ -332,25 +334,25 @@ class StatsBenchmarkBatchProducer {
public Long[] createSnapshot() { public Long[] createSnapshot() {
Long[] snap = new Long[] { Long[] snap = new Long[] {
System.currentTimeMillis(), System.currentTimeMillis(),
this.sendRequestSuccessCount.get(), this.sendRequestSuccessCount.longValue(),
this.sendRequestFailedCount.get(), this.sendRequestFailedCount.longValue(),
this.sendMessageSuccessCount.get(), this.sendMessageSuccessCount.longValue(),
this.sendMessageFailedCount.get(), this.sendMessageFailedCount.longValue(),
this.sendMessageSuccessTimeTotal.get(), this.sendMessageSuccessTimeTotal.longValue(),
}; };
return snap; return snap;
} }
public AtomicLong getSendRequestSuccessCount() { public LongAdder getSendRequestSuccessCount() {
return sendRequestSuccessCount; return sendRequestSuccessCount;
} }
public AtomicLong getSendRequestFailedCount() { public LongAdder getSendRequestFailedCount() {
return sendRequestFailedCount; return sendRequestFailedCount;
} }
public AtomicLong getSendMessageSuccessTimeTotal() { public LongAdder getSendMessageSuccessTimeTotal() {
return sendMessageSuccessTimeTotal; return sendMessageSuccessTimeTotal;
} }
...@@ -358,11 +360,11 @@ class StatsBenchmarkBatchProducer { ...@@ -358,11 +360,11 @@ class StatsBenchmarkBatchProducer {
return sendMessageMaxRT; return sendMessageMaxRT;
} }
public AtomicLong getSendMessageSuccessCount() { public LongAdder getSendMessageSuccessCount() {
return sendMessageSuccessCount; return sendMessageSuccessCount;
} }
public AtomicLong getSendMessageFailedCount() { public LongAdder getSendMessageFailedCount() {
return sendMessageFailedCount; return sendMessageFailedCount;
} }
...@@ -390,7 +392,7 @@ class StatsBenchmarkBatchProducer { ...@@ -390,7 +392,7 @@ class StatsBenchmarkBatchProducer {
final double averageMsgRT = (end[5] - begin[5]) / (double) (end[3] - begin[3]); final double averageMsgRT = (end[5] - begin[5]) / (double) (end[3] - begin[3]);
System.out.printf("Current Time: %s Send TPS: %d Send MPS: %d Max RT(ms): %d Average RT(ms): %7.3f Average Message RT(ms): %7.3f Send Failed: %d Send Message Failed: %d%n", System.out.printf("Current Time: %s Send TPS: %d Send MPS: %d Max RT(ms): %d Average RT(ms): %7.3f Average Message RT(ms): %7.3f Send Failed: %d Send Message Failed: %d%n",
System.currentTimeMillis(), sendTps, sendMps, getSendMessageMaxRT().get(), averageRT, averageMsgRT, end[2], end[4]); System.currentTimeMillis(), sendTps, sendMps, getSendMessageMaxRT().longValue(), averageRT, averageMsgRT, end[2], end[4]);
} }
} }
......
...@@ -50,10 +50,11 @@ import java.util.concurrent.atomic.AtomicLong; ...@@ -50,10 +50,11 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
public class TransactionProducer { public class TransactionProducer {
private static final long START_TIME = System.currentTimeMillis(); private static final long START_TIME = System.currentTimeMillis();
private static final AtomicLong MSG_COUNT = new AtomicLong(0); private static final LongAdder MSG_COUNT = new LongAdder();
//broker max check times should less than this value //broker max check times should less than this value
static final int MAX_CHECK_RESULT_IN_MSG = 20; static final int MAX_CHECK_RESULT_IN_MSG = 20;
...@@ -158,7 +159,7 @@ public class TransactionProducer { ...@@ -158,7 +159,7 @@ public class TransactionProducer {
success = false; success = false;
} finally { } finally {
final long currentRT = System.currentTimeMillis() - beginTimestamp; final long currentRT = System.currentTimeMillis() - beginTimestamp;
statsBenchmark.getSendMessageTimeTotal().addAndGet(currentRT); statsBenchmark.getSendMessageTimeTotal().add(currentRT);
long prevMaxRT = statsBenchmark.getSendMessageMaxRT().get(); long prevMaxRT = statsBenchmark.getSendMessageMaxRT().get();
while (currentRT > prevMaxRT) { while (currentRT > prevMaxRT) {
boolean updated = statsBenchmark.getSendMessageMaxRT() boolean updated = statsBenchmark.getSendMessageMaxRT()
...@@ -169,9 +170,9 @@ public class TransactionProducer { ...@@ -169,9 +170,9 @@ public class TransactionProducer {
prevMaxRT = statsBenchmark.getSendMessageMaxRT().get(); prevMaxRT = statsBenchmark.getSendMessageMaxRT().get();
} }
if (success) { if (success) {
statsBenchmark.getSendRequestSuccessCount().incrementAndGet(); statsBenchmark.getSendRequestSuccessCount().increment();
} else { } else {
statsBenchmark.getSendRequestFailedCount().incrementAndGet(); statsBenchmark.getSendRequestFailedCount().increment();
} }
if (config.sendInterval > 0) { if (config.sendInterval > 0) {
try { try {
...@@ -194,7 +195,9 @@ public class TransactionProducer { ...@@ -194,7 +195,9 @@ public class TransactionProducer {
ByteBuffer buf = ByteBuffer.wrap(bs); ByteBuffer buf = ByteBuffer.wrap(bs);
buf.putLong(config.batchId); buf.putLong(config.batchId);
long sendMachineId = START_TIME << 32; long sendMachineId = START_TIME << 32;
long msgId = sendMachineId | MSG_COUNT.getAndIncrement(); long count = MSG_COUNT.longValue();
long msgId = sendMachineId | count;
MSG_COUNT.increment();
buf.putLong(msgId); buf.putLong(msgId);
// save send tx result in message // save send tx result in message
...@@ -316,7 +319,7 @@ class TransactionListenerImpl implements TransactionListener { ...@@ -316,7 +319,7 @@ class TransactionListenerImpl implements TransactionListener {
// message not generated in this test // message not generated in this test
return LocalTransactionState.ROLLBACK_MESSAGE; return LocalTransactionState.ROLLBACK_MESSAGE;
} }
statBenchmark.getCheckCount().incrementAndGet(); statBenchmark.getCheckCount().increment();
int times = 0; int times = 0;
try { try {
...@@ -339,7 +342,7 @@ class TransactionListenerImpl implements TransactionListener { ...@@ -339,7 +342,7 @@ class TransactionListenerImpl implements TransactionListener {
dup = newCheckLog.equals(oldCheckLog); dup = newCheckLog.equals(oldCheckLog);
} }
if (dup) { if (dup) {
statBenchmark.getDuplicatedCheckCount().incrementAndGet(); statBenchmark.getDuplicatedCheckCount().increment();
} }
if (msgMeta.sendResult != LocalTransactionState.UNKNOW) { if (msgMeta.sendResult != LocalTransactionState.UNKNOW) {
System.out.printf("%s unexpected check: msgId=%s,txId=%s,checkTimes=%s,sendResult=%s\n", System.out.printf("%s unexpected check: msgId=%s,txId=%s,checkTimes=%s,sendResult=%s\n",
...@@ -347,7 +350,7 @@ class TransactionListenerImpl implements TransactionListener { ...@@ -347,7 +350,7 @@ class TransactionListenerImpl implements TransactionListener {
msg.getMsgId(), msg.getTransactionId(), msg.getMsgId(), msg.getTransactionId(),
msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES), msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES),
msgMeta.sendResult.toString()); msgMeta.sendResult.toString());
statBenchmark.getUnexpectedCheckCount().incrementAndGet(); statBenchmark.getUnexpectedCheckCount().increment();
return msgMeta.sendResult; return msgMeta.sendResult;
} }
...@@ -358,7 +361,7 @@ class TransactionListenerImpl implements TransactionListener { ...@@ -358,7 +361,7 @@ class TransactionListenerImpl implements TransactionListener {
new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()), new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()),
msg.getMsgId(), msg.getTransactionId(), msg.getMsgId(), msg.getTransactionId(),
msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES), s); msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES), s);
statBenchmark.getUnexpectedCheckCount().incrementAndGet(); statBenchmark.getUnexpectedCheckCount().increment();
return s; return s;
} }
} }
...@@ -385,42 +388,42 @@ class Snapshot { ...@@ -385,42 +388,42 @@ class Snapshot {
} }
class StatsBenchmarkTProducer { class StatsBenchmarkTProducer {
private final AtomicLong sendRequestSuccessCount = new AtomicLong(0L); private final LongAdder sendRequestSuccessCount = new LongAdder();
private final AtomicLong sendRequestFailedCount = new AtomicLong(0L); private final LongAdder sendRequestFailedCount = new LongAdder();
private final AtomicLong sendMessageTimeTotal = new AtomicLong(0L); private final LongAdder sendMessageTimeTotal = new LongAdder();
private final AtomicLong sendMessageMaxRT = new AtomicLong(0L); private final AtomicLong sendMessageMaxRT = new AtomicLong(0L);
private final AtomicLong checkCount = new AtomicLong(0L); private final LongAdder checkCount = new LongAdder();
private final AtomicLong unexpectedCheckCount = new AtomicLong(0L); private final LongAdder unexpectedCheckCount = new LongAdder();
private final AtomicLong duplicatedCheckCount = new AtomicLong(0); private final LongAdder duplicatedCheckCount = new LongAdder();
public Snapshot createSnapshot() { public Snapshot createSnapshot() {
Snapshot s = new Snapshot(); Snapshot s = new Snapshot();
s.endTime = System.currentTimeMillis(); s.endTime = System.currentTimeMillis();
s.sendRequestSuccessCount = sendRequestSuccessCount.get(); s.sendRequestSuccessCount = sendRequestSuccessCount.longValue();
s.sendRequestFailedCount = sendRequestFailedCount.get(); s.sendRequestFailedCount = sendRequestFailedCount.longValue();
s.sendMessageTimeTotal = sendMessageTimeTotal.get(); s.sendMessageTimeTotal = sendMessageTimeTotal.longValue();
s.sendMessageMaxRT = sendMessageMaxRT.get(); s.sendMessageMaxRT = sendMessageMaxRT.get();
s.checkCount = checkCount.get(); s.checkCount = checkCount.longValue();
s.unexpectedCheckCount = unexpectedCheckCount.get(); s.unexpectedCheckCount = unexpectedCheckCount.longValue();
s.duplicatedCheck = duplicatedCheckCount.get(); s.duplicatedCheck = duplicatedCheckCount.longValue();
return s; return s;
} }
public AtomicLong getSendRequestSuccessCount() { public LongAdder getSendRequestSuccessCount() {
return sendRequestSuccessCount; return sendRequestSuccessCount;
} }
public AtomicLong getSendRequestFailedCount() { public LongAdder getSendRequestFailedCount() {
return sendRequestFailedCount; return sendRequestFailedCount;
} }
public AtomicLong getSendMessageTimeTotal() { public LongAdder getSendMessageTimeTotal() {
return sendMessageTimeTotal; return sendMessageTimeTotal;
} }
...@@ -428,15 +431,15 @@ class StatsBenchmarkTProducer { ...@@ -428,15 +431,15 @@ class StatsBenchmarkTProducer {
return sendMessageMaxRT; return sendMessageMaxRT;
} }
public AtomicLong getCheckCount() { public LongAdder getCheckCount() {
return checkCount; return checkCount;
} }
public AtomicLong getUnexpectedCheckCount() { public LongAdder getUnexpectedCheckCount() {
return unexpectedCheckCount; return unexpectedCheckCount;
} }
public AtomicLong getDuplicatedCheckCount() { public LongAdder getDuplicatedCheckCount() {
return duplicatedCheckCount; return duplicatedCheckCount;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册