Skip to content

Commit

Permalink
#332 fix Random Object generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jan 26, 2024
1 parent 25b7a0d commit ba55fd6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 95 deletions.
130 changes: 43 additions & 87 deletions src/main/java/com/sngular/kloadgen/randomtool/random/RandomObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,111 +42,67 @@ public boolean isTypeValid(final String type) {
public Object generateRandom(
final String fieldType, final Integer valueLength, final List<String> fieldValueList,
final Map<ConstraintTypeEnum, String> constraints) {
Object value;
switch (fieldType.toLowerCase()) {
case ValidTypeConstants.STRING:
value = getStringValueOrRandom(valueLength, fieldValueList, constraints);
break;
case ValidTypeConstants.INT:
final String fixFieldType = StringUtils.defaultString(fieldType, "string");
return switch (fixFieldType.toLowerCase()) {
case ValidTypeConstants.STRING -> getStringValueOrRandom(valueLength, fieldValueList, constraints);
case ValidTypeConstants.INT -> {
try {
value = getIntegerValueOrRandom(valueLength, fieldValueList, constraints).intValueExact();
yield getIntegerValueOrRandom(valueLength, fieldValueList, constraints).intValueExact();
} catch (final ArithmeticException exception) {
value = Integer.MAX_VALUE;
yield Integer.MAX_VALUE;
}
break;
case ValidTypeConstants.LONG:
}
case ValidTypeConstants.LONG -> {
try {
value = getIntegerValueOrRandom(valueLength, fieldValueList, constraints).longValueExact();
yield getIntegerValueOrRandom(valueLength, fieldValueList, constraints).longValueExact();
} catch (final ArithmeticException exception) {
value = Long.MAX_VALUE;
yield Long.MAX_VALUE;
}
break;
case ValidTypeConstants.SHORT:
}
case ValidTypeConstants.SHORT -> {
try {
value = getIntegerValueOrRandom(valueLength, fieldValueList, constraints).shortValueExact();
yield getIntegerValueOrRandom(valueLength, fieldValueList, constraints).shortValueExact();
} catch (final ArithmeticException exception) {
value = Short.MAX_VALUE;
yield Short.MAX_VALUE;
}
break;
case ValidTypeConstants.DOUBLE:
}
case ValidTypeConstants.DOUBLE -> {
try {
value = getDecimalValueOrRandom(valueLength, fieldValueList, constraints).doubleValue();
yield getDecimalValueOrRandom(valueLength, fieldValueList, constraints).doubleValue();
} catch (final ArithmeticException exception) {
value = Double.MAX_VALUE;
yield Double.MAX_VALUE;
}
break;
case ValidTypeConstants.NUMBER:
case ValidTypeConstants.FLOAT:
}
case ValidTypeConstants.NUMBER, ValidTypeConstants.FLOAT -> {
try {
value = getDecimalValueOrRandom(valueLength, fieldValueList, constraints).floatValue();
yield getDecimalValueOrRandom(valueLength, fieldValueList, constraints).floatValue();
} catch (final ArithmeticException exception) {
value = Float.MAX_VALUE;
yield Float.MAX_VALUE;
}
break;
case ValidTypeConstants.BYTES:
}
case ValidTypeConstants.BYTES -> {
try {
value = getIntegerValueOrRandom(valueLength, Collections.emptyList(), Collections.emptyMap()).byteValueExact();
yield getIntegerValueOrRandom(valueLength, Collections.emptyList(), Collections.emptyMap()).byteValueExact();
} catch (final ArithmeticException exception) {
value = Byte.MAX_VALUE;
yield Byte.MAX_VALUE;
}
break;
case ValidTypeConstants.TIMESTAMP:
case ValidTypeConstants.LONG_TIMESTAMP:
case ValidTypeConstants.STRING_TIMESTAMP:
value = getTimestampValueOrRandom(fieldType, fieldValueList);
break;
case ValidTypeConstants.BOOLEAN:
value = getBooleanValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.ENUM:
value = getEnumValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.INT_DATE:
value = getDateValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.INT_TIME_MILLIS:
value = getTimeMillisValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.LONG_TIME_MICROS:
value = getTimeMicrosValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.LONG_TIMESTAMP_MILLIS:
value = getTimestampMillisValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.LONG_TIMESTAMP_MICROS:
value = getTimestampMicrosValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MILLIS:
value = getLocalTimestampMillisValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MICROS:
value = getLocalTimestampMicrosValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.UUID:
case ValidTypeConstants.STRING_UUID:
value = getUUIDValueOrRandom(fieldValueList);
break;
case ValidTypeConstants.BYTES_DECIMAL:
case ValidTypeConstants.FIXED_DECIMAL:
value = getDecimalValueOrRandom(fieldValueList, constraints);
break;
case ValidTypeConstants.INT_YEAR:
case ValidTypeConstants.INT_MONTH:
case ValidTypeConstants.INT_DAY:
value = getDateValueOrRandom(fieldType, fieldValueList);
break;
case ValidTypeConstants.INT_HOURS:
case ValidTypeConstants.INT_MINUTES:
case ValidTypeConstants.INT_SECONDS:
case ValidTypeConstants.INT_NANOS:
value = getTimeOfDayValueOrRandom(fieldType, fieldValueList);
break;
default:
value = fieldType;
break;
}

return value;
}
case ValidTypeConstants.TIMESTAMP, ValidTypeConstants.LONG_TIMESTAMP, ValidTypeConstants.STRING_TIMESTAMP -> getTimestampValueOrRandom(fieldType, fieldValueList);
case ValidTypeConstants.BOOLEAN -> getBooleanValueOrRandom(fieldValueList);
case ValidTypeConstants.ENUM -> getEnumValueOrRandom(fieldValueList);
case ValidTypeConstants.INT_DATE -> getDateValueOrRandom(fieldValueList);
case ValidTypeConstants.INT_TIME_MILLIS -> getTimeMillisValueOrRandom(fieldValueList);
case ValidTypeConstants.LONG_TIME_MICROS -> getTimeMicrosValueOrRandom(fieldValueList);
case ValidTypeConstants.LONG_TIMESTAMP_MILLIS -> getTimestampMillisValueOrRandom(fieldValueList);
case ValidTypeConstants.LONG_TIMESTAMP_MICROS -> getTimestampMicrosValueOrRandom(fieldValueList);
case ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MILLIS -> getLocalTimestampMillisValueOrRandom(fieldValueList);
case ValidTypeConstants.LONG_LOCAL_TIMESTAMP_MICROS -> getLocalTimestampMicrosValueOrRandom(fieldValueList);
case ValidTypeConstants.UUID, ValidTypeConstants.STRING_UUID -> getUUIDValueOrRandom(fieldValueList);
case ValidTypeConstants.BYTES_DECIMAL, ValidTypeConstants.FIXED_DECIMAL -> getDecimalValueOrRandom(fieldValueList, constraints);
case ValidTypeConstants.INT_YEAR, ValidTypeConstants.INT_MONTH, ValidTypeConstants.INT_DAY -> getDateValueOrRandom(fieldType, fieldValueList);
case ValidTypeConstants.INT_HOURS, ValidTypeConstants.INT_MINUTES, ValidTypeConstants.INT_SECONDS, ValidTypeConstants.INT_NANOS -> getTimeOfDayValueOrRandom(fieldType, fieldValueList);
default -> fieldType;
};
}

private BigInteger getIntegerValueOrRandom(final Integer valueLength, final List<String> fieldValueList, final Map<ConstraintTypeEnum, String> constraints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
Expand All @@ -33,12 +32,14 @@
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.serialization.Serializer;
import org.jetbrains.annotations.NotNull;

public final class KafkaProducerSampler extends AbstractJavaSamplerClient implements Serializable {

Expand Down Expand Up @@ -68,18 +69,17 @@ public final class KafkaProducerSampler extends AbstractJavaSamplerClient implem
@Override
public void setupTest(final JavaSamplerContext context) {
props = JMeterContextService.getContext().getProperties();

final var vars = JMeterContextService.getContext().getVariables();
generator = SamplerUtil.configureValueGenerator(props);

if ("true".equals(context.getJMeterVariables().get(PropsKeysHelper.SCHEMA_KEYED_MESSAGE_KEY))
|| "true".equals(context.getJMeterVariables().get(PropsKeysHelper.SIMPLE_KEYED_MESSAGE_KEY))) {
if ("true".equals(vars.get(PropsKeysHelper.SCHEMA_KEYED_MESSAGE_KEY))
|| "true".equals(vars.get(PropsKeysHelper.SIMPLE_KEYED_MESSAGE_KEY))) {
keyMessageFlag = true;
if (!Objects.isNull(JMeterContextService.getContext().getVariables().get(PropsKeysHelper.KEY_SUBJECT_NAME))) {
if (!Objects.isNull(vars.get(PropsKeysHelper.KEY_SUBJECT_NAME))) {
keyGenerator = SamplerUtil.configureKeyGenerator(props);
} else {
msgKeyType = props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_TYPE);
msgKeyValue = PropsKeysHelper.MSG_KEY_VALUE.equalsIgnoreCase(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE))
? Collections.emptyList() : Collections.singletonList(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE));
msgKeyType = getMsgKeyType(props, vars);
msgKeyValue = getMsgKeyValue(props, vars);
}
} else {
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ProducerKeysHelper.KEY_SERIALIZER_CLASS_CONFIG_DEFAULT);
Expand Down Expand Up @@ -110,6 +110,28 @@ public void setupTest(final JavaSamplerContext context) {
}
}

private String getMsgKeyType(final Properties props, final JMeterVariables vars) {
String result = props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_TYPE, null);
if (Objects.isNull(result)) {
result = vars.get(PropsKeysHelper.KEY_VALUE);
}
return result;
}

@NotNull
private List<String> getMsgKeyValue(final Properties props, final JMeterVariables vars) {

final List<String> result = new ArrayList<>();

if (PropsKeysHelper.MSG_KEY_VALUE.equalsIgnoreCase(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE))
|| Objects.isNull(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE))) {
result.add(props.getProperty(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE));
} else if (Objects.nonNull(vars.get(PropsKeysHelper.KEY_VALUE))) {
result.add(vars.get(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE));
}
return result;
}

@Override
public void teardownTest(final JavaSamplerContext context) {
if (Objects.nonNull(producer)) {
Expand Down

0 comments on commit ba55fd6

Please sign in to comment.