Last active
February 11, 2025 02:31
-
-
Save azass/dc18061ab47e3407e424ced582773407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
class SocketLoadTest | |
{ | |
private const int ClientCount = 300; | |
private const string MulticastAddress = "239.0.0.1"; | |
private const int MulticastPort = 5000; | |
private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(ClientCount); | |
private static readonly IPAddress McastAddress = IPAddress.Parse(MulticastAddress); | |
private static readonly IPEndPoint EndPoint = new IPEndPoint(McastAddress, MulticastPort); | |
static async Task Main() | |
{ | |
// 事前に EntityStatePdu のリストを作成 | |
string csvFilePath = "your_csv_file.csv"; // ここにCSVファイルのパスを設定 | |
SortedDictionary<int, List<EntityStatePdu>> sortedDictionary = new SortedDictionary<int, List<EntityStatePdu>>(); | |
using (StreamReader reader = new StreamReader(csvFilePath)) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
string[] values = line.Split(','); | |
if (int.TryParse(values[0], out double key)) | |
{ | |
// 辞書にキーが存在するかを調べ、存在する場合は値をlist変数に格納 | |
if (!sortedDictionary.TryGetValue(key, out List<EntityStatePdu> list)) | |
{ | |
list = new List<EntityStatePdu>(); | |
sortedDictionary.Add(key, list); | |
} | |
// CSVの2番目以降を配列として | |
list.Add(createEntityStatePdu(values.Skip(1).ToArray())); | |
} | |
else | |
{ | |
Console.WriteLine($"Error parsing line: {line}"); | |
} | |
} | |
} | |
int lasttime = 0; | |
foreach (var kvp in sortedDictionary) | |
{ | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
var List<EntityStatePdu> EntityPduList = kvp.Value; | |
var tasks = EntityPduList.Select(async espdu => | |
{ | |
Task.Run(() => RunClient(espdu)); | |
}).ToArray(); | |
await Task.WhenAll(tasks); | |
stopwatch.Stop(); | |
var elapsedMilliseconds = stopwatch.ElapsedMilliseconds; | |
int Interval = kvp.Key - lasttime; | |
lasttime = kvp.Key; | |
// Intervalから経過時間を差し引く | |
var adjustedInterval = Interval - elapsedMilliseconds; | |
// 0以下の場合は0にする | |
adjustedInterval = Math.Max(adjustedInterval, 0); | |
await Task.Delay(adjustedInterval); | |
} | |
} | |
private static async Task RunClient(EntityStatePdu espdu) | |
{ | |
await Semaphore.WaitAsync(); | |
try | |
{ | |
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) | |
{ | |
var dos = new DataOutputStream(Endian.Big); | |
espdu.MarshalAutoLengthSet(dos); | |
byte[] data = dos.ConvertToBytes(); | |
await Task.Run(() => socket.SendTo(data, EndPoint)); | |
Console.WriteLine($"Client {espdu.EntityID} sent EntityStatePdu to {MulticastAddress}:{MulticastPort}"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Client {espdu.EntityID} error: {ex.Message}"); | |
} | |
finally | |
{ | |
Semaphore.Release(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment