You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/05/04 04:05:34 UTC

[GitHub] [kafka] chia7712 commented on a change in pull request #10588: KAFKA-12662: add unit test for ProducerPerformance

chia7712 commented on a change in pull request #10588:
URL: https://github.com/apache/kafka/pull/10588#discussion_r625495588



##########
File path: tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tools;
+
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.Callback;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import static java.util.Arrays.asList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+public class ProducerPerformanceTest {
+
+    @Mock
+    KafkaProducer<byte[], byte[]> producerMock;
+
+    @Spy
+    ProducerPerformance producerPerformaceSpy;

Review comment:
       typo: `producerPerformaceSpy` -> `producerPerformanceSpy`
   

##########
File path: tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tools;
+
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.Callback;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import static java.util.Arrays.asList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+public class ProducerPerformanceTest {
+
+    @Mock
+    KafkaProducer<byte[], byte[]> producerMock;
+
+    @Spy
+    ProducerPerformance producerPerformaceSpy;
+
+    private File createTempFile(String contents) throws IOException {
+        File file = File.createTempFile("ProducerPerformanceTest", ".tmp");
+        file.deleteOnExit();
+        final FileWriter writer = new FileWriter(file);
+        writer.write(contents);
+        writer.close();
+        return file;
+    }
+
+    @Test
+    public void testReadPayloadFile() throws Exception {
+        File payloadFile = createTempFile("Hello\nKafka");
+        String payloadFilePath = payloadFile.getAbsolutePath();
+        String payloadDelimiter = "\n";
+
+        List<byte[]> payloadByteList = ProducerPerformance.readPayloadFile(payloadFilePath, payloadDelimiter);
+
+        assertEquals(2, payloadByteList.size());
+        assertEquals("Hello", new String(payloadByteList.get(0)));
+        assertEquals("Kafka", new String(payloadByteList.get(1)));
+    }
+
+    @Test
+    public void testReadProps() throws Exception {
+        
+        List<String> producerProps = asList("bootstrap.servers=localhost:9000");
+        String producerConfig = createTempFile("acks=1").getAbsolutePath();
+        String transactionalId = "1234";
+        boolean transactionsEnabled = true;
+
+        Properties prop = ProducerPerformance.readProps(producerProps, producerConfig, transactionalId, transactionsEnabled);
+
+        assertNotNull(prop);
+        assertEquals(5, prop.size());
+    }
+
+    @Test
+    public void testNumberOfCallsForSendAndClose() throws IOException {
+
+        doReturn(null).when(producerMock).send(ArgumentMatchers.<ProducerRecord<byte[], byte[]>>any(), ArgumentMatchers.<Callback>any());
+        doReturn(producerMock).when(producerPerformaceSpy).createKafkaProducer(any(Properties.class));
+
+        String[] args = new String[] {"--topic", "Hello-Kafka", "--num-records", "5", "--throughput", "100", "--record-size", "100", "--producer-props", "bootstrap.servers=localhost:9000"};
+        producerPerformaceSpy.start(args);
+        verify(producerMock, times(5)).send(ArgumentMatchers.<ProducerRecord<byte[], byte[]>>any(), ArgumentMatchers.<Callback>any());
+        verify(producerMock, times(1)).close();
+    }
+
+    @Test
+    public void testUnexpectedArg() throws Exception {

Review comment:
       unnecessary `Exception`

##########
File path: tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tools;
+
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.Callback;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import static java.util.Arrays.asList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+public class ProducerPerformanceTest {
+
+    @Mock
+    KafkaProducer<byte[], byte[]> producerMock;
+
+    @Spy
+    ProducerPerformance producerPerformaceSpy;
+
+    private File createTempFile(String contents) throws IOException {
+        File file = File.createTempFile("ProducerPerformanceTest", ".tmp");
+        file.deleteOnExit();
+        final FileWriter writer = new FileWriter(file);
+        writer.write(contents);
+        writer.close();
+        return file;
+    }
+
+    @Test
+    public void testReadPayloadFile() throws Exception {
+        File payloadFile = createTempFile("Hello\nKafka");
+        String payloadFilePath = payloadFile.getAbsolutePath();
+        String payloadDelimiter = "\n";
+
+        List<byte[]> payloadByteList = ProducerPerformance.readPayloadFile(payloadFilePath, payloadDelimiter);
+
+        assertEquals(2, payloadByteList.size());
+        assertEquals("Hello", new String(payloadByteList.get(0)));
+        assertEquals("Kafka", new String(payloadByteList.get(1)));
+    }
+
+    @Test
+    public void testReadProps() throws Exception {
+        
+        List<String> producerProps = asList("bootstrap.servers=localhost:9000");
+        String producerConfig = createTempFile("acks=1").getAbsolutePath();
+        String transactionalId = "1234";
+        boolean transactionsEnabled = true;
+
+        Properties prop = ProducerPerformance.readProps(producerProps, producerConfig, transactionalId, transactionsEnabled);
+
+        assertNotNull(prop);
+        assertEquals(5, prop.size());
+    }
+
+    @Test
+    public void testNumberOfCallsForSendAndClose() throws IOException {
+
+        doReturn(null).when(producerMock).send(ArgumentMatchers.<ProducerRecord<byte[], byte[]>>any(), ArgumentMatchers.<Callback>any());

Review comment:
       redundant type arguments `<ProducerRecord<byte[], byte[]`

##########
File path: tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
##########
@@ -190,8 +166,53 @@ public static void main(String[] args) throws Exception {
 
     }
 
+    public KafkaProducer<byte[], byte[]> createKafkaProducer(Properties props) {
+        return new KafkaProducer<>(props);
+    }
+    
+    public static Properties readProps(List<String> producerProps, String producerConfig, String transactionalId,
+            boolean transactionsEnabled) throws IOException {
+        Properties props = new Properties();
+        if (producerConfig != null) {
+            props.putAll(Utils.loadProps(producerConfig));
+        }
+        if (producerProps != null)
+            for (String prop : producerProps) {
+                String[] pieces = prop.split("=");
+                if (pieces.length != 2)
+                    throw new IllegalArgumentException("Invalid property: " + prop);
+                props.put(pieces[0], pieces[1]);
+            }
+
+        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);
+        return props;
+    }
+
+    public static List<byte[]> readPayloadFile(String payloadFilePath, String payloadDelimiter) throws IOException {

Review comment:
       ditto. package-private

##########
File path: tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
##########
@@ -46,6 +47,11 @@
 public class ProducerPerformance {
 
     public static void main(String[] args) throws Exception {
+        ProducerPerformance perf = new ProducerPerformance();
+        perf.start(args);
+    }
+    
+    public void start(String[] args) throws IOException {

Review comment:
       Could you change it from public to package-private?

##########
File path: tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
##########
@@ -190,8 +166,53 @@ public static void main(String[] args) throws Exception {
 
     }
 
+    public KafkaProducer<byte[], byte[]> createKafkaProducer(Properties props) {
+        return new KafkaProducer<>(props);
+    }
+    
+    public static Properties readProps(List<String> producerProps, String producerConfig, String transactionalId,

Review comment:
       ditto. package-private

##########
File path: tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tools;
+
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.Callback;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import static java.util.Arrays.asList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+public class ProducerPerformanceTest {
+
+    @Mock
+    KafkaProducer<byte[], byte[]> producerMock;
+
+    @Spy
+    ProducerPerformance producerPerformaceSpy;
+
+    private File createTempFile(String contents) throws IOException {
+        File file = File.createTempFile("ProducerPerformanceTest", ".tmp");
+        file.deleteOnExit();
+        final FileWriter writer = new FileWriter(file);
+        writer.write(contents);
+        writer.close();
+        return file;
+    }
+
+    @Test
+    public void testReadPayloadFile() throws Exception {
+        File payloadFile = createTempFile("Hello\nKafka");
+        String payloadFilePath = payloadFile.getAbsolutePath();
+        String payloadDelimiter = "\n";
+
+        List<byte[]> payloadByteList = ProducerPerformance.readPayloadFile(payloadFilePath, payloadDelimiter);
+
+        assertEquals(2, payloadByteList.size());
+        assertEquals("Hello", new String(payloadByteList.get(0)));
+        assertEquals("Kafka", new String(payloadByteList.get(1)));
+    }
+
+    @Test
+    public void testReadProps() throws Exception {
+        
+        List<String> producerProps = asList("bootstrap.servers=localhost:9000");

Review comment:
       `asList` -> `Collections.singletonList`

##########
File path: tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
##########
@@ -190,8 +166,53 @@ public static void main(String[] args) throws Exception {
 
     }
 
+    public KafkaProducer<byte[], byte[]> createKafkaProducer(Properties props) {
+        return new KafkaProducer<>(props);
+    }
+    
+    public static Properties readProps(List<String> producerProps, String producerConfig, String transactionalId,
+            boolean transactionsEnabled) throws IOException {
+        Properties props = new Properties();
+        if (producerConfig != null) {
+            props.putAll(Utils.loadProps(producerConfig));
+        }
+        if (producerProps != null)
+            for (String prop : producerProps) {
+                String[] pieces = prop.split("=");
+                if (pieces.length != 2)
+                    throw new IllegalArgumentException("Invalid property: " + prop);
+                props.put(pieces[0], pieces[1]);
+            }
+
+        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);
+        return props;
+    }
+
+    public static List<byte[]> readPayloadFile(String payloadFilePath, String payloadDelimiter) throws IOException {
+        List<byte[]> payloadByteList = new ArrayList<>();
+        if (payloadFilePath != null) {
+            Path path = Paths.get(payloadFilePath);
+            System.out.println("Reading payloads from: " + path.toAbsolutePath());
+            if (Files.notExists(path) || Files.size(path) == 0)  {
+                throw new IllegalArgumentException("File does not exist or empty file provided.");
+            }
+
+            String[] payloadList = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).split(payloadDelimiter);
+
+            System.out.println("Number of messages read: " + payloadList.length);
+
+            for (String payload : payloadList) {
+                payloadByteList.add(payload.getBytes(StandardCharsets.UTF_8));
+            }
+        }
+        return payloadByteList;
+    }
+
     /** Get the command-line argument parser. */
-    private static ArgumentParser argParser() {
+    public static ArgumentParser argParser() {

Review comment:
       ditto. package-private

##########
File path: tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java
##########
@@ -190,8 +166,53 @@ public static void main(String[] args) throws Exception {
 
     }
 
+    public KafkaProducer<byte[], byte[]> createKafkaProducer(Properties props) {

Review comment:
       ditto. package-private




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org