Skip to content

Commit 672030e

Browse files
committed
Polish "--use-millis" option
Some renaming, use lambdas where possible, use JDK-provided functional interfaces where possible.
1 parent f67c41a commit 672030e

File tree

4 files changed

+24
-49
lines changed

4 files changed

+24
-49
lines changed

src/main/java/com/rabbitmq/perf/Consumer.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package com.rabbitmq.perf;
1717

18-
import com.rabbitmq.client.AMQP;
1918
import com.rabbitmq.client.AMQP.BasicProperties;
2019
import com.rabbitmq.client.Channel;
2120
import com.rabbitmq.client.DefaultConsumer;
@@ -31,6 +30,7 @@
3130
import java.util.Map;
3231
import java.util.concurrent.CountDownLatch;
3332
import java.util.concurrent.TimeUnit;
33+
import java.util.function.BiFunction;
3434

3535
public class Consumer extends ProducerConsumerBase implements Runnable {
3636

@@ -47,7 +47,7 @@ public class Consumer extends ProducerConsumerBase implements Runnable {
4747
private final CountDownLatch latch = new CountDownLatch(1);
4848
private final Map<String, String> consumerTagBranchMap = Collections.synchronizedMap(new HashMap<String, String>());
4949
private final ConsumerLatency consumerLatency;
50-
private final TimestampExtractor timestampExtractor;
50+
private final BiFunction<BasicProperties, byte[], Long> timestampExtractor;
5151
private final TimestampProvider timestampProvider;
5252

5353
public Consumer(Channel channel, String id,
@@ -77,21 +77,20 @@ public Consumer(Channel channel, String id,
7777
}
7878

7979
if (timestampProvider.isTimestampInHeader()) {
80-
this.timestampExtractor = new TimestampExtractor() {
81-
@Override
82-
public long extract(BasicProperties properties, byte[] body) throws IOException {
80+
this.timestampExtractor = (properties, body) -> {
8381
Object timestamp = properties.getHeaders().get(Producer.TIMESTAMP_HEADER);
8482
return timestamp == null ? Long.MAX_VALUE : (Long) timestamp;
85-
}
8683
};
8784
} else {
88-
this.timestampExtractor = new TimestampExtractor() {
89-
@Override
90-
public long extract(BasicProperties properties, byte[] body) throws IOException {
91-
DataInputStream d = new DataInputStream(new ByteArrayInputStream(body));
85+
this.timestampExtractor = (properties, body) -> {
86+
DataInputStream d = new DataInputStream(new ByteArrayInputStream(body));
87+
try {
9288
d.readInt();
9389
return d.readLong();
90+
} catch (IOException e) {
91+
throw new RuntimeException("Error while extracting timestamp from body");
9492
}
93+
9594
};
9695
}
9796
}
@@ -135,8 +134,8 @@ public void handleDelivery(String consumerTag, Envelope envelope, BasicPropertie
135134
msgCount++;
136135

137136
if (msgLimit == 0 || msgCount <= msgLimit) {
138-
long msg_ts = timestampExtractor.extract(properties, body);
139-
long now_ts = timestampProvider.getCurrentTime();
137+
long messageTimestamp = timestampExtractor.apply(properties, body);
138+
long nowTimestamp = timestampProvider.getCurrentTime();
140139

141140
if (!autoAck) {
142141
if (multiAckEvery == 0) {
@@ -150,10 +149,10 @@ public void handleDelivery(String consumerTag, Envelope envelope, BasicPropertie
150149
channel.txCommit();
151150
}
152151

153-
now = System.currentTimeMillis();
154-
155-
long diff_time = timestampProvider.getDifference(now_ts, msg_ts);
152+
long diff_time = timestampProvider.getDifference(nowTimestamp, messageTimestamp);
156153
stats.handleRecv(id.equals(envelope.getRoutingKey()) ? diff_time : 0L);
154+
155+
now = System.currentTimeMillis();
157156
if (rateLimit > 0.0f) {
158157
delay(now);
159158
}
@@ -231,9 +230,4 @@ public void simulateLatency() {
231230
}
232231
}
233232

234-
private interface TimestampExtractor {
235-
236-
long extract(BasicProperties properties, byte[] body) throws IOException;
237-
238-
}
239233
}

src/main/java/com/rabbitmq/perf/MulticastParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public Producer createProducer(Connection connection, Stats stats, String id) th
264264
if (!predeclared || !exchangeExists(connection, exchangeName)) {
265265
channel.exchangeDeclare(exchangeName, exchangeType);
266266
}
267-
MessageBodySource messageBodySource = null;
267+
MessageBodySource messageBodySource;
268268
TimestampProvider tsp;
269269
if (bodyFiles.size() > 0) {
270270
tsp = new TimestampProvider(useMillis, true);

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,7 @@ public static void main(String[] args) {
107107
file.delete();
108108
}
109109
output = new PrintWriter(new BufferedWriter(new FileWriter(file)), true);
110-
Runtime.getRuntime().addShutdownHook(new Thread() {
111-
112-
@Override
113-
public void run() {
114-
output.close();
115-
}
116-
});
110+
Runtime.getRuntime().addShutdownHook(new Thread(() -> output.close()));
117111
} else {
118112
output = null;
119113
}

src/main/java/com/rabbitmq/perf/Producer.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.UUID;
2929
import java.util.concurrent.Semaphore;
3030
import java.util.concurrent.TimeUnit;
31+
import java.util.function.UnaryOperator;
3132

3233
public class Producer extends ProducerConsumerBase implements Runnable, ReturnListener,
3334
ConfirmListener
@@ -48,7 +49,7 @@ public class Producer extends ProducerConsumerBase implements Runnable, ReturnLi
4849

4950
private final MessageBodySource messageBodySource;
5051

51-
private final PropertiesBuilderProcessor propertiesBuilderProcessor;
52+
private final UnaryOperator<AMQP.BasicProperties.Builder> propertiesBuilderProcessor;
5253
private Semaphore confirmPool;
5354
private int confirmTimeout;
5455
private final SortedSet<Long> unconfirmedSet =
@@ -74,20 +75,12 @@ public Producer(Channel channel, String exchangeName, String id, boolean randomR
7475
this.timeLimitMillis = 1000L * timeLimitSecs;
7576
this.messageBodySource = messageBodySource;
7677
if (tsp.isTimestampInHeader()) {
77-
this.propertiesBuilderProcessor = new PropertiesBuilderProcessor() {
78-
@Override
79-
public AMQP.BasicProperties.Builder process(AMQP.BasicProperties.Builder builder) {
80-
builder.headers(Collections.<String, Object>singletonMap(TIMESTAMP_HEADER, tsp.getCurrentTime()));
81-
return builder;
82-
}
78+
this.propertiesBuilderProcessor = builder -> {
79+
builder.headers(Collections.<String, Object>singletonMap(TIMESTAMP_HEADER, tsp.getCurrentTime()));
80+
return builder;
8381
};
8482
} else {
85-
this.propertiesBuilderProcessor = new PropertiesBuilderProcessor() {
86-
@Override
87-
public AMQP.BasicProperties.Builder process(AMQP.BasicProperties.Builder builder) {
88-
return builder;
89-
}
90-
};
83+
this.propertiesBuilderProcessor = UnaryOperator.identity();
9184
}
9285
if (confirm > 0) {
9386
this.confirmPool = new Semaphore((int)confirm);
@@ -141,7 +134,7 @@ private void handleAckNack(long seqNo, boolean multiple,
141134

142135
public void run() {
143136
long now;
144-
long startTime;
137+
final long startTime;
145138
startTime = now = System.currentTimeMillis();
146139
lastStatsTime = startTime;
147140
msgCount = 0;
@@ -194,7 +187,7 @@ private void publish(MessageBodySource.MessageBodyAndContentType messageBodyAndC
194187
propertiesBuilder.contentType(messageBodyAndContentType.getContentType());
195188
}
196189

197-
propertiesBuilder = this.propertiesBuilderProcessor.process(propertiesBuilder);
190+
propertiesBuilder = this.propertiesBuilderProcessor.apply(propertiesBuilder);
198191

199192
unconfirmedSet.add(channel.getNextPublishSeqNo());
200193
channel.basicPublish(exchangeName, randomRoutingKey ? UUID.randomUUID().toString() : id,
@@ -203,10 +196,4 @@ private void publish(MessageBodySource.MessageBodyAndContentType messageBodyAndC
203196
messageBodyAndContentType.getBody());
204197
}
205198

206-
private interface PropertiesBuilderProcessor {
207-
208-
AMQP.BasicProperties.Builder process(AMQP.BasicProperties.Builder builder);
209-
210-
}
211-
212199
}

0 commit comments

Comments
 (0)