Created
November 22, 2015 22:11
-
-
Save paulobsf/46967479e2af9aee92bc to your computer and use it in GitHub Desktop.
Apache Kafka Simple Write Performance Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.pauloferreira.ktests; | |
import org.apache.kafka.clients.producer.KafkaProducer; | |
import org.apache.kafka.clients.producer.ProducerRecord; | |
import org.apache.kafka.common.serialization.StringSerializer; | |
import java.util.Date; | |
import java.util.Properties; | |
import java.util.Random; | |
/*** | |
* Apache Kafka Simple Write Performance Test | |
* | |
* Based on: | |
* https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example | |
* | |
* Depends on org.apache.kafka:kafka-clients:0.8.2.2 | |
*/ | |
public class KafkaWriteTester { | |
public static void main(String[] args) { | |
Properties props = new Properties(); | |
props.put("bootstrap.servers", "localhost:9092"); | |
String stringSerializerClassName = StringSerializer.class.getCanonicalName(); | |
props.put("key.serializer", stringSerializerClassName); | |
props.put("value.serializer", stringSerializerClassName); | |
KafkaProducer<String, String> producer = new KafkaProducer<>(props); | |
long events = 10_000_000L; | |
Random rnd = new Random(); | |
System.out.println("START"); | |
long start = new Date().getTime(); | |
for (long nEvents = 0; nEvents < events; nEvents++) { | |
long runtime = new Date().getTime(); | |
String ip = "192.168.2." + rnd.nextInt(255); | |
String msg = runtime + ",www.example.com," + ip; | |
ProducerRecord<String, String> data = new ProducerRecord<>("page_visits", ip, msg); | |
producer.send(data); | |
} | |
producer.close(); | |
System.out.println("FINISH"); | |
System.out.println("Took " + (new Date().getTime() - start) + "ms"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took < 7.5s