Here's the comparison between an async action and a sync action.
Pre-requirements:
- Windows Server 2003 or 2008 with ASP.NET enabled (you cannot use Vista or XP since the limitation of connection number in IIS).
- Visual Studio 2008
- .NET framework 3.5 installed
- Powershell, tinyget in IIS 6.0 Resource Kit Tools and perfmon.
Step 1: Download the sample site with the source code.
Step 2: The HomeController contains an async action and a sync action, you can find them in the sample site (samples/SampleSite) released with the source code.
[AsyncAction]
public ActionResult AsyncAction(AsyncCallback asyncCallback, [AsyncState]object asyncState)
{
SqlConnection conn = new SqlConnection("...;Asynchronous Processing=true");
SqlCommand cmd = new SqlCommand("WAITFOR DELAY '00:00:03';", conn);
conn.Open();
return this.Async(
cmd.BeginExecuteNonQuery(asyncCallback, asyncState),
(ar) =>
{
int value = cmd.EndExecuteNonQuery(ar);
conn.Close();
return this.View();
});
}
public ActionResult SyncAction()
{
using (SqlConnection conn = new SqlConnection("...;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("WAITFOR DELAY '00:00:03';", conn);
conn.Open();
int value = cmd.ExecuteNonQuery();
}
return this.View();
}
Step 3: Configure the sample site in the IIS of you local system. Make sure that you can visit the home page in
http://localhost:7788/Home/Index.
Step 4: Increase the min thread number in the thread pool by executing the following code in the web site (you can build your own handler to do so):
ThreadPool.SetMinThreads(10, 10);
Step 5: Start perfmon and monitor the counter: ASP.NET Applications/Requests Executing
Step 6: Open the Powershell command prompt, execute the following scripts (it can be found in the samples/Benchmarks/Scripts.ps1):
Measure-Command {.\tinyget -srv:localhost -r:7788 -uri:/Home/SyncAction -threads:30 -loop:1}
[System.Threading.Thread]::Sleep(2000)
Measure-Command {.\tinyget -srv:localhost -r:7788 -uri:/Home/AsyncAction -threads:30 -loop:1}
[System.Threading.Thread]::Sleep(2000)
Measure-Command {.\tinyget -srv:localhost -r:7788 -uri:/Home/AsyncAction -threads:40 -loop:1}
[System.Threading.Thread]::Sleep(2000)
Measure-Command {.\tinyget -srv:localhost -r:7788 -uri:/Home/AsyncAction -threads:50 -loop:1}
Result:
| Max Request Executing | Execution Time (s) |
| 6 | 15.06 |
| 30 | 3.10 |
| 40 | 3.11 |
| 50 | 3.10 |
Snapshot of perfmon: