Skip to content

Commit

Permalink
feat: add a random string in the topic name
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Yu <ningyu@automq.com>
  • Loading branch information
Chillax-0v0 authored and superhx committed Dec 25, 2024
1 parent 339c75a commit b47ffab
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.apache.kafka.tools.automq.perf;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.tools.automq.perf.ConsumerService.ConsumersConfig;
import org.apache.kafka.tools.automq.perf.ProducerService.ProducersConfig;
Expand Down Expand Up @@ -79,7 +81,7 @@ public PerfConfig(String[] args) {
producerConfigs = parseConfigs(ns.getList("producerConfigs"));
consumerConfigs = parseConfigs(ns.getList("consumerConfigs"));
reset = ns.getBoolean("reset");
topicPrefix = ns.getString("topicPrefix") == null ? "topic_" + System.currentTimeMillis() : ns.getString("topicPrefix");
topicPrefix = ns.getString("topicPrefix") == null ? randomTopicPrefix() : ns.getString("topicPrefix");
topics = ns.getInt("topics");
partitionsPerTopic = ns.getInt("partitionsPerTopic");
producersPerTopic = ns.getInt("producersPerTopic");
Expand Down Expand Up @@ -305,6 +307,20 @@ private Map<String, String> parseConfigs(List<String> configs) {
return map;
}

private String randomTopicPrefix() {
return String.format("topic_%d_%s", System.currentTimeMillis(), randomString(4));
}

private String randomString(int length) {
final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random r = ThreadLocalRandom.current();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(CHARACTERS.charAt(r.nextInt(CHARACTERS.length())));
}
return sb.toString();
}

static class IntegerArgumentType extends ReflectArgumentType<Integer> {

private final IntegerValidator validator;
Expand Down

0 comments on commit b47ffab

Please sign in to comment.