2014-04-02 112 views
1

我想模擬登錄壓力測試到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. 我怎麼能自動化此,例如其代表的地方調用同一個功能多的呼叫的數量參數時間手動becuase的用戶名和密碼是相同的所有功能?
  2. 我不確定是否所有的呼叫都同時請求認證。

請提出任何提示?

回答

1

你可以試試這個。

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(); 
} 
1

Parallel.ForEach()可能是您更好的選擇。您可以在this answer中看到一個很好的示例。

對於計時,只有服務器可以確定,因爲這是服務器的視角。您可以檢查其日誌以瞭解請求的接近程度。

+0

謝謝約翰C,我不明白我該如何控制循環次數。 – Maro

+0

ForEach()的第一個參數是一個可以動態生成的數組,因此您可以根據需要快速擴展一個請求。 –

+0

+1非常感謝 – Maro