Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refine random payload gen for large payload size #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions mqtt_jmeter/src/main/java/net/xmeter/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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<block; i++) {
int ind = block % 10;
res.append(longChunks[indices[ind]]);
}
res.append(randomChunkPiece(remainSize));
}
return res.toString();
}

private static int[] randomIndices(int length) {
int[] indices = new int[10];
int val = random.nextInt(Integer.MAX_VALUE);
String str = String.format("%010d", val);
for (int i = 0; i < indices.length; i++) {
int intVal = Character.getNumericValue(str.charAt(i)) % length;
indices[i] = intVal;
}
return indices;
}

private static String randomChunkPiece(int remainSize) {
int start = random.nextInt(PAYLOAD_CHUNK_SIZE - remainSize + 1);
int end = start + remainSize;
int randomInd = random.nextInt(longChunks.length);
return longChunks[randomInd].substring(start, end);
}

public static boolean isSecureProtocol(String protocol) {
return SSL_PROTOCOL.equals(protocol) || WSS_PROTOCOL.equals(protocol);
Expand Down
14 changes: 9 additions & 5 deletions mqtt_jmeter/src/main/java/net/xmeter/samplers/PubSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ public SampleResult sample(Entry arg0) {
System.arraycopy(timePrefix, 0, toSend, 0, timePrefix.length);
System.arraycopy(tmp, 0, toSend, timePrefix.length , tmp.length);
} else {
toSend = new byte[tmp.length];
System.arraycopy(tmp, 0, toSend, 0 , tmp.length);
toSend = tmp;
}

result.sampleStart();
Expand All @@ -155,7 +154,12 @@ public SampleResult sample(Entry arg0) {
MQTTPubResult pubResult = connection.publish(topicName, toSend, qos_enum, retainedMsg);

result.sampleEnd();
result.setSamplerData(new String(toSend));
//only keep the first 1024 characters in sampler data
if (toSend.length <= 1024) {
result.setSamplerData(new String(toSend));
} else {
result.setSamplerData(new String(toSend).substring(0, 1024) + "...");
}
result.setSentBytes(toSend.length);
result.setLatency(result.getEndTime() - result.getStartTime());
result.setSuccessful(pubResult.isSuccessful());
Expand All @@ -169,8 +173,8 @@ public SampleResult sample(Entry arg0) {
result.setResponseMessage(MessageFormat.format("Publish failed for connection {0}.", connection));
result.setResponseData(MessageFormat.format("Client [{0}] publish failed: {1}", (clientId == null ? "null" : clientId), pubResult.getError().orElse("")).getBytes());
result.setResponseCode("501");
logger.info(MessageFormat.format("** [clientId: {0}, topic: {1}, payload: {2}] Publish failed for connection {3}.", (clientId == null ? "null" : clientId),
topicName, new String(toSend), connection));
logger.info(MessageFormat.format("** [clientId: {0}, topic: {1}] Publish failed for connection {2}.", (clientId == null ? "null" : clientId),
topicName, connection));
pubResult.getError().ifPresent(logger::info);
}
} catch (Exception ex) {
Expand Down