private void RunOneThread(object parameter)
        {
            PerfTestThreadParameter    p        = (PerfTestThreadParameter)parameter;
            Producer <byte[], Message> producer = ProducePerfTestKafkaSimpleManagerWrapper.Instance.GetProducerMustReturn(p.Topic, p.PartitionId, p.ThreadId, produceOption.SleepInMSWhenException, p.RandomReturnIfNotExist);

            Logger.InfoFormat("==========Thread:{0} PartitionID:{1} RandomReturnIfNotExist:{2}  AssignedProducer:{3}==============", p.ThreadId, p.PartitionId, p.RandomReturnIfNotExist, producer.Config.ForceToPartition);

            DateTime startTime = DateTime.UtcNow;

            while (true)
            {
                p.RequestIndex++;
                try
                {
                    p.StopWatchForSuccessRequest.Restart();
                    producer.Send(listOfDataNeedSendInOneBatch);
                    p.StopWatchForSuccessRequest.Stop();
                    p.SuccRecordsTimeInms += p.StopWatchForSuccessRequest.ElapsedMilliseconds;

                    p.RequestSucc++;
                    p.SentSuccBytes += produceOption.MessageCountPerBatch * produceOption.MessageSize;
                }
                catch (Exception e)
                {
                    p.RequestFail++;
                    Logger.ErrorFormat("Exception:{0}", e.ToString());
                    producer = ProducePerfTestKafkaSimpleManagerWrapper.Instance.GetProducerMustReturn(p.Topic, p.PartitionId, p.ThreadId, produceOption.SleepInMSWhenException, p.RandomReturnIfNotExist);
                    Logger.InfoFormat("==========Thread:{0} PartitionID:{1} RandomReturnIfNotExist:{2}  AssignedProducer:{3}==============", p.ThreadId, p.PartitionId, p.RandomReturnIfNotExist, producer.Config.ForceToPartition);
                }

                if (p.ThreadId == 0 && p.RequestIndex % 100 == 0)
                {
                    Statistics();
                }
                if (produceOption.BatchCount > 0 && p.RequestIndex >= produceOption.BatchCount)
                {
                    break;
                }

                if (p.SpeedConstrolMBPerSecond > 0.0)
                {
                    while (true)
                    {
                        double expectedSuccBytes = (DateTime.UtcNow - startTime).TotalSeconds * p.SpeedConstrolMBPerSecond * 1024 * 1024;
                        if (p.SentSuccBytes > expectedSuccBytes)
                        {
                            Thread.Sleep(20);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            p.EventOfFinish.Set();
        }
        internal void Run(JavaEventServerPerfTestHelperOptions evetServerPerfTestOptions)
        {
            produceOption  = evetServerPerfTestOptions;
            perfTestOption = evetServerPerfTestOptions;
            url            = evetServerPerfTestOptions.EventServerFullAddress + "/eventserver?type=" + evetServerPerfTestOptions.Topic + "&sendtype=" + evetServerPerfTestOptions.SendType;
            if (url.ToLowerInvariant().StartsWith("https:"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
            }

            byte[] bKey = null;
            byte[] bVal = null;
            for (int i = 0; i < evetServerPerfTestOptions.MessageCountPerBatch; i++)
            {
                String key = RandomString(56);
                bKey = System.Text.Encoding.UTF8.GetBytes(key);
                String val = RandomString(evetServerPerfTestOptions.MessageSize);
                bVal = System.Text.Encoding.UTF8.GetBytes(val);
                messages.Add(new Message(bKey, bVal));
            }

            bufferLenth = evetServerPerfTestOptions.MessageCountPerBatch * (8 + bKey.Length + bVal.Length);

            Thread[]         threads = new Thread[evetServerPerfTestOptions.ThreadCount];
            AutoResetEvent[] autos   = new AutoResetEvent[evetServerPerfTestOptions.ThreadCount];
            threadPareamaters = new PerfTestThreadParameter[evetServerPerfTestOptions.ThreadCount];

            Logger.InfoFormat("start send {0} ", DateTime.Now);

            stopWatch.Restart();
            for (int i = 0; i < evetServerPerfTestOptions.ThreadCount; i++)
            {
                AutoResetEvent          Auto = new AutoResetEvent(false);
                PerfTestThreadParameter p    = new PerfTestThreadParameter();
                p.ThreadId = i;
                p.SpeedConstrolMBPerSecond = (evetServerPerfTestOptions.SpeedConstrolMBPerSecond * 1.0) / evetServerPerfTestOptions.ThreadCount;
                p.EventOfFinish            = Auto;
                Thread t = new Thread(new ParameterizedThreadStart(RunOneThread));
                t.Start(p);
                autos[i]             = Auto;
                threadPareamaters[i] = p;
            }

            WaitHandle.WaitAll(autos);
            stopWatch.Stop();
            Statistics();
        }
        internal void Run(JavaEventServerPerfTestHelperOptions evetServerPerfTestOptions)
        {
            produceOption = evetServerPerfTestOptions;
            perfTestOption = evetServerPerfTestOptions;
            url = evetServerPerfTestOptions.EventServerFullAddress + "/eventserver?type=" + evetServerPerfTestOptions.Topic + "&sendtype=" + evetServerPerfTestOptions.SendType;
            if (url.ToLowerInvariant().StartsWith("https:"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
            }

            byte[] bKey = null;
            byte[] bVal = null;
            for (int i = 0; i < evetServerPerfTestOptions.MessageCountPerBatch; i++)
            {
                String key = RandomString(56);
                bKey = System.Text.Encoding.UTF8.GetBytes(key);
                String val = RandomString(evetServerPerfTestOptions.MessageSize);
                bVal = System.Text.Encoding.UTF8.GetBytes(val);
                messages.Add(new Message(bKey, bVal));
            }

            bufferLenth = evetServerPerfTestOptions.MessageCountPerBatch * (8 + bKey.Length + bVal.Length);

            Thread[] threads = new Thread[evetServerPerfTestOptions.ThreadCount];
            AutoResetEvent[] autos = new AutoResetEvent[evetServerPerfTestOptions.ThreadCount];
            threadPareamaters = new PerfTestThreadParameter[evetServerPerfTestOptions.ThreadCount];

            Logger.InfoFormat("start send {0} ", DateTime.Now);

            stopWatch.Restart();
            for (int i = 0; i < evetServerPerfTestOptions.ThreadCount; i++)
            {
                AutoResetEvent Auto = new AutoResetEvent(false);
                PerfTestThreadParameter p = new PerfTestThreadParameter();
                p.ThreadId = i;
                p.SpeedConstrolMBPerSecond = (evetServerPerfTestOptions.SpeedConstrolMBPerSecond * 1.0) / evetServerPerfTestOptions.ThreadCount;
                p.EventOfFinish = Auto;
                Thread t = new Thread(new ParameterizedThreadStart(RunOneThread));
                t.Start(p);
                autos[i] = Auto;
                threadPareamaters[i] = p;
            }

            WaitHandle.WaitAll(autos);
            stopWatch.Stop();
            Statistics();
        }
        internal void Run(ProducePerfTestHelperOption option)
        {
            produceOption = option;
            ProducePerfTestKafkaSimpleManagerWrapper.produceOptions = option;

            Initializer(option);
            Thread[]         threads = new Thread[option.ThreadCount];
            AutoResetEvent[] autos   = new AutoResetEvent[option.ThreadCount];
            threadPareamaters = new PerfTestThreadParameter[option.ThreadCount];

            Logger.InfoFormat("start send {0} ", DateTime.Now);
            stopWatch.Restart();
            //[('p',   -1  random partition.      -2  by default or customized partitioner class. (you need specify PartitionerClass by -l)   >=0 to specific partition.
            for (int i = 0; i < option.ThreadCount; i++)
            {
                AutoResetEvent          Auto = new AutoResetEvent(false);
                PerfTestThreadParameter p    = new PerfTestThreadParameter();
                p.ThreadId = i;
                p.SpeedConstrolMBPerSecond = (option.SpeedConstrolMBPerSecond * 1.0) / option.ThreadCount;
                p.EventOfFinish            = Auto;
                if (produceOption.PartitionId < 0)
                {
                    p.RandomReturnIfNotExist = true;
                }
                else
                {
                    p.RandomReturnIfNotExist = false;
                }
                p.PartitionId = produceOption.PartitionId;
                p.Topic       = produceOption.Topic;

                Logger.InfoFormat("Thread:{0} PartitionID:{1} RandomReturnIfNotExist:{2} ", p.ThreadId, p.PartitionId, p.RandomReturnIfNotExist);
                Thread t = new Thread(new ParameterizedThreadStart(RunOneThread));
                t.Start(p);
                autos[i]             = Auto;
                threadPareamaters[i] = p;
            }

            WaitHandle.WaitAll(autos);
            stopWatch.Stop();
            Statistics();
        }
        internal void Run(ProducePerfTestHelperOption option)
        {
            produceOption = option;
            ProducePerfTestKafkaSimpleManagerWrapper.produceOptions = option;

            Initializer(option);
            Thread[] threads = new Thread[option.ThreadCount];
            AutoResetEvent[] autos = new AutoResetEvent[option.ThreadCount];
            threadPareamaters = new PerfTestThreadParameter[option.ThreadCount];

            Logger.InfoFormat("start send {0} ", DateTime.Now);
            stopWatch.Restart();
            //[('p',   -1  random partition.      -2  by default or customized partitioner class. (you need specify PartitionerClass by -l)   >=0 to specific partition.
            for (int i = 0; i < option.ThreadCount; i++)
            {
                AutoResetEvent Auto = new AutoResetEvent(false);
                PerfTestThreadParameter p = new PerfTestThreadParameter();
                p.ThreadId = i;
                p.SpeedConstrolMBPerSecond = (option.SpeedConstrolMBPerSecond * 1.0) / option.ThreadCount;
                p.EventOfFinish = Auto;
                if (produceOption.PartitionId < 0)
                {
                    p.RandomReturnIfNotExist = true;
                }
                else
                    p.RandomReturnIfNotExist = false;
                p.PartitionId = produceOption.PartitionId;
                p.Topic = produceOption.Topic;

                Logger.InfoFormat("Thread:{0} PartitionID:{1} RandomReturnIfNotExist:{2} ", p.ThreadId, p.PartitionId, p.RandomReturnIfNotExist);
                Thread t = new Thread(new ParameterizedThreadStart(RunOneThread));
                t.Start(p);
                autos[i] = Auto;
                threadPareamaters[i] = p;
            }

            WaitHandle.WaitAll(autos);
            stopWatch.Stop();
            Statistics();
        }
        private void RunOneThread(object parameter)
        {
            PerfTestThreadParameter p = (PerfTestThreadParameter)parameter;
            DateTime startTime        = DateTime.UtcNow;

            while (true)
            {
                p.RequestIndex++;

                HttpWebRequest request = CreateRequest(url, messages, bufferLenth, p.ThreadId);
                p.StopWatchForSuccessRequest.Restart();
                ResponseStatus responseStatus = GetResponse(request);
                p.StopWatchForSuccessRequest.Stop();
                p.SuccRecordsTimeInms += p.StopWatchForSuccessRequest.ElapsedMilliseconds;

                if (responseStatus.success == false)
                {
                    Logger2.ErrorFormat("Thread: {0}  Request: {1}  {2} everRetryForConnectionClosed:{4} Will sleep {3}ms", p.ThreadId, p.RequestIndex, responseStatus.info, perfTestOption.SleepInMSWhenException, responseStatus.everRetryForConnectionClosed);
                    p.RequestFail++;
                    p.RequestFailAfterRetry++;
                    Thread.Sleep(perfTestOption.SleepInMSWhenException);
                }
                else
                {
                    p.RequestSucc++;
                    p.SentSuccBytes += bufferLenth;
                    if (responseStatus.everRetryForConnectionClosed)
                    {
                        p.RequestFail++;
                        Logger2.ErrorFormat("Thread: {0}  Request: {1}  {2} everRetryForConnectionClosed:{3} Got success after retry.", p.ThreadId, p.RequestIndex, responseStatus.info, responseStatus.everRetryForConnectionClosed);
                    }
                }

                if (p.ThreadId == 0 && p.RequestIndex % 200 == 0)
                {
                    Statistics();
                }
                if (perfTestOption.BatchCount > 0 && p.RequestIndex >= perfTestOption.BatchCount)
                {
                    break;
                }

                if (p.SpeedConstrolMBPerSecond > 0.0)
                {
                    while (true)
                    {
                        double expectedSuccBytes = (DateTime.UtcNow - startTime).TotalSeconds * p.SpeedConstrolMBPerSecond * 1024 * 1024;
                        if (p.SentSuccBytes > expectedSuccBytes)
                        {
                            Thread.Sleep(20);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            p.EventOfFinish.Set();
        }