登录压力测试

本文关键字:压力测试 登录 | 更新日期: 2023-09-27 17:56:23

我想模拟LDAP的登录压力测试。同时从多个请求向 LDAP 发送身份验证请求。

我写了以下内容:

 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";
       }
   }

问题是:

  1. 我如何自动执行此操作,例如表示调用次数的参数,而不是手动多次调用同一函数,因为所有函数的用户名和密码都相同?
  2. 我不确定是否所有呼叫都同时请求身份验证。

请问有什么提示吗?

登录压力测试

你可以

试试这个。

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();
}
Parallel.ForEach()可能是

您更好的选择。 你可以在这个答案中看到一个很好的小例子。

对于时间,只有服务器才能确定,因为服务器的视角很重要。 您可以检查其日志以了解请求的接近程度。