Пример #1
0
        private async void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            stopFlag = false;
            buttonStart.IsEnabled = false;
            listOfDevices         = null;

            // Save the setting
            Properties.Settings.Default["IoTHubConnectionString"]       = textBoxIotHub.Text;
            Properties.Settings.Default["QueueStorageConnectionString"] = textBoxQStorageConnString.Text;
            Properties.Settings.Default["DeviceList"]     = textBoxDevices.Text;
            Properties.Settings.Default["NumOfLoops"]     = textBoxLoops.Text;
            Properties.Settings.Default["TexBoxJson"]     = textBoxJson.Text;
            Properties.Settings.Default["ComboBoxP1"]     = comboBoxP1.Text;
            Properties.Settings.Default["ComboBoxP2"]     = comboBoxP2.Text;
            Properties.Settings.Default["ComboBoxP3"]     = comboBoxP3.Text;
            Properties.Settings.Default["LabelP1"]        = labelP1.Content.ToString();
            Properties.Settings.Default["LabelP2"]        = labelP2.Content.ToString();
            Properties.Settings.Default["LabelP3"]        = labelP3.Content.ToString();
            Properties.Settings.Default["TextBoxIncreP1"] = textBoxIncreP1.Text;
            Properties.Settings.Default["TextBoxIncreP2"] = textBoxIncreP2.Text;
            Properties.Settings.Default["TextBoxIncreP3"] = textBoxIncreP3.Text;
            Properties.Settings.Default["TextBoxStartP1"] = textBoxStartP1.Text;
            Properties.Settings.Default["TextBoxStartP2"] = textBoxStartP2.Text;
            Properties.Settings.Default["TextBoxStartP3"] = textBoxStartP3.Text;
            Properties.Settings.Default["TextBoxEndP1"]   = textBoxEndP1.Text;
            Properties.Settings.Default["TextBoxEndP2"]   = textBoxEndP2.Text;
            Properties.Settings.Default["TextBoxEndP3"]   = textBoxEndP3.Text;
            Properties.Settings.Default.Save();

            string[] deviceNames = textBoxDevices.Text.Split(',');
            listOfDeviceNames = new List <string>();
            threadCount       = listOfDeviceNames.Count;

            foreach (string deviceName in deviceNames)
            {
                listOfDeviceNames.Add(deviceName);
            }

            // Get Deivice List
            DeviceInfo devInfo = new DeviceInfo(textBoxIotHub.Text, listOfDeviceNames);

            listOfDevices = await devInfo.GetDevices();

            // Create ThreadInput
            ThreadInput ti = new ThreadInput();

            ti.ThreadCount        = listOfDeviceNames.Count;
            ti.Message            = textBoxJson.Text;
            ti.ParamDateTimeId    = labelDateTime.Content.ToString();
            ti.ParamDateTimeValue = textBoxDateTime.Text;

            List <ReplaceOperator> replaceOperatorList = new List <ReplaceOperator>();
            // param 1
            ReplaceOperator ope = new ReplaceOperator();

            ope.Id         = labelParam1.Content.ToString();
            ope.Mode       = comboBoxP1.Text;
            ope.Increment  = textBoxIncreP1.Text;
            ope.StartValue = textBoxStartP1.Text;
            ope.EndValue   = textBoxEndP1.Text;
            replaceOperatorList.Add(ope);
            // param 2
            ope            = new ReplaceOperator();
            ope.Id         = labelParam2.Content.ToString();
            ope.Mode       = comboBoxP2.Text;
            ope.Increment  = textBoxIncreP2.Text;
            ope.StartValue = textBoxStartP2.Text;
            ope.EndValue   = textBoxEndP2.Text;
            replaceOperatorList.Add(ope);
            // param 3
            ope            = new ReplaceOperator();
            ope.Id         = labelParam3.Content.ToString();
            ope.Mode       = comboBoxP3.Text;
            ope.Increment  = textBoxIncreP3.Text;
            ope.StartValue = textBoxStartP3.Text;
            ope.EndValue   = textBoxEndP3.Text;
            replaceOperatorList.Add(ope);
            ti.ReplaceOperatorList = replaceOperatorList;

            var    builder                = Microsoft.Azure.Devices.IotHubConnectionStringBuilder.Create(textBoxIotHub.Text);
            string iotHubHostName         = builder.HostName;
            string queueStorageConnString = textBoxQStorageConnString.Text;

            // Launch multi-threads which send and receive message
            dictionaryOfThreadResult = new Dictionary <string, ThreadResult>();
            int threadNum = 0;

            foreach (var device in listOfDevices)
            {
                ti.ThreadNo = threadNum;
                CommunicateAsync(ti, iotHubHostName, device.Id, device.PrimaryKey, queueStorageConnString);
                ++threadNum;
            }

            resultWindow = new ResultWindow();
            resultWindow.Show();
        }
