デバイスから、Azure IoT Hubへの送信時間を測定しました。
前提
- デバイスは、Surface Pro 2。
- デバイスのOSは、Windows 10 1511。
測定方法
Console Application(.NET4.6)に、nugetでMicrosoft.Azure.Devices.Clientを追加して、DeviceClient.SendEventAsyncメソッドで送信しました。
CloseAsyncすると、SendEventAsyncでフリーズしたことから、DeviceClientインスタンスが再利用できないと判断。毎回、DeviceClientをインスタンス化することにしました。
DeviceExplorerで見ていると、OpenAsyncではなく初回のSendEventAsyncでConnectしているようなので1回目の送信時間から2回目の送信時間を引いた値を、接続にかかる時間としました。
コード
class Program
{
private const string DeviceConnectionString = "xxx";
static void Main(string[] args)
{
MyTask().Wait();
Console.WriteLine("Hit any key.");
Console.ReadKey();
}
static async Task MyTask()
{
for (int count = 0; count < 30; count++)
{
var sw0 = new System.Diagnostics.Stopwatch();
var sw1 = new System.Diagnostics.Stopwatch();
Message eventMessage1st = new Message(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
Message eventMessage2nd = new Message(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
using (DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt))
{
sw0.Restart();
await deviceClient.SendEventAsync(eventMessage1st);
sw0.Stop();
sw1.Restart();
await deviceClient.SendEventAsync(eventMessage2nd);
sw1.Stop();
await deviceClient.CloseAsync();
}
Console.WriteLine($"{count},{sw0.ElapsedMilliseconds},{sw1.ElapsedMilliseconds}");
await Task.Delay(1000);
}
}
}