Skip to content

Commit 8784c74

Browse files
committed
Polish consumer variable latency support
1 parent 0d0b444 commit 8784c74

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

src/docs/asciidoc/usage-advanced.adoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ then back to 1 kB for 30 seconds, and so on.
124124

125125
You can simulate processing time per message with either a fixed or a variable latency value in microseconds.
126126

127-
The `--consumer-latency' ('-L') option sets a fixed consumer latency in microseconds. In the example below a 1ms latency is set.
127+
The `--consumer-latency` (`-L`) option sets a fixed consumer latency in microseconds. In the example
128+
below a 1 ms latency is set:
128129

129130
bin/runjava com.rabbitmq.perf.PerfTest --consumer-latency 1000
130131

131-
The `--variable-latency' ('-vl') option sets a variable consumer latency. In the example below it is set to 60 seconds of 1ms followed by 60 seconds of 1 second).
132+
The `--variable-latency` (`-vl`) option sets a variable consumer latency. In the example below it is
133+
set to 1 ms for 60 seconds then 1 second for 30 seconds:
132134

133-
bin/runjava com.rabbitmq.perf.PerfTest --variable-latency 1000:60 --variable-latency 1000000:60
135+
bin/runjava com.rabbitmq.perf.PerfTest --variable-latency 1000:60 --variable-latency 1000000:30
134136

135137
== Working With Many Queues
136138

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

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,25 @@ private interface ConsumerLatency {
391391

392392
}
393393

394-
private static class VariableConsumerLatency implements ConsumerLatency {
394+
private static boolean latencySleep(long delay) {
395+
try {
396+
long ms = delay / 1000;
397+
Thread.sleep(ms);
398+
return true;
399+
} catch (InterruptedException e) {
400+
Thread.currentThread().interrupt();
401+
return false;
402+
}
403+
}
404+
405+
private static boolean latencyBusyWait(long delay) {
406+
delay = delay * 1000;
407+
long start = System.nanoTime();
408+
while (System.nanoTime() - start < delay) ;
409+
return true;
410+
}
411+
412+
private static class VariableConsumerLatency implements ConsumerLatency {
395413

396414
private final ValueIndicator<Long> consumerLatenciesIndicator;
397415

@@ -400,35 +418,17 @@ private VariableConsumerLatency(ValueIndicator<Long> consumerLatenciesIndicator)
400418
}
401419

402420
@Override
403-
public boolean simulateLatency()
404-
{
421+
public boolean simulateLatency() {
405422
long consumerLatencyInMicroSeconds = consumerLatenciesIndicator.getValue();
406423
if (consumerLatencyInMicroSeconds <= 0) {
407424
return true;
408425
} else if (consumerLatencyInMicroSeconds >= 1000) {
409-
return sleep();
426+
return latencySleep(consumerLatencyInMicroSeconds);
410427
} else {
411-
return busyWait();
428+
return latencyBusyWait(consumerLatencyInMicroSeconds);
412429
}
413430
}
414431

415-
private boolean sleep() {
416-
try {
417-
long ms = consumerLatenciesIndicator.getValue() / 1000;
418-
Thread.sleep(ms);
419-
return true;
420-
} catch (InterruptedException e) {
421-
Thread.currentThread().interrupt();
422-
return false;
423-
}
424-
}
425-
426-
private boolean busyWait() {
427-
long delay = consumerLatenciesIndicator.getValue() * 1000;
428-
long start = System.nanoTime();
429-
while(System.nanoTime() - start < delay);
430-
return true;
431-
}
432432
}
433433

434434
private static class NoWaitConsumerLatency implements ConsumerLatency {
@@ -450,14 +450,7 @@ private ThreadSleepConsumerLatency(ValueIndicator<Long> consumerLatenciesIndicat
450450

451451
@Override
452452
public boolean simulateLatency() {
453-
try {
454-
long ms = consumerLatenciesIndicator.getValue() / 1000;
455-
Thread.sleep(ms);
456-
return true;
457-
} catch (InterruptedException e) {
458-
Thread.currentThread().interrupt();
459-
return false;
460-
}
453+
return latencySleep(consumerLatenciesIndicator.getValue());
461454
}
462455
}
463456

@@ -472,10 +465,7 @@ private BusyWaitConsumerLatency(ValueIndicator<Long> consumerLatenciesIndicator)
472465

473466
@Override
474467
public boolean simulateLatency() {
475-
long delay = consumerLatenciesIndicator.getValue() * 1000;
476-
long start = System.nanoTime();
477-
while(System.nanoTime() - start < delay);
478-
return true;
468+
return latencyBusyWait(consumerLatenciesIndicator.getValue());
479469
}
480470
}
481471

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private void createConsumers(boolean announceStartup,
332332
}
333333
}
334334

335-
private void createProducers(boolean announceStartup, AgentState[] producerStates, Connection[] producerConnections) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException, IOException, TimeoutException {
335+
private void createProducers(boolean announceStartup, AgentState[] producerStates, Connection[] producerConnections) throws IOException, TimeoutException {
336336
for (int i = 0; i < producerConnections.length; i++) {
337337
if (announceStartup) {
338338
System.out.println("id: " + testID + ", starting producer #" + i);

0 commit comments

Comments
 (0)