Skip to content

Instantly share code, notes, and snippets.

@paulobsf
Created November 22, 2015 22:11
Show Gist options
  • Save paulobsf/46967479e2af9aee92bc to your computer and use it in GitHub Desktop.
Save paulobsf/46967479e2af9aee92bc to your computer and use it in GitHub Desktop.
Apache Kafka Simple Write Performance Test
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");
}
}
@paulobsf
Copy link
Author

Took < 7.5s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment