You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ch...@apache.org on 2024/08/20 02:24:01 UTC

(kafka) branch trunk updated: KAFKA-16900 kafka-producer-perf-test reports error when using transaction. (#16646)

This is an automated email from the ASF dual-hosted git repository.

chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 42715654095 KAFKA-16900 kafka-producer-perf-test reports error when using transaction. (#16646)
42715654095 is described below

commit 42715654095d019effc478ab343c0132e02aa70f
Author: Kuan-Po Tseng <br...@gmail.com>
AuthorDate: Tue Aug 20 10:23:55 2024 +0800

    KAFKA-16900 kafka-producer-perf-test reports error when using transaction. (#16646)
    
    Currently, users need to set --transaction-duration-ms to enable transactions in kafka-producer-perf-test, which is not straightforward. A better approach is to enable transactions when a transaction ID is provided.
    
    This PR allows enabling transaction in kafka-producer-perf-test by either
    
    - set transaction.id=<id> via --producer-props or
    - set transaction.id=<id> in config file via --producer.config or
    - set --transaction-id <id> or
    - set --transaction-duration-ms=<ms>
    
    Reviewers: Chia-Ping Tsai <ch...@gmail.com>
---
 .../apache/kafka/tools/ProducerPerformance.java    | 141 ++++++++++------
 .../kafka/tools/ProducerPerformanceTest.java       | 178 ++++++++++++++++++++-
 2 files changed, 261 insertions(+), 58 deletions(-)

diff --git a/tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java b/tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
index 3015fdee9f8..c9317d8a24c 100644
--- a/tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
+++ b/tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
@@ -21,6 +21,7 @@ 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.Uuid;
 import org.apache.kafka.common.utils.Exit;
 import org.apache.kafka.common.utils.Utils;
 import org.apache.kafka.server.util.ThroughputThrottler;
@@ -39,6 +40,7 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.SplittableRandom;
 
@@ -47,79 +49,56 @@ import static net.sourceforge.argparse4j.impl.Arguments.storeTrue;
 
 public class ProducerPerformance {
 
+    public static final String DEFAULT_TRANSACTION_ID_PREFIX = "performance-producer-";
+    public static final long DEFAULT_TRANSACTION_DURATION_MS = 3000L;
+
     public static void main(String[] args) throws Exception {
         ProducerPerformance perf = new ProducerPerformance();
         perf.start(args);
     }
-    
+
     void start(String[] args) throws IOException {
         ArgumentParser parser = argParser();
 
         try {
-            Namespace res = parser.parseArgs(args);
-
-            /* parse args */
-            String topicName = res.getString("topic");
-            long numRecords = res.getLong("numRecords");
-            Integer recordSize = res.getInt("recordSize");
-            double throughput = res.getDouble("throughput");
-            boolean payloadMonotonic = res.getBoolean("payloadMonotonic");
-            List<String> producerProps = res.getList("producerConfig");
-            String producerConfig = res.getString("producerConfigFile");
-            String payloadFilePath = res.getString("payloadFile");
-            String transactionalId = res.getString("transactionalId");
-            boolean shouldPrintMetrics = res.getBoolean("printMetrics");
-            long transactionDurationMs = res.getLong("transactionDurationMs");
-            boolean transactionsEnabled =  0 < transactionDurationMs;
-
-            // since default value gets printed with the help text, we are escaping \n there and replacing it with correct value here.
-            String payloadDelimiter = res.getString("payloadDelimiter").equals("\\n") ? "\n" : res.getString("payloadDelimiter");
-
-            if (producerProps == null && producerConfig == null) {
-                throw new ArgumentParserException("Either --producer-props or --producer.config must be specified.", parser);
-            }
-
-            List<byte[]> payloadByteList = readPayloadFile(payloadFilePath, payloadDelimiter);
-
-            Properties props = readProps(producerProps, producerConfig, transactionalId, transactionsEnabled);
-
-            KafkaProducer<byte[], byte[]> producer = createKafkaProducer(props);
+            ConfigPostProcessor config = new ConfigPostProcessor(parser, args);
+            KafkaProducer<byte[], byte[]> producer = createKafkaProducer(config.producerProps);
 
-            if (transactionsEnabled)
+            if (config.transactionsEnabled)
                 producer.initTransactions();
 
             /* setup perf test */
             byte[] payload = null;
-            if (recordSize != null) {
-                payload = new byte[recordSize];
+            if (config.recordSize != null) {
+                payload = new byte[config.recordSize];
             }
-            // not threadsafe, do not share with other threads
+            // not thread-safe, do not share with other threads
             SplittableRandom random = new SplittableRandom(0);
             ProducerRecord<byte[], byte[]> record;
-            stats = new Stats(numRecords, 5000);
+            stats = new Stats(config.numRecords, 5000);
             long startMs = System.currentTimeMillis();
 
-            ThroughputThrottler throttler = new ThroughputThrottler(throughput, startMs);
+            ThroughputThrottler throttler = new ThroughputThrottler(config.throughput, startMs);
 
             int currentTransactionSize = 0;
             long transactionStartTime = 0;
-            for (long i = 0; i < numRecords; i++) {
+            for (long i = 0; i < config.numRecords; i++) {
 
-                payload = generateRandomPayload(recordSize, payloadByteList, payload, random, payloadMonotonic, i);
+                payload = generateRandomPayload(config.recordSize, config.payloadByteList, payload, random, config.payloadMonotonic, i);
 
-                if (transactionsEnabled && currentTransactionSize == 0) {
+                if (config.transactionsEnabled && currentTransactionSize == 0) {
                     producer.beginTransaction();
                     transactionStartTime = System.currentTimeMillis();
                 }
 
-                record = new ProducerRecord<>(topicName, payload);
+                record = new ProducerRecord<>(config.topicName, payload);
 
                 long sendStartMs = System.currentTimeMillis();
                 cb = new PerfCallback(sendStartMs, payload.length, stats);
                 producer.send(record, cb);
 
                 currentTransactionSize++;
-                if (transactionsEnabled && transactionDurationMs <= (sendStartMs - transactionStartTime)) {
+                if (config.transactionsEnabled && config.transactionDurationMs <= (sendStartMs - transactionStartTime)) {
                     producer.commitTransaction();
                     currentTransactionSize = 0;
                 }
@@ -129,10 +108,10 @@ public class ProducerPerformance {
                 }
             }
 
-            if (transactionsEnabled && currentTransactionSize != 0)
+            if (config.transactionsEnabled && currentTransactionSize != 0)
                 producer.commitTransaction();
 
-            if (!shouldPrintMetrics) {
+            if (!config.shouldPrintMetrics) {
                 producer.close();
 
                 /* print final results */
@@ -185,8 +164,7 @@ public class ProducerPerformance {
         return payload;
     }
     
-    static Properties readProps(List<String> producerProps, String producerConfig, String transactionalId,
-            boolean transactionsEnabled) throws IOException {
+    static Properties readProps(List<String> producerProps, String producerConfig) throws IOException {
         Properties props = new Properties();
         if (producerConfig != null) {
             props.putAll(Utils.loadProps(producerConfig));
@@ -201,7 +179,6 @@ public class ProducerPerformance {
 
         props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
         props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
-        if (transactionsEnabled) props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionalId);
         if (props.getProperty(ProducerConfig.CLIENT_ID_CONFIG) == null) {
             props.put(ProducerConfig.CLIENT_ID_CONFIG, "perf-producer-client");
         }
@@ -233,7 +210,10 @@ public class ProducerPerformance {
         ArgumentParser parser = ArgumentParsers
                 .newArgumentParser("producer-performance")
                 .defaultHelp(true)
-                .description("This tool is used to verify the producer performance.");
+                .description("This tool is used to verify the producer performance. To enable transactions, " +
+                        "you can specify a transaction id or set a transaction duration using --transaction-duration-ms. " +
+                        "There are three ways to specify the transaction id: set transaction.id=<id> via --producer-props, " +
+                        "set transaction.id=<id> in the config file via --producer.config, or use --transaction-id <id>.");
 
         MutuallyExclusiveGroup payloadOptions = parser
                 .addMutuallyExclusiveGroup()
@@ -330,8 +310,10 @@ public class ProducerPerformance {
                .type(String.class)
                .metavar("TRANSACTIONAL-ID")
                .dest("transactionalId")
-               .setDefault("performance-producer-default-transactional-id")
-               .help("The transactionalId to use if transaction-duration-ms is > 0. Useful when testing the performance of concurrent transactions.");
+               .help("The transactional id to use. This config takes precedence over the transactional.id " +
+                       "specified via --producer.config or --producer-props. Note that if the transactional id " +
+                       "is not specified while --transaction-duration-ms is provided, the default value for the " +
+                       "transactional id will be performance-producer- followed by a random uuid.");
 
         parser.addArgument("--transaction-duration-ms")
                .action(store())
@@ -339,9 +321,10 @@ public class ProducerPerformance {
                .type(Long.class)
                .metavar("TRANSACTION-DURATION")
                .dest("transactionDurationMs")
-               .setDefault(0L)
-               .help("The max age of each transaction. The commitTransaction will be called after this time has elapsed. Transactions are only enabled if this value is positive.");
-
+               .help("The max age of each transaction. The commitTransaction will be called after this time has elapsed. " +
+                       "The value should be greater than 0. If the transactional id is specified via --producer-props, " +
+                       "--producer.config, or --transactional-id but --transaction-duration-ms is not specified, " +
+                       "the default value will be 3000.");
 
         return parser;
     }
@@ -494,4 +477,60 @@ public class ProducerPerformance {
         }
     }
 
+    static final class ConfigPostProcessor {
+        final String topicName;
+        final long numRecords;
+        final Integer recordSize;
+        final double throughput;
+        final boolean payloadMonotonic;
+        final Properties producerProps;
+        final boolean shouldPrintMetrics;
+        final Long transactionDurationMs;
+        final boolean transactionsEnabled;
+        final List<byte[]> payloadByteList;
+
+        public ConfigPostProcessor(ArgumentParser parser, String[] args) throws IOException, ArgumentParserException {
+            Namespace namespace = parser.parseArgs(args);
+            this.topicName = namespace.getString("topic");
+            this.numRecords = namespace.getLong("numRecords");
+            this.recordSize = namespace.getInt("recordSize");
+            this.throughput = namespace.getDouble("throughput");
+            this.payloadMonotonic = namespace.getBoolean("payloadMonotonic");
+            this.shouldPrintMetrics = namespace.getBoolean("printMetrics");
+
+            List<String> producerConfigs = namespace.getList("producerConfig");
+            String producerConfigFile = namespace.getString("producerConfigFile");
+            String payloadFilePath = namespace.getString("payloadFile");
+            Long transactionDurationMsArg = namespace.getLong("transactionDurationMs");
+            String transactionIdArg = namespace.getString("transactionalId");
+            if (producerConfigs == null && producerConfigFile == null) {
+                throw new ArgumentParserException("Either --producer-props or --producer.config must be specified.", parser);
+            }
+            if (transactionDurationMsArg != null && transactionDurationMsArg <= 0) {
+                throw new ArgumentParserException("--transaction-duration-ms should > 0", parser);
+            }
+
+            // since default value gets printed with the help text, we are escaping \n there and replacing it with correct value here.
+            String payloadDelimiter = namespace.getString("payloadDelimiter").equals("\\n")
+                    ? "\n" : namespace.getString("payloadDelimiter");
+            this.payloadByteList = readPayloadFile(payloadFilePath, payloadDelimiter);
+            this.producerProps = readProps(producerConfigs, producerConfigFile);
+            // setup transaction related configs
+            this.transactionsEnabled = transactionDurationMsArg != null
+                    || transactionIdArg != null
+                    || producerProps.containsKey(ProducerConfig.TRANSACTIONAL_ID_CONFIG);
+            if (transactionsEnabled) {
+                Optional<String> txIdInProps =
+                        Optional.ofNullable(producerProps.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG))
+                                .map(Object::toString);
+                String transactionId = Optional.ofNullable(transactionIdArg).orElse(txIdInProps.orElse(DEFAULT_TRANSACTION_ID_PREFIX + Uuid.randomUuid().toString()));
+                producerProps.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionId);
+
+                if (transactionDurationMsArg == null) {
+                    transactionDurationMsArg = DEFAULT_TRANSACTION_DURATION_MS;
+                }
+            }
+            this.transactionDurationMs = transactionDurationMsArg;
+        }
+    }
 }
diff --git a/tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java b/tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
index d8dec4f6b7f..1757fa86069 100644
--- a/tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
+++ b/tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
@@ -18,7 +18,9 @@ package org.apache.kafka.tools;
 
 import org.apache.kafka.clients.producer.Callback;
 import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
 import org.apache.kafka.common.errors.AuthorizationException;
+import org.apache.kafka.common.utils.Utils;
 
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
@@ -47,8 +49,10 @@ import java.util.concurrent.TimeUnit;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
@@ -85,19 +89,19 @@ public class ProducerPerformanceTest {
         assertEquals(2, payloadByteList.size());
         assertEquals("Hello", new String(payloadByteList.get(0)));
         assertEquals("Kafka", new String(payloadByteList.get(1)));
+        Utils.delete(payloadFile);
     }
 
     @Test
     public void testReadProps() throws Exception {
         List<String> producerProps = Collections.singletonList("bootstrap.servers=localhost:9000");
-        String producerConfig = createTempFile("acks=1").getAbsolutePath();
-        String transactionalId = "1234";
-        boolean transactionsEnabled = true;
+        File producerConfig = createTempFile("acks=1");
 
-        Properties prop = ProducerPerformance.readProps(producerProps, producerConfig, transactionalId, transactionsEnabled);
+        Properties prop = ProducerPerformance.readProps(producerProps, producerConfig.getAbsolutePath());
 
         assertNotNull(prop);
-        assertEquals(6, prop.size());
+        assertEquals(5, prop.size());
+        Utils.delete(producerConfig);
     }
 
     @Test
@@ -116,6 +120,24 @@ public class ProducerPerformanceTest {
         verify(producerMock, times(1)).close();
     }
 
+    @Test
+    public void testEnableTransaction() throws IOException {
+        doReturn(null).when(producerMock).send(any(), any());
+        doReturn(producerMock).when(producerPerformanceSpy).createKafkaProducer(any(Properties.class));
+
+        String[] args = new String[] {
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--transactional-id", "foobar",
+            "--producer-props", "bootstrap.servers=localhost:9000"};
+        producerPerformanceSpy.start(args);
+        verify(producerMock, times(1)).beginTransaction();
+        verify(producerMock, times(1)).commitTransaction();
+        verify(producerMock, times(1)).close();
+    }
+
     @Test
     public void testNumberOfSuccessfulSendAndClose() throws IOException {
         doReturn(producerMock).when(producerPerformanceSpy).createKafkaProducer(any(Properties.class));
@@ -265,7 +287,7 @@ public class ProducerPerformanceTest {
     public void testClientIdOverride()  throws Exception {
         List<String> producerProps = Collections.singletonList("client.id=producer-1");
 
-        Properties prop = ProducerPerformance.readProps(producerProps, null, "1234", true);
+        Properties prop = ProducerPerformance.readProps(producerProps, null);
 
         assertNotNull(prop);
         assertEquals("producer-1", prop.getProperty("client.id"));
@@ -275,7 +297,7 @@ public class ProducerPerformanceTest {
     public void testDefaultClientId() throws Exception {
         List<String> producerProps = Collections.singletonList("acks=1");
 
-        Properties prop = ProducerPerformance.readProps(producerProps, null, "1234", true);
+        Properties prop = ProducerPerformance.readProps(producerProps, null);
 
         assertNotNull(prop);
         assertEquals("perf-producer-client", prop.getProperty("client.id"));
@@ -308,4 +330,146 @@ public class ProducerPerformanceTest {
         assertEquals(500000, stats.index());
         assertEquals(1000000 * 100, stats.bytes());
     }
+
+    @Test
+    public void testConfigPostProcessor() throws IOException, ArgumentParserException {
+        ArgumentParser parser = ProducerPerformance.argParser();
+        String[] args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--print-metrics",
+            "--producer-props", "bootstrap.servers=localhost:9000",
+            "--transactional-id", "foobar",
+            "--transaction-duration-ms", "5000",
+        };
+        ProducerPerformance.ConfigPostProcessor configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertEquals("Hello-Kafka", configs.topicName);
+        assertEquals(5, configs.numRecords);
+        assertEquals(100, configs.throughput);
+        assertEquals(100, configs.recordSize);
+        assertFalse(configs.payloadMonotonic);
+        assertTrue(configs.shouldPrintMetrics);
+        assertTrue(configs.payloadByteList.isEmpty());
+        Properties props = configs.producerProps;
+        assertEquals(5, props.size());
+        assertTrue(configs.transactionsEnabled);
+        assertEquals(5000, configs.transactionDurationMs);
+    }
+
+    @Test
+    public void testInvalidConfigPostProcessor() {
+        ArgumentParser parser = ProducerPerformance.argParser();
+        String[] invalidProducerProps = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100"};
+        assertEquals("Either --producer-props or --producer.config must be specified.",
+                assertThrows(ArgumentParserException.class,
+                        () -> new ProducerPerformance.ConfigPostProcessor(parser, invalidProducerProps)).getMessage());
+
+        String[] invalidTransactionDurationMs = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--producer-props", "bootstrap.servers=localhost:9000",
+            "--transaction-duration-ms", "0"};
+        assertEquals("--transaction-duration-ms should > 0",
+                assertThrows(ArgumentParserException.class,
+                        () -> new ProducerPerformance.ConfigPostProcessor(parser, invalidTransactionDurationMs)).getMessage());
+    }
+
+    @Test
+    public void testNoTransactionRelatedConfigs() throws IOException, ArgumentParserException {
+        ArgumentParser parser = ProducerPerformance.argParser();
+        String[] args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--producer-props", "bootstrap.servers=localhost:9000"};
+        ProducerPerformance.ConfigPostProcessor configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertFalse(configs.transactionsEnabled);
+        assertNull(configs.transactionDurationMs);
+        assertFalse(configs.producerProps.contains(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
+    }
+
+    @Test
+    public void testEnableTransactionByProducerProps() throws IOException, ArgumentParserException {
+        ArgumentParser parser = ProducerPerformance.argParser();
+        String[] args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--producer-props", "bootstrap.servers=localhost:9000", "transactional.id=foobar"};
+        ProducerPerformance.ConfigPostProcessor configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertTrue(configs.transactionsEnabled);
+        assertEquals(ProducerPerformance.DEFAULT_TRANSACTION_DURATION_MS, configs.transactionDurationMs);
+        assertEquals("foobar", configs.producerProps.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
+    }
+
+    @Test
+    public void testEnableTransactionByTransactionId() throws IOException, ArgumentParserException {
+        File producerConfigFile = createTempFile("transactional.id=foobar");
+        ArgumentParser parser = ProducerPerformance.argParser();
+        String[] args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--producer.config", producerConfigFile.getAbsolutePath(),
+            "--producer-props", "bootstrap.servers=localhost:9000"};
+        ProducerPerformance.ConfigPostProcessor configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertTrue(configs.transactionsEnabled);
+        assertEquals(ProducerPerformance.DEFAULT_TRANSACTION_DURATION_MS, configs.transactionDurationMs);
+        assertEquals("foobar", configs.producerProps.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
+
+        args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--producer.config", producerConfigFile.getAbsolutePath(),
+            "--producer-props", "bootstrap.servers=localhost:9000", "transactional.id=hello_kafka"};
+        configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertTrue(configs.transactionsEnabled);
+        assertEquals(ProducerPerformance.DEFAULT_TRANSACTION_DURATION_MS, configs.transactionDurationMs);
+        assertEquals("hello_kafka", configs.producerProps.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
+
+        args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--transactional-id", "kafka_hello",
+            "--producer.config", producerConfigFile.getAbsolutePath(),
+            "--producer-props", "bootstrap.servers=localhost:9000", "transactional.id=hello_kafka"};
+        configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertTrue(configs.transactionsEnabled);
+        assertEquals(ProducerPerformance.DEFAULT_TRANSACTION_DURATION_MS, configs.transactionDurationMs);
+        assertEquals("kafka_hello", configs.producerProps.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
+
+        Utils.delete(producerConfigFile);
+    }
+
+    @Test
+    public void testEnableTransactionByTransactionDurationMs() throws IOException, ArgumentParserException {
+        ArgumentParser parser = ProducerPerformance.argParser();
+        String[] args = new String[]{
+            "--topic", "Hello-Kafka",
+            "--num-records", "5",
+            "--throughput", "100",
+            "--record-size", "100",
+            "--transaction-duration-ms", "5000",
+            "--producer-props", "bootstrap.servers=localhost:9000"};
+        ProducerPerformance.ConfigPostProcessor configs = new ProducerPerformance.ConfigPostProcessor(parser, args);
+        assertTrue(configs.transactionsEnabled);
+        assertEquals(5000, configs.transactionDurationMs);
+        assertTrue(configs.producerProps.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG).toString()
+                .startsWith(ProducerPerformance.DEFAULT_TRANSACTION_ID_PREFIX));
+    }
 }