Exemplo n.º 1
0
        static void ProducerSendTPSTest()
        {
            int batchCount  = 100000;
            var queueClient = new KafkaProducer(commandQueue, zkConnectionString);

            var       message      = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
            var       kafkaMessage = new Kafka.Client.Messages.Message(Encoding.UTF8.GetBytes(message));
            var       data         = new Kafka.Client.Producers.ProducerData <string, Kafka.Client.Messages.Message>(commandQueue, message, kafkaMessage);
            double    totalSendRt  = 0;
            Stopwatch watch        = new Stopwatch();

            watch.Start();
            for (int i = 0; i < batchCount; i++)
            {
                try
                {
                    var start = DateTime.Now;
                    queueClient.Send(data);
                    totalSendRt += (DateTime.Now - start).TotalMilliseconds;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.GetBaseException().Message);
                }
            }
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine($"cost: {elapsedMs}  tps: {batchCount * 1000 / elapsedMs} rt: {totalSendRt / (double)batchCount}");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public void ProducerTest()
        {
            var queueClient = new KafkaProducer(commandQueue, _zkConnection);

            var message      = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
            var kafkaMessage = new Kafka.Client.Messages.Message(Encoding.UTF8.GetBytes(message));
            var data         = new Kafka.Client.Producers.ProducerData <string, Kafka.Client.Messages.Message>(commandQueue, message, kafkaMessage);

            queueClient.Send(data);
            Console.WriteLine($"send message: {message}");
            queueClient.Stop();
            ZookeeperConsumerConnector.zkClientStatic.Dispose();
        }
Exemplo n.º 3
0
        static void GroupConsuemrTest()
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var consumerTasks           = new List <Task>();

            for (int i = 0; i < 1; i++)
            {
                consumerTasks.Add(CreateConsumerTask(commandQueue, i.ToString(), cancellationTokenSource));
            }


            var queueClient = new KafkaProducer(commandQueue, zkConnectionString);

            while (true)
            {
                var message = Console.ReadLine();
                if (message.Equals("q"))
                {
                    cancellationTokenSource.Cancel();
                    Task.WaitAll(consumerTasks.ToArray());
                    break;
                }
                message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
                var kafkaMessage = new Kafka.Client.Messages.Message(Encoding.UTF8.GetBytes(message));
                var data         = new Kafka.Client.Producers.ProducerData <string, Kafka.Client.Messages.Message>(commandQueue, message, kafkaMessage);
                try
                {
                    queueClient.Send(data);
                    Console.WriteLine($"send message: {message}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.GetBaseException().Message);
                }
            }
        }