예제 #1
0
        public static MyDoc Generate()
        {
            // randomize the data
            var locId     = random.Next(1, 700);
            var partition = random.Next(1, 100);
            var year      = random.Next(2000, 2020);
            var month     = random.Next(1, 12);
            var day       = random.Next(1, 28);
            var options   = random.Next(1, 3);

            // generate the document
            var myDoc = new MyDoc()
            {
                id        = System.Guid.NewGuid().ToString(),
                key       = $"{locId.ToString()}-{partition.ToString().PadLeft(3, '0')}",
                date      = $"{year.ToString()}-{month.ToString().PadLeft(2, '0')}-{day.ToString().PadLeft(2, '0')}",
                locId     = locId.ToString(),
                attribA   = "",
                attribB   = "MIGRATOR",
                attribC   = DateTime.UtcNow.ToString("o"),
                attribD   = "",
                attribE   = "",
                isDeleted = false,
                attribF   = "",
                attribG   = "",
                options   = new List <Option>()
            };

            // generate waves
            for (int i = 0; i < options; i++)
            {
                myDoc.options.Add(new Option()
                {
                    optAttribA = random.Next(-10, 10).ToString(),
                    optAttribB = random.Next(1, 10).ToString(),
                    optAttribC = "0",
                    optAttribD = "0",
                    optAttribE = $"OPTION {i.ToString()}",
                    optAttribF = i.ToString(),
                    optAttribG = "",
                    optAttribH = "0"
                });
            }

            return(myDoc);
        }
예제 #2
0
        static async Task Main(string[] args)
        {
            // get variables
            DotEnv.Config(false);
            var connstring      = System.Environment.GetEnvironmentVariable("EVENTHUB_CONNSTRING");
            var name            = System.Environment.GetEnvironmentVariable("EVENTHUB_NAME");
            var batchSizeString = System.Environment.GetEnvironmentVariable("EVENTHUB_BATCHSIZE");

            if (!int.TryParse(batchSizeString, out int batchSize))
            {
                batchSize = 100;
            }
            var countString = System.Environment.GetEnvironmentVariable("EVENTHUB_COUNT");

            if (!int.TryParse(countString, out int count))
            {
                count = 100;
            }

            // create the stopwatch and counters
            int success = 0;
            int failure = 0;
            var watch   = new Stopwatch();

            // start the timer for reporting progress
            Timer timer = null;

            timer = new Timer((state) =>
            {
                // report progress
                Console.WriteLine($"{success} successes, {failure} failures, after {watch.Elapsed.TotalSeconds} seconds...");

                // set timer to run again
                timer.Change(5000, Timeout.Infinite);
            }, null, 5000, Timeout.Infinite);

            // create producer client
            await using (var producerClient = new EventHubProducerClient(connstring, name))
            {
                watch.Start();

                // process count
                for (int j = 0; j < count; j++)
                {
                    // add a batch
                    using (EventDataBatch eventBatch = await producerClient.CreateBatchAsync())
                    {
                        for (int i = 0; i < batchSize; i++)
                        {
                            var msg   = MyDoc.Generate();
                            var bytes = JsonSerializer.SerializeToUtf8Bytes(msg);
                            if (eventBatch.TryAdd(new EventData(bytes)))
                            {
                                Interlocked.Increment(ref success);
                            }
                            else
                            {
                                Console.WriteLine($"failed {i} to add {msg.id} of {msg.locId}...");
                                Interlocked.Increment(ref failure);
                            }
                        }
                        await producerClient.SendAsync(eventBatch);
                    }
                }
            }

            // wait forever
            Console.WriteLine($"{success} successes, {failure} failures, after {watch.Elapsed.TotalSeconds} seconds, done.");
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            Process.GetCurrentProcess().WaitForExit();
        }