Пример #1
0
 static async Task WriteResultsToFile(BenchmarkEntry benchmarkEntry, string filename)
 {
     Console.WriteLine($"Writing Bencmark Results to file {Path.GetFullPath(filename)}");
     using (StreamWriter file = File.CreateText(filename))
     {
         JsonSerializer serializer = new JsonSerializer();
         //serialize object directly into file stream
         serializer.Serialize(file, benchmarkEntry);
         file.Close();
     }
 }
Пример #2
0
        static void Main(string[] args)
        {
            CommandLine.Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(opts =>
            {
                string IoTHub = opts.IotHubConnectionString.Split(';')[0].Split('=')[1];
                Console.WriteLine("Creating Devices");
                CreateDevices(opts.DeviceCount, opts.IotHubConnectionString).Wait();
                // Messagesize
                if (opts.MessageSize > 262143)
                {
                    Console.WriteLine($"Setting MessageSize to maximum of {MAXMESSAGESIZE}");
                    MessageSize = MAXMESSAGESIZE;
                }
                else
                {
                    MessageSize = opts.MessageSize;
                }
                // Transporttype
                switch (opts.TransportType)
                {
                case "MQTT":
                    TransportType = Microsoft.Azure.Devices.Client.TransportType.Mqtt;
                    break;

                case "AMQP":
                    TransportType = Microsoft.Azure.Devices.Client.TransportType.Amqp;
                    break;

                case "HTTP":
                    TransportType = Microsoft.Azure.Devices.Client.TransportType.Http1;
                    break;

                default:
                    TransportType = Microsoft.Azure.Devices.Client.TransportType.Mqtt;
                    break;
                }
                Console.WriteLine($"Starting Devices: DeviceCount: {opts.DeviceCount} MaxMessages:{opts.MaxMessages} MessageSize:{MessageSize} TransportType:{TransportType}");
                DateTime startTime            = DateTime.Now;
                BenchmarkEntry benchmarkEntry = new BenchmarkEntry()
                {
                    StartTime        = startTime,
                    DeviceCount      = opts.DeviceCount,
                    MaxMessages      = opts.MaxMessages,
                    MessageSize      = MessageSize,
                    DeviceBenchmarks = new List <DeviceBenchmarkEntry>(),
                    TransportType    = opts.TransportType
                };
                Task[] tasks = new Task[opts.DeviceCount];
                for (int deviceNumber = 1; deviceNumber <= opts.DeviceCount; deviceNumber++)
                {
                    tasks[deviceNumber - 1] = StartClient(IoTHub, IoTDevicePrefix, deviceNumber, CommonKey, opts.MaxMessages, MessageSize, TransportType).ContinueWith(task =>
                    {
                        benchmarkEntry.DeviceBenchmarks.Add(task.Result);
                    });
                }
                Task.WaitAll(tasks);
                DateTime endTime                = DateTime.Now;
                double messagesPerSecond        = opts.DeviceCount * opts.MaxMessages / (endTime - startTime).TotalSeconds;
                benchmarkEntry.EndTime          = endTime;
                benchmarkEntry.MessagePerSecond = messagesPerSecond;
                WriteResultsToFile(benchmarkEntry, opts.BenchmarkFileNamePath).Wait();

                Console.WriteLine("All Messages are sent");
                Console.WriteLine($"Total Clients: {opts.DeviceCount}");
                Console.WriteLine($"Message Size: {opts.MessageSize}");
                Console.WriteLine($"Total Messages Sent: {opts.DeviceCount * opts.MaxMessages}");
                Console.WriteLine($"Total Execution Time: {(endTime - startTime).TotalSeconds} seconds");
                Console.WriteLine($"Messages Per Second: {messagesPerSecond}");
                Thread.Sleep(7000); // Wait before starting to delete devices
                DeleteDevices(opts.DeviceCount, opts.IotHubConnectionString).Wait();
            });
        }