/// <summary> /// Constructor /// </summary> /// <param name="timeoutMilliSeconds">Timeout milli seconds for send and receive. Timeout of connect is not effected.</param> /// <param name="keepAliveNoticeIntervalSeconds"></param> /// <param name="logger"></param> public MatchMakerClient(int timeoutMilliSeconds = 10000, int keepAliveNoticeIntervalSeconds = 30, ILogger logger = null) { if (logger == null) { logger = StreamLogger.CreateStandardOutputLogger(); } TimeoutMilliSeconds = timeoutMilliSeconds; Logger = logger; PortMappingCreator = new NatPortMappingCreator(logger); keepAliveSenderNotificator = new KeepAliveSenderNotificator(this, keepAliveNoticeIntervalSeconds); }
protected override async Task Execute(MatchMakerClient sharedClient, TOptions options, CancellationToken cancellationToken) { var testClientList = new List <MatchMakerClient>(); try { // create clients for stress test var nullLogger = StreamLogger.CreateNullLogger(); for (var i = 0; i < options.ClientCount; ++i) { testClientList.Add(new MatchMakerClient(logger: nullLogger)); } var benchmarkResults = new ConcurrentDictionary <string, ConcurrentQueue <(int, double)> >(); // create tasks for stress test var taskList = testClientList .Select(client => Task.Run(async() => await StressTest(client, benchmarkResults, options, cancellationToken))) .ToList(); // monitor stress test OutputStream.WriteLine("Start stress test."); while (taskList.Any(task => !task.IsCompleted)) { await Task.Delay(1000); var lineList = new List <string>(); foreach (var pair in benchmarkResults) { if (pair.Value.IsEmpty) { continue; } var queueCount = pair.Value.Count; var resultList = new List <double>(); var resultCount = 0; for (var i = 0; i < queueCount; ++i) { if (!pair.Value.TryDequeue(out var result)) { break; } var(count, averageTime) = result; resultCount += count; resultList.Add(averageTime); } lineList.Add( $"{pair.Key}: response={resultList.Average():f03}ms, operation={resultCount}/s"); } if (lineList.Count == 0) { continue; } OutputStream.WriteLine("--------Benchmark Results--------"); lineList.ForEach(Console.WriteLine); OutputStream.WriteLine("---------------------------------"); } OutputStream.WriteLine("Stress test is finished."); } finally { testClientList.ForEach(c => c.Dispose()); } }
private static void Main(string[] args) { var versionInfo = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location); Console.WriteLine($"{versionInfo.ProductName} v{versionInfo.ProductVersion}"); var parsedResult = CommandLine.Parser.Default.ParseArguments <Options>(args); if (parsedResult.Tag != CommandLine.ParserResultType.Parsed) { var helpText = HelpText.AutoBuild(parsedResult); Console.WriteLine("Failed to parse options."); Console.WriteLine(helpText); return; } var options = ((CommandLine.Parsed <Options>)parsedResult).Value; var commandProcessor = CommandProcessorFactory.Create(); try { var logger = StreamLogger.CreateStandardOutputLogger(LogLevel.Debug); using var client = new MatchMakerClient(logger: logger); Console.CancelKeyPress += OnKeyBoardInterrupted; if (!string.IsNullOrEmpty(options.CommandAndOptions)) { var items = options.CommandAndOptions.Split(' '); if (!CommandProcessor.TryParseCommand(items[0], out var command)) { Console.WriteLine("Command is invalid."); } else { var commandOptions = items.Skip(1); ExecuteCommand(commandProcessor, command, client, commandOptions); } } else { while (!isFinishProgram) { Console.WriteLine("Input command."); commandProcessor.DisplayCommandList(); var input = Console.ReadLine(); if (input == null) { Console.WriteLine("Command is invalid."); continue; } var items = input.Split(' '); if (!CommandProcessor.TryParseCommand(items[0], out var command)) { Console.WriteLine("Command is invalid."); continue; } var commandOptions = items.Skip(1); ExecuteCommand(commandProcessor, command, client, commandOptions); } } } finally { NatPortMappingCreator.ReleaseCreatedPortMappings(); } }