Question

I want to simulate a login stress test to LDAP. send a auth request to LDAP from multiple request at the same time.

I wrote the following:

 static void Main(string[] args)
    {
        string a = "", b = "", c = "", d = "", 
e = "", f = "", g = "", h = "", i = "", j = "", k = "";

        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        var startTime = DateTime.Now.ToString("hh:mm:ss.fff tt");

        Parallel.Invoke
               (
                   () => a = LdsConnection.Auth("username", "password", true),
                   () => b = LdsConnection.Auth("username", "password", true),
                   () => c = LdsConnection.Auth("username", "password", true),
                   () => d = LdsConnection.Auth("username", "password", true),
                   () => e = LdsConnection.Auth("username", "password", true),
                   () => f = LdsConnection.Auth("username", "password", true),
                   () => g = LdsConnection.Auth("username", "password", true),
                   () => h = LdsConnection.Auth("username", "password", true),
                   () => i = LdsConnection.Auth("username", "password", true),
                   () => j = LdsConnection.Auth("username", "password", true),
                   () => k = LdsConnection.Auth("username", "password", true)
               );
        stopwatch.Stop();
        var endTime = DateTime.Now.ToString("hh:mm:ss.fff tt");

        Console.WriteLine(a + "\n");
        Console.WriteLine(b + "\n");
        Console.WriteLine(c + "\n");
        Console.WriteLine(d + "\n");
        Console.WriteLine(e + "\n");
        Console.WriteLine(f + "\n");
        Console.WriteLine(g + "\n");
        Console.WriteLine(h + "\n");
        Console.WriteLine(j + "\n");
        Console.WriteLine(k + "\n");

        // Total run time
        Console.WriteLine("Started:{0}\nEnded: {1}\nElapsed: {2}", 
startTime, endTime, stopwatch.Elapsed);

        Console.ReadKey();
    }


public static string Auth (string username, string password, bool fullDn)
   {
       var url = GenerateUrl(fullDn);

       Stopwatch stopwatch = new Stopwatch();
       stopwatch.Start();
       var startTime = DateTime.Now.ToString("hh:mm:ss.fff tt");
       try
       {
           Connect(username, password, url);
           stopwatch.Stop();
           var elaps = stopwatch.Elapsed.ToString();
           var endTime = DateTime.Now.ToString("hh:mm:ss.fff tt");
           return string.Format("Started:{0}\nEnded: {1}\nElapsed: {2}", 
startTime, endTime, elaps);
       }
       catch (Exception exception)
       {

           return "Error";
       }

   }

The problems are:

  1. How can i automate this, for example a parameter which represent the number of calls in place of calling the same function multiple time manually becuase the username and the password is the same for all functions?
  2. I'm not really sure if all calls are requesting authentication at the same time.

Any tips please?

Was it helpful?

Solution

You could try this.

static void Main(string[] args)
{
    const int numberOfTasks = 10;

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    var startTime = DateTime.Now.ToString("hh:mm:ss.fff tt");

    List<Task<string>> taskList = new List<Task<string>>();

    for(int i = 0; i < numberOfTasks; i++)
      taskList.Add(new Task<string> ( LdsConnection.Auth("username", "password", true) )

    foreach(var task in taskList)
      taskList.Start();

    foreach(var task in taskList)
      Console.WriteLine(task.Result);

    Console.ReadKey();
}

OTHER TIPS

Parallel.ForEach() would probably be a better choice for you. You can see a great little example in this answer.

For the timing, only the server can determine that, since it's the server's perspective that matters. You can check its logs to get a sense of how close the requests are coming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top