我想模拟LDAP的登录压力测试。同时从多个请求向LDAP发送auth请求。
我写了以下文章:
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";
}
}问题是:
有小费吗?
发布于 2014-04-02 12:18:23
你可以试试这个。
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();
}发布于 2014-04-02 12:16:19
Parallel.ForEach()可能是您更好的选择。您可以在this answer中看到一个很好的小例子。
对于时间安排,只有服务器才能确定,因为重要的是服务器的透视图。您可以检查它的日志,以了解请求有多接近。
https://stackoverflow.com/questions/22810315
复制相似问题