Пример #2
0
        private async void CommunicateAsync(ThreadInput threadInput, string hostName,
                                            string deviceId, string deviceKey, string queueStorageConnString)
        {
            try
            {
                string loopsStr = textBoxLoops.Text;
                int    loops    = 0;
                if (!int.TryParse(loopsStr, out loops))
                {
                    loops = -1;
                }
                int loopCounter = 0;

                int    increment       = 0;
                int    startValue      = 0;
                int    endValue        = 0;
                string templateMessage = threadInput.Message;
                string message;
                List <ValueGenerator> valueGenList = new List <ValueGenerator>();

                foreach (var ope in threadInput.ReplaceOperatorList)
                {
                    if (ope.Mode.ToLower() == "random" || ope.Mode.ToLower() == "addition")
                    {
                        increment = int.Parse(ope.Increment);
                        if (!int.TryParse(ope.StartValue, out startValue))
                        {
                            startValue = 0;
                        }
                        if (!int.TryParse(ope.EndValue, out endValue))
                        {
                            endValue = 0;
                        }
                        if (ope.Mode.ToLower() == "addition")
                        {
                            int range = (endValue - startValue + 1) / threadInput.ThreadCount;
                            startValue = startValue + range * threadInput.ThreadNo;
                            endValue   = startValue + range - 1;
                        }
                        var valueGen = new ValueGenerator(ope.Id, ope.Mode, increment, startValue, endValue);
                        valueGenList.Add(valueGen);
                    }
                }

                using (var deviceClient = DeviceClient.Create(hostName,
                                                              new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey)))
                {
                    while (true)
                    {
                        message = templateMessage;

                        if (stopFlag)
                        {
                            lock (lockObject)
                            {
                                ThreadResult trStop = new ThreadResult();
                                trStop.DeviceId                    = deviceId;
                                trStop.ThroughputPer60Sec          = 0;
                                trStop.IsEnabled                   = false;
                                dictionaryOfThreadResult[deviceId] = trStop;
                                --threadCount;
                            }
                            return;
                        }

                        if (threadInput.ParamDateTimeValue != string.Empty)
                        {
                            if (message.Contains(threadInput.ParamDateTimeId))
                            {
                                if (threadInput.ParamDateTimeValue.ToLower() == "datetime.now")
                                {
                                    message = message.Replace(threadInput.ParamDateTimeId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                }
                                else
                                {
                                    message = message.Replace(threadInput.ParamDateTimeId, threadInput.ParamDateTimeValue);
                                }
                            }
                        }

                        // Replace $$xxx$$ parameter
                        int size = valueGenList.Count;
                        for (int i = 0; i < size; i++)
                        {
                            message = valueGenList[i].ReplaceValueInMessage(message);
                        }

                        var      msg = new Microsoft.Azure.Devices.Client.Message(Encoding.UTF8.GetBytes(message));
                        DateTime dt1 = DateTime.Now;

                        // Send msg to IoT Hub
                        await deviceClient.SendEventAsync(msg);

                        // Receive msg
                        int loopCount    = 0;
                        int maxLoopCount = 100;
                        if (queueStorageConnString == null || queueStorageConnString == string.Empty)
                        {
                            // ------------------------------------
                            // Receive msg from IoT Hub
                            // ------------------------------------
                            while (true)
                            {
                                Microsoft.Azure.Devices.Client.Message receivedMessage = await deviceClient.ReceiveAsync();

                                if (receivedMessage == null)
                                {
                                    ++loopCount;
                                    if (loopCount >= maxLoopCount)
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    string messageContent = Encoding.UTF8.GetString(receivedMessage.GetBytes());
                                    await deviceClient.CompleteAsync(receivedMessage);

                                    break;
                                }
                            }
                        }
                        else
                        {
                            // ------------------------------------
                            // Receive msg from Queue Storage
                            // ------------------------------------

                            // If communicate with the deviceId at first, Cloud Robotics FX creates the queue.
                            // So need to wait for a second
                            if (loopCounter == 0)
                            {
                                Thread.Sleep(1000);
                            }

                            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(queueStorageConnString);
                            CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                            CloudQueue          queue          = queueClient.GetQueueReference(deviceId);
                            while (true)
                            {
                                CloudQueueMessage queueMessage = await queue.GetMessageAsync();

                                if (queueMessage == null)
                                {
                                    ++loopCount;
                                    if (loopCount >= maxLoopCount)
                                    {
                                        break;
                                    }
                                    Thread.Sleep(50);
                                }
                                else
                                {
                                    string receivedMessage = queueMessage.AsString;
                                    await queue.DeleteMessageAsync(queueMessage);

                                    break;
                                }
                            }
                        }

                        DateTime dt2 = DateTime.Now;
                        TimeSpan ts  = dt2 - dt1;

                        int    milliSec = ts.Seconds * 1000 + ts.Milliseconds;
                        double throuput = 60 * 1000 / milliSec;
                        if (loopCount >= maxLoopCount)
                        {
                            throuput = 0;
                        }
                        ThreadResult tr = new ThreadResult();
                        tr.DeviceId           = deviceId;
                        tr.ThroughputPer60Sec = (int)Math.Round(throuput, 0);
                        tr.UpdateTime         = dt2;
                        tr.IsEnabled          = true;
                        tr.ExceptionMessage   = string.Empty;
                        lock (lockObject)
                        {
                            dictionaryOfThreadResult[deviceId] = tr;
                        }

                        ++loopCounter;
                        if (loops != -1 && loopCounter >= loops)
                        {
                            stopLoadTest();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ThreadResult tr = new ThreadResult();
                tr.DeviceId           = deviceId;
                tr.ThroughputPer60Sec = 0;
                tr.UpdateTime         = DateTime.Now;
                tr.IsEnabled          = false;
                tr.ExceptionMessage   = ex.ToString();
                lock (lockObject)
                {
                    dictionaryOfThreadResult[deviceId] = tr;
                }
                throw;
            }
        }