|
| 1 | +// Copyright (c) 2025 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// |
| 4 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 5 | +// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2 |
| 6 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 7 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 8 | +// please see LICENSE-APACHE2. |
| 9 | +// |
| 10 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 11 | +// either express or implied. See the LICENSE file for specific language governing |
| 12 | +// rights and limitations of this software. |
| 13 | +// |
| 14 | +// If you have any questions regarding licensing, please contact us at |
| 15 | + |
| 16 | +package com.rabbitmq.perf.it; |
| 17 | + |
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.fail; |
| 21 | + |
| 22 | +import com.google.gson.Gson; |
| 23 | +import com.google.gson.reflect.TypeToken; |
| 24 | +import com.rabbitmq.client.Channel; |
| 25 | +import com.rabbitmq.client.Connection; |
| 26 | +import com.rabbitmq.perf.PerfTest; |
| 27 | +import com.rabbitmq.perf.PerfTestMulti; |
| 28 | +import com.rabbitmq.perf.TestUtils; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.PrintStream; |
| 32 | +import java.lang.reflect.Type; |
| 33 | +import java.nio.charset.StandardCharsets; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.nio.file.StandardOpenOption; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.junit.jupiter.api.TestInfo; |
| 42 | +import org.junit.jupiter.api.io.TempDir; |
| 43 | + |
| 44 | +public class PerfTestMultiIT { |
| 45 | + |
| 46 | + private static final Gson GSON = new Gson(); |
| 47 | + |
| 48 | + Path specFile, resultFile; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp(@TempDir Path directory) throws Exception { |
| 52 | + specFile = directory.resolve("spec.json"); |
| 53 | + resultFile = directory.resolve("result.json"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void simpleScenario() throws Exception { |
| 58 | + String spec = |
| 59 | + "[{'name': 'simple', 'type': 'simple', 'params':\n" |
| 60 | + + "[{'time-limit': 2, 'producer-count': 1, 'consumer-count': 1}]}]"; |
| 61 | + writeSpec(spec); |
| 62 | + run(); |
| 63 | + assertReceiveRatePositive(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void queuePattern(TestInfo info) throws Exception { |
| 68 | + String prefix = TestUtils.randomName(info); |
| 69 | + int queueCount = 3; |
| 70 | + String spec = |
| 71 | + String.format( |
| 72 | + "[{'name': 'simple', 'type': 'simple', 'params':\n" |
| 73 | + + "[{" |
| 74 | + + "'time-limit': 2, 'producer-count': 6, 'consumer-count': 3," |
| 75 | + + "'flags': 'persistent', 'auto-delete': false," |
| 76 | + + "'queue-pattern': '%s', 'queue-sequence-from': 1, 'queue-sequence-to': %d" |
| 77 | + + "}]}]", |
| 78 | + prefix + "-%d", queueCount); |
| 79 | + writeSpec(spec); |
| 80 | + run(); |
| 81 | + assertReceiveRatePositive(); |
| 82 | + try (Connection c = TestUtils.connectionFactory().newConnection()) { |
| 83 | + Channel ch = c.createChannel(); |
| 84 | + for (int i = 1; i <= queueCount; i++) { |
| 85 | + String queueName = String.format("%s-%d", prefix, i); |
| 86 | + ch.queueDeclarePassive(queueName); |
| 87 | + ch.queueDelete(queueName); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + private void assertReceiveRatePositive() throws IOException { |
| 94 | + String result = new String(Files.readAllBytes(resultFile), StandardCharsets.UTF_8); |
| 95 | + Type type = new TypeToken<Map<String, Object>>() {}.getType(); |
| 96 | + Map<String, Object> results = GSON.fromJson(result, type); |
| 97 | + Map<String, Object> scenario = (Map<String, Object>) results.values().iterator().next(); |
| 98 | + List<Map<String, Object>> samples = (List<Map<String, Object>>) scenario.get("samples"); |
| 99 | + assertThat(samples).isNotEmpty(); |
| 100 | + for (Map<String, Object> sample : samples) { |
| 101 | + if (((Number) sample.get("recv-msg-rate")).intValue() > 0) { |
| 102 | + return; |
| 103 | + } |
| 104 | + } |
| 105 | + fail("The receive rate is not positive"); |
| 106 | + } |
| 107 | + |
| 108 | + private void run() throws Exception { |
| 109 | + String[] args = {specFile.toAbsolutePath().toString(), resultFile.toAbsolutePath().toString()}; |
| 110 | + PerfTest.PerfTestOptions options = new PerfTest.PerfTestOptions(); |
| 111 | + options |
| 112 | + .setSystemExiter(status -> {}) |
| 113 | + .setConsoleOut(new PrintStream(new ByteArrayOutputStream())); |
| 114 | + PerfTestMulti.main(args, options); |
| 115 | + } |
| 116 | + |
| 117 | + private void writeSpec(String spec) throws IOException { |
| 118 | + Files.write(specFile, spec.getBytes(UTF_8), StandardOpenOption.CREATE_NEW); |
| 119 | + } |
| 120 | +} |
0 commit comments