1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

通信速度の取得方法【C#】

Posted at

はじめに

こんにちは。
ジョジョ好きなXRクリエイターの もふるね です。

VRChatをしてるときにラグくなることがあるので、通信速度を可視化しようと思いました。

環境
・Windows
・Visual Studio 2022

通信速度の取得

手法まとめ

通信速度を測る方法はいくつかあって

  • ダウンロード/アップロードテスト
    • インターネットから大きなファイルをダウンロードしたり、サーバーにアップロードしたりして、その時間を測定する
  • Network Performance Counters (ネットワークパフォーマンスカウンター)
    • ネットワークパフォーマンスカウンターを使用してインターフェースの送受信バイト数やパケット数などを取得する。これらのカウンターを定期的にサンプリングすることで、通信速度を計算する。

ダウンロード/アップロードテストはアップロード場所の確保など実装が大変そうなので、今回はネットワークパフォーマンスカウンターの手法を採用します。

Network Performance Counters (ネットワークパフォーマンスカウンター)

参考にしたサイト:
ネットワークインターフェイスの構成、統計情報を取得する

コードで書くとこんなふう

using System.Net.NetworkInformation;

// ネットワークインターフェイスを取得する
NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault();
Console.WriteLine("説明:{0}", networkInterface.Description);

// 送受信バイト数を取得するパフォーマンスカウンターを作成
PerformanceCounter bytesSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkInterface.Description);
PerformanceCounter bytesReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkInterface.Description);

while (true)
{
    // 現在の送信と受信のバイト数を取得
    float bytesSentPerSec = bytesSentCounter.NextValue();
    float bytesReceivedPerSec = bytesReceivedCounter.NextValue();

    // 通信速度を表示
    Console.WriteLine($"送信速度: {bytesSentPerSec / 1024} KB/s, 受信速度: {bytesReceivedPerSec / 1024} KB/s");

    // 1秒ごとに更新
    Thread.Sleep(1000);
}

自分の環境で実行してみたらこうなった。

説明:Killer E3100G 2.5 Gigabit Ethernet Controller
送信速度: 0 KB/s, 受信速度: 0 KB/s
送信速度: 6.703342 KB/s, 受信速度: 0.4097682 KB/s
送信速度: 0.149864 KB/s, 受信速度: 0.635009 KB/s
送信速度: 0.1967771 KB/s, 受信速度: 0.5123546 KB/s

youtubeを高画質でみたりすると受信速度が変化するので試してみてください。
HD画質で見たら100KB/sくらいでたよ。

振り返り

現在の送受信の速度は取れたけど、ラグいかどうかの判定の仕方はわからなかった(´・ω・`)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?