Skip to main content
1 of 3
Kixoka
  • 143
  • 5

Creating an app to simulate load

I am writing an app that is supposed to simulate load on our web api and NServiceBus(particular) service. This is the first time I am writing an application like this. It appears to run ok, however it takes a few seconds before anything shows up in the console window. Is there a better way to do this?

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press any key to start...");
        Console.ReadKey();
        Console.WriteLine();

        RunLoad(100);

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    private static void RunLoad(int loadSize)
    {
        Parallel.For(0, loadSize, i =>
        {
            RunTasks();
        });
    }

    public static async Task RunTasks()
    {
        var tasks = new List<Task>
        {
            GetLoanByLoanId("7000002050"),
            GetEnvelopesForLoan("7000002077"),
            GetLoanDataByLoanId("7000002072")

        };

        await Task.WhenAll(tasks);
    }

    private static async Task GetLoanByLoanId(string loanNumber)
    {
        await Task.Run(() => {
            var svc = new WebApiService();
            var msg1 = svc.GetLoanByLoanId(loanNumber);
            if (string.IsNullOrWhiteSpace(msg1)) return;
            Console.WriteLine($"GL {loanNumber}: {msg1.Length}");
        });
    }

    private static async Task GetLoanDataByLoanId(string loanNumber)
    {
        await Task.Run(() => {
            var svc = new WebApiService();
            var msg1 = svc.GetLoanDataByLoanId(loanNumber);
            if (string.IsNullOrWhiteSpace(msg1)) return;
            Console.WriteLine($"GLD {loanNumber}: {msg1.Length}");
        });
    }

    private static async Task GetEnvelopesForLoan(string loanNumber)
    {
        await Task.Run(() => {
            var svc = new WebApiService();
            var msg1 = svc.GetEnvelopesForLoan(loanNumber);
            if (string.IsNullOrWhiteSpace(msg1)) return;
            Console.WriteLine($"GEFL {loanNumber}: {msg1.Length}");
        });
    }

}
}
Kixoka
  • 143
  • 5