Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the library #21

Merged
merged 12 commits into from Jan 29, 2019

Added logs-per-second option to stress test tool

  • Loading branch information
Jiri Tomek
Jiri Tomek committed Jan 29, 2019
commit b50c85df1748b70eefb5c262b07f811ed6e6a5f7
@@ -8,6 +8,7 @@ internal class CommandLineArgs
public int NumLoggingThreads { get; private set; } = 1;
public int ExceptionFrequency { get; private set; } = 0;
public TimeSpan SendDelay { get; private set; } = TimeSpan.Zero;
public int LogsPerSecond { get; set; } = 0;

public static CommandLineArgs Parse(string[] args)
{
@@ -35,11 +36,23 @@ public static CommandLineArgs Parse(string[] args)
break;
case "-d":
case "--send-delay":
{
i++;
var value = int.Parse(args[i]);
if (value < 0)
throw new ArgumentException("Delay must be >= 0");
result.SendDelay = TimeSpan.FromMilliseconds(value);
}
break;
case "-l":
case "--logs-per-second":
{
i++;
var value = int.Parse(args[i]);
if (value <= 0)
throw new ArgumentException("Logs-per-second must be > 0");
result.LogsPerSecond = value;
}
break;
case "-e":
case "--exception-every":
@@ -71,6 +84,7 @@ private static void PrintHelp()
Usage: log4net-loggly-stress-tool.exe [-n|--num-threads <NUM_THREADS>] [-d|--send-delay <SEND_DELAY_MS>] [-e|--exception-every <NUMBER>]
-n|--num-events - Number of events to send. Must be > 0. Default: 1000
-l|--logs-per-second - How many logs per second should be generated. Total number is still defined by -n, this value just slows down the generator to given frequency.
-t|--num-threads - Number of threads used to generate logs. Must be > 0. Default: 1.
-d|--send-delay - Delay for one simulated send to Loggly servers in milliseconds. Must be >= 0. Default: 0
-e|--exception-every - Log error with exception every N logs. Must be >= 0. Default: 0 - never");
@@ -36,24 +36,29 @@ public static void Main(string[] args)

var watch = Stopwatch.StartNew();
var tasks = new List<Task>(commandLine.NumLoggingThreads);
int logsPerSecondPerThread = (int)Math.Ceiling((double)commandLine.LogsPerSecond / commandLine.NumLoggingThreads);
for (int i = 0; i < commandLine.NumLoggingThreads; i++)
{
tasks.Add(Task.Factory.StartNew(() => SendContinuously(commandLine, exception), TaskCreationOptions.LongRunning));
tasks.Add(Task.Factory.StartNew(() => SendContinuously(commandLine, exception, logsPerSecondPerThread), TaskCreationOptions.LongRunning));
}

Task.WaitAll(tasks.ToArray());

watch.Stop();

Console.WriteLine("Test finished. Elapsed: {0}, Throughput: {1} logs/s", watch.Elapsed, _count*1000 / watch.Elapsed.TotalMilliseconds);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}

private static void SendContinuously(CommandLineArgs commandLine, Exception exception)
private static void SendContinuously(CommandLineArgs commandLine, Exception exception, int logsPerSecond)
{
long currentCount = 0;
long currentCount;
Stopwatch watch = Stopwatch.StartNew();
int countThisSecond = 0;
while ((currentCount = Interlocked.Increment(ref _count)) <= commandLine.NumEvents)
{
if (currentCount % 2000 == 0)
if (currentCount % 1000 == 0)
{
Console.WriteLine("Sent: {0}", currentCount);
}
@@ -74,6 +79,14 @@ private static void SendContinuously(CommandLineArgs commandLine, Exception exce
"eu maximus nisi mauris vel lorem. Duis a ex eu orci consectetur congue sed sit amet ligula. " +
"Aenean congue mollis quam volutpat varius.");
}

// if rate limiting is applied then sleep remainder of the current second before moving on
if (logsPerSecond > 0 && ++countThisSecond >= logsPerSecond)
{
Thread.Sleep(1000 - Math.Min(1000, (int)watch.ElapsedMilliseconds));
countThisSecond = 0;
watch.Restart();
}
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.