From 8e24de7d2c28fb1ded14ad3260d32ca77c7723ef Mon Sep 17 00:00:00 2001 From: ChongYuan Date: Fri, 12 Apr 2024 16:42:08 +0800 Subject: [PATCH] refactor: refine random payload gen for large payload size --- .../src/main/java/net/xmeter/Util.java | 54 +++++++++++++++++-- .../java/net/xmeter/samplers/PubSampler.java | 14 +++-- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/mqtt_jmeter/src/main/java/net/xmeter/Util.java b/mqtt_jmeter/src/main/java/net/xmeter/Util.java index 8fca1aa..85d61b1 100644 --- a/mqtt_jmeter/src/main/java/net/xmeter/Util.java +++ b/mqtt_jmeter/src/main/java/net/xmeter/Util.java @@ -5,6 +5,7 @@ import java.io.InputStream; import java.security.KeyStore; import java.security.SecureRandom; +import java.util.Random; import java.util.UUID; import java.util.logging.Logger; @@ -18,9 +19,23 @@ public class Util implements Constants { - private static SecureRandom random = new SecureRandom(); + private static Random random = new Random(); private static char[] seeds = "abcdefghijklmnopqrstuvwxmy0123456789".toCharArray(); private static final Logger logger = Logger.getLogger(Util.class.getCanonicalName()); + + private static String[] longChunks = new String[16]; + private static final int PAYLOAD_CHUNK_SIZE = 4096; + + static { + for (int i = 0; i < longChunks.length; i++) { + StringBuffer res = new StringBuffer(); + for(int j = 0; j < 4096; j++) { + res.append(seeds[random.nextInt(seeds.length - 1)]); + } + longChunks[i] = res.toString(); + } + + } public static String generateClientId(String prefix) { int leng = prefix.length(); @@ -37,12 +52,12 @@ public static String generateClientId(String prefix) { public static SSLContext getContext(AbstractMQTTSampler sampler) throws Exception { if (!sampler.isDualSSLAuth()) { - logger.info("Configured with non-dual SSL."); + logger.fine("Configured with non-dual SSL."); SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(null, new TrustManager[] {new AcceptAllTrustManager()}, new SecureRandom()); return sslContext; } else { - logger.info("Configured with dual SSL, trying to load client certification."); + logger.fine("Configured with dual SSL, trying to load client certification."); // String KEYSTORE_PASS = sampler.getKeyStorePassword(); String CLIENTCERT_PASS = sampler.getClientCertPassword(); @@ -95,11 +110,40 @@ private static File getFilePath(String filePath) { public static String generatePayload(int size) { StringBuffer res = new StringBuffer(); - for(int i = 0; i < size; i++) { - res.append(seeds[random.nextInt(seeds.length - 1)]); + if (size <= PAYLOAD_CHUNK_SIZE) { + for(int i = 0; i < size; i++) { + res.append(seeds[random.nextInt(seeds.length - 1)]); + } + } else { + int block = size / PAYLOAD_CHUNK_SIZE; + int remainSize = size % PAYLOAD_CHUNK_SIZE; + int[] indices = randomIndices(longChunks.length); + for(int i=0; i