You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "showuon (via GitHub)" <gi...@apache.org> on 2023/03/17 05:46:30 UTC

[GitHub] [kafka] showuon commented on a diff in pull request #13410: ProducerPerformance: reduce cost of message randomisation

showuon commented on code in PR #13410:
URL: https://github.com/apache/kafka/pull/13410#discussion_r1139783205


##########
tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java:
##########
@@ -164,17 +162,9 @@ KafkaProducer<byte[], byte[]> createKafkaProducer(Properties props) {
         return new KafkaProducer<>(props);
     }
 
-    static byte[] generateRandomPayload(Integer recordSize, List<byte[]> payloadByteList, byte[] payload,
-            Random random) {
-        if (!payloadByteList.isEmpty()) {
-            payload = payloadByteList.get(random.nextInt(payloadByteList.size()));
-        } else if (recordSize != null) {
-            for (int j = 0; j < payload.length; ++j)
-                payload[j] = (byte) (random.nextInt(26) + 65);
-        } else {
-            throw new IllegalArgumentException("no payload File Path or record Size provided");
-        }
-        return payload;
+    static byte[] select(List<byte[]> payloadByteList,
+                         Random random) {
+        return payloadByteList.get(random.nextInt(payloadByteList.size()));

Review Comment:
   I'm thinking, if the record size is 1 or a small number, aren't we still wasting many randonization time for selecting next payload? I'm thinking we random selecting one start index, and then go through all the remaining array 1 by 1. After running back to the starting index, we run random again. WDYT?



##########
tools/src/main/java/org/apache/kafka/tools/ProducerPerformance.java:
##########
@@ -200,6 +190,37 @@ static Properties readProps(List<String> producerProps, String producerConfig, S
         return props;
     }
 
+
+    static List<byte[]> generatePayloads(String payloadFilePath, String payloadDelimiter, Integer recordSize, Random random) throws IOException {
+        if (payloadFilePath != null) {
+            return readPayloadFile(payloadFilePath, payloadDelimiter);
+        } else if (recordSize != null) {
+            return generateRandomPayloads(recordSize, random);
+        } else {
+            throw new IllegalArgumentException("no payload File Path or record Size provided");
+        }
+    }
+
+
+    private static List<byte[]> generateRandomPayloads(final int recordSize, final Random random) {
+        if (recordSize == 0) {
+            final ArrayList<byte[]> bytes = new ArrayList<>(1);
+            bytes.add(new byte[0]);
+            return bytes;
+        }

Review Comment:
   Is this for backward compatibility?



##########
tools/src/test/java/org/apache/kafka/tools/ProducerPerformanceTest.java:
##########
@@ -120,39 +119,42 @@ public void testUnexpectedArg() {
 
     @Test
     public void testGenerateRandomPayloadByPayloadFile() {
-        Integer recordSize = null;
         String inputString = "Hello Kafka";
         byte[] byteArray = inputString.getBytes(StandardCharsets.UTF_8);
         List<byte[]> payloadByteList = new ArrayList<>();
         payloadByteList.add(byteArray);
         byte[] payload = null;
         Random random = new Random(0);
 
-        payload = ProducerPerformance.generateRandomPayload(recordSize, payloadByteList, payload, random);
+        payload = ProducerPerformance.select(payloadByteList, random);
         assertEquals(inputString, new String(payload));
     }
 
     @Test
-    public void testGenerateRandomPayloadByRecordSize() {
-        Integer recordSize = 100;
-        byte[] payload = new byte[recordSize];
-        List<byte[]> payloadByteList = new ArrayList<>();
+    public void testGenerateRandomPayloadsByRecordSize() throws IOException {
+        Integer recordSize = 1024;
         Random random = new Random(0);
 
-        payload = ProducerPerformance.generateRandomPayload(recordSize, payloadByteList, payload, random);
-        for (byte b : payload) {
-            assertNotEquals(0, b);
+        final List<byte[]> bytes = ProducerPerformance.generatePayloads(null, null, recordSize, random);
+        assertEquals(1024 * 10, bytes.size());
+        for (byte[] aByte : bytes) {
+            assertEquals(recordSize, aByte.length);
         }
     }
 
     @Test
-    public void testGenerateRandomPayloadException() {
-        Integer recordSize = null;
-        byte[] payload = null;
-        List<byte[]> payloadByteList = new ArrayList<>();
+    public void testGenerateRandomPayloadsLargerThan10M() throws IOException {
+        Integer recordSize = 11 * 1024 * 1024;
         Random random = new Random(0);
 
-        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> ProducerPerformance.generateRandomPayload(recordSize, payloadByteList, payload, random));
+        final List<byte[]> bytes = ProducerPerformance.generatePayloads(null, null, recordSize, random);
+        assertEquals(1, bytes.size());
+        assertEquals(recordSize, bytes.get(0).length);
+    }
+
+    @Test
+    public void testGeneratePayloadsException() {
+        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> ProducerPerformance.generatePayloads(null, null, null, null));

Review Comment:
   Could we add `random` and `payloadByteList` for this test, so that we can make sure we're testing payload file and record size?



-- 
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.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

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