Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDeml committed May 4, 2021
2 parents 6e1a6a2 + d897a7d commit d5745af
Show file tree
Hide file tree
Showing 45 changed files with 718 additions and 283 deletions.
110 changes: 108 additions & 2 deletions NetworkBenchmarkDotNet/BenchmarkCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,29 @@ public static void PrepareBenchmark(INetworkBenchmark networkBenchmark)
Utilities.WriteVerboseLine(" Done");
}

public static void RunBenchmark(INetworkBenchmark networkBenchmark)
{
if (Config.Test == TestType.Manual)
{
RunManualMode(networkBenchmark);
return;
}

if (Config.Duration < 0)
{
RunIndefinitely(networkBenchmark);
return;
}

RunTimedBenchmark(networkBenchmark);
}

/// <summary>
/// Run the benchmark for a specific duration
/// The benchmark needs to be prepared once before running it.
/// </summary>
/// <param name="networkBenchmark">Library to run</param>
public static void RunTimedBenchmark(INetworkBenchmark networkBenchmark)
private static void RunTimedBenchmark(INetworkBenchmark networkBenchmark)
{
Utilities.WriteVerbose($"-> Run Benchmark {Config.Library}...");
StartBenchmark(networkBenchmark);
Expand All @@ -85,7 +102,7 @@ public static void RunTimedBenchmark(INetworkBenchmark networkBenchmark)
/// Runs until the user stops the process
/// </summary>
/// <param name="networkBenchmark"></param>
public static void RunIndefinitely(INetworkBenchmark networkBenchmark)
private static void RunIndefinitely(INetworkBenchmark networkBenchmark)
{
Utilities.WriteVerbose($"-> Run indefinitely {Config.Library}... (press enter to stop)");
StartBenchmark(networkBenchmark);
Expand All @@ -96,6 +113,95 @@ public static void RunIndefinitely(INetworkBenchmark networkBenchmark)
Utilities.WriteVerboseLine(" Done");
}

/// <summary>
/// Enables to enter defined commands
/// Runs until the user stops the process
/// </summary>
/// <param name="networkBenchmark"></param>
private static void RunManualMode(INetworkBenchmark networkBenchmark)
{
Utilities.WriteVerbose($"-> Run Manual Mode {Config.Library}\n");
StartBenchmark(networkBenchmark);

bool running = true;

while (running)
{
var input = Console.ReadLine();
if (input == null)
{
PrintInvalidManualInput();
continue;
}
var parts = input.ToLower().Split(' ');
if (parts.Length == 0 || parts[0].Length == 0)
{
PrintInvalidManualInput();
continue;
}

running = ProcessManualInput(networkBenchmark, parts);
}

StopBenchmark(networkBenchmark);
Utilities.WriteVerboseLine(" Done");
}

private static bool ProcessManualInput(INetworkBenchmark networkBenchmark, string[] parts)
{
var target = parts[0][0];

if (target == 'q')
{
return false;
}

if (parts.Length < 2 || !int.TryParse(parts[1], out int value))
{
PrintInvalidManualInput();
return true;
}

var transmissionMode = Config.Transmission;
if (parts.Length >= 3)
{
var transmission = parts[2][0];
if (transmission == 'r')
{
transmissionMode = TransmissionType.Reliable;
}
else if (transmission == 'u')
{
transmissionMode = TransmissionType.Unreliable;
}
}

switch (target)
{
case 'c':
var clients = networkBenchmark.Clients;
for (int i = 0; i < clients.Count; i++)
{
clients[i].SendMessages(value, transmissionMode);
}

break;
case 's':
networkBenchmark.Server.SendMessages(value, transmissionMode);
break;
default:
PrintInvalidManualInput();
break;
}

return true;
}

private static void PrintInvalidManualInput()
{
Utilities.WriteVerbose($"Invalid - Enter a command, q|c|s (0-9)* [r|u] e.g. 'c 1' or 's 4 u', to quit enter q");
}

public static void StartBenchmark(INetworkBenchmark networkBenchmark)
{
BenchmarkStatistics.Reset();
Expand Down
3 changes: 2 additions & 1 deletion NetworkBenchmarkDotNet/BenchmarkStatistics.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BenchmarkData.cs">
// <copyright file="BenchmarkStatistics.cs">
// Copyright (c) 2020 Johannes Deml. All rights reserved.
// </copyright>
// <author>
Expand Down Expand Up @@ -83,6 +83,7 @@ public string PrintStatistics(Configuration config)
{
sb.AppendLine($"Client Round Trip Time: {clientRtt.ToString()}");
}

sb.AppendLine("```");
sb.AppendLine();

Expand Down
10 changes: 5 additions & 5 deletions NetworkBenchmarkDotNet/Configuration/BenchmarkMode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BenchmarkMode.cs">
// Copyright (c) 2020 Johannes Deml. All rights reserved.
// Copyright (c) 2021 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
Expand Down Expand Up @@ -28,11 +28,11 @@ public enum BenchmarkMode
Performance = 1 << 0,

/// <summary>
/// Run Benchmark Garbage
/// Benchmark which collects GC information
/// Runtime: ~1 minute
/// Run Benchmark Sampling
/// Benchmark which collects GC information and CPU time samples
/// Runtime: ~30 seconds
/// </summary>
Garbage = 1 << 1,
Sampling = 1 << 1,

/// <summary>
/// Run all essential benchmarks (Performance, Garbage)
Expand Down
9 changes: 5 additions & 4 deletions NetworkBenchmarkDotNet/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BenchmarkSetup.cs">
// Copyright (c) 2020 Johannes Deml. All rights reserved.
// <copyright file="Configuration.cs">
// Copyright (c) 2021 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
Expand Down Expand Up @@ -157,7 +157,7 @@ public string ToFormattedString()
{
sb.AppendLine($"* Number of clients: {Clients}");
}

sb.AppendLine($"* Parallel messages: {ParallelMessages:n0}, Size: {MessageByteSize} bytes, Payload: {MessagePayload}");
sb.AppendLine($"* TickRate per second: Client: {ClientTickRate}, Server: {ServerTickRate}");
sb.AppendLine($"* Reproduce: `");
Expand All @@ -169,7 +169,8 @@ public string ToFormattedString()
return sb.ToString();
}

private string GetDurationString() {
private string GetDurationString()
{
if (Duration < 0)
{
return "indefinite time";
Expand Down
4 changes: 3 additions & 1 deletion NetworkBenchmarkDotNet/Configuration/ExecutionMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public enum ExecutionMode
/// Run the clients
/// </summary>
Client = 1 << 0,

/// <summary>
/// Run the server
/// </summary>
Server = 1 << 1,

/// <summary>
/// Run both clients and server
/// </summary>
Complete = (1 << 2) -1
Complete = (1 << 2) - 1
}
}
2 changes: 1 addition & 1 deletion NetworkBenchmarkDotNet/Configuration/MessagePayload.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MessagePayload.cs">
// Copyright (c) 2020 Johannes Deml. All rights reserved.
// Copyright (c) 2021 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
Expand Down
18 changes: 16 additions & 2 deletions NetworkBenchmarkDotNet/Configuration/TestType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TestType.cs">
// Copyright (c) 2020 Johannes Deml. All rights reserved.
// Copyright (c) 2021 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
Expand All @@ -18,6 +18,20 @@ public enum TestType
/// 3. Client again sends each message it receives back to the server and so on.
/// Also known as Echo. Great for testing round trip if just one parallel message is used.
/// </summary>
PingPong
PingPong,

/// <summary>
/// Messages are sent manually by clients or server
/// Helps to understand data by sniffing the traffic or to debug libraries
/// Can also be used in conjunction with a ping-pong server process to get direct responses from the server
/// <example>
/// <code>
/// c 1 // sends one message for each client
/// s 5 // send five messages to each client the server is connected to
/// q // quit
/// </code>
/// </example>
/// </summary>
Manual
}
}
26 changes: 26 additions & 0 deletions NetworkBenchmarkDotNet/Libraries/AClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// </author>
// --------------------------------------------------------------------------------------------------------------------

using System;

namespace NetworkBenchmark
{
public abstract class AClient : IClient
Expand All @@ -25,15 +27,33 @@ public virtual bool IsStopped
/// Benchmark is preparing to be run
/// </summary>
protected volatile bool BenchmarkPreparing;

/// <summary>
/// Client should listen for incoming messages
/// </summary>
protected volatile bool Listen;

/// <summary>
/// Is a benchmark running (and therefore messages should be counted in the statistics)
/// </summary>
protected volatile bool BenchmarkRunning;

/// <summary>
/// Manual Mode stops the default behavior and waits for user input to execute tasks
/// </summary>
protected readonly bool ManualMode;

protected readonly byte[] Message;

protected AClient(Configuration config)
{
ManualMode = config.Test == TestType.Manual;

// Use Pinned Object Heap to reduce GC pressure
Message = GC.AllocateArray<byte>(config.MessageByteSize, true);
config.Message.CopyTo(Message, 0);
}

public virtual void StartClient()
{
Listen = true;
Expand All @@ -59,5 +79,11 @@ public virtual void StopClient()
public abstract void DisconnectClient();

public abstract void Dispose();

#region ManualMode

public abstract void SendMessages(int messageCount, TransmissionType transmissionType);

#endregion
}
}
Loading

0 comments on commit d5745af

Please sign in to comment.