static void Main(string[] args)
{
var config = new KafkaSimpleManagerConfiguration()
{
Zookeeper = "127.0.0.1:2181,127.0.0.1:2181",
MaxMessageSize = SyncProducerConfiguration.DefaultMaxMessageSize
};
var producerConfigTemplate = new ProducerConfiguration(
new List<BrokerConfiguration>()) //The Brokers will be replaced inside of KafkaSimpleManager
{
ForceToPartition = -1,
PartitionerClass = config.PartitionerClass,
TotalNumPartitions = 0,
RequiredAcks = 0,
AckTimeout = SyncProducerConfiguration.DefaultAckTimeout,
SendTimeout = SyncProducerConfiguration.DefaultSendTimeout,
ReceiveTimeout = SyncProducerConfiguration.DefaultReceiveTimeout,
CompressionCodec = CompressionCodecs.NoCompressionCodec,
BufferSize = SyncProducerConfiguration.DefaultBufferSize,
SyncProducerOfOneBroker = ProducerConfiguration.DefaultSyncProducerOfOneBroker, //Actually it's sync producer socket count of one partition
MaxMessageSize = SyncProducerConfiguration.DefaultMaxMessageSize
};
var kafkaSimpleManage = new KafkaSimpleManager<byte[], Message>(config);
kafkaSimpleManage.InitializeProducerPoolForTopic(0, "0", 0, "123", true, producerConfigTemplate, true);
for (int i = 0; i < 2; i++)
{
new Thread(obj =>
{
var sw = Stopwatch.StartNew();
mkafka2(kafkaSimpleManage, 1500000);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}).Start();
}
Console.ReadLine();
}
private static void mkafka2(KafkaSimpleManager<byte[], Message> kafkaSimpleManage, int count)
{
var key = Encoding.UTF8.GetBytes("12asdfasdfasdfasdf");
var value = new byte[1 * 1024];
var rdm = new Random();
Parallel.For(0, value.Length, i =>
{
value[i] = (byte)rdm.Next(0, 127);
});
for (int i = 0; i < count; i++)
{
var producer = kafkaSimpleManage.GetProducerOfPartition("123", -1, true);
producer.Send(new ProducerData<byte[], Message>("123", key, new Message(value)));
}
}