Skip to content

Commit

Permalink
completed was being written within a lock, but read without. Also - m…
Browse files Browse the repository at this point in the history
…ulticore CPU level caching might have caused problems.

So, writes were replaces with Interlocked.Increment, and reads with Thread.VolatileRead

faulted will use Interlocked also, just for completeness.
  • Loading branch information
kenegozi committed Mar 3, 2011
1 parent 7725eae commit 766f69c
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/WebStresser/RawHttpClient.cs
Expand Up @@ -13,10 +13,8 @@ public class RawHttpClient
private readonly TextWriter outputWriter;

private int completed = 0;
private readonly object completedLock = new object();

private int faulted = 0;
private readonly object faultedLock = new object();

private readonly List<long> elapsed = new List<long>();
private readonly object elapsedLock = new object();
Expand All @@ -43,7 +41,7 @@ public void MakeRawHttpCall()
ThreadPool.QueueUserWorkItem(ExecuteRequests);

Thread.Sleep(1000);
while (completed < configuration.Iterations)
while (Thread.VolatileRead(ref completed) < configuration.Iterations)
{
outputWriter.WriteLine("Completed: {0:#,##0} \tFaulted: {1:#,##0} \tConnections: {2}",
completed, faulted, servicePoint.CurrentConnections);
Expand Down Expand Up @@ -125,21 +123,16 @@ private void ExecuteRequests(object state)
}
catch (WebException webException)
{
lock (faultedLock)
{
faulted++;
}
if (!webException.Message.StartsWith("The underlying connection was closed"))
Interlocked.Increment(ref faulted);
if (!webException.Message.StartsWith("The underlying connection was closed"))
{
ConsumeResponse(webException.Response);
}
}
finally
{
lock (completedLock)
{
completed++;
}
Interlocked.Increment(ref completed);
}
}, null);
}
Expand Down

0 comments on commit 766f69c

Please sign in to comment.