Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Created June 20, 2013 11:50
Show Gist options
  • Select an option

  • Save DominicFinn/5822124 to your computer and use it in GitHub Desktop.

Select an option

Save DominicFinn/5822124 to your computer and use it in GitHub Desktop.
Example of sending lots of requests to the server.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace WebRequest
{
[TestClass]
public class AttackTheServer
{
[TestMethod]
public void Attack()
{
var requestMaster = new RequestMaster("http://www.test.dev");
int damage = requestMaster.Attack(10);
Debug.WriteLine("Total hit count = {0}", damage);
}
}
public class RequestMaster
{
private readonly string victim;
private int totalHitCount;
/// <param name="victim">The URL of the test subject</param>
public RequestMaster(string victim)
{
this.victim = victim;
}
/// <summary>
/// A powerful attack that increases in power with each 'hit'
/// </summary>
/// <param name="hitCount">The amount of times to hit the victim</param>
public int Attack(int hitCount)
{
Parallel.For(0, hitCount, (x) =>
{
for (var i = 0; i < x; i++)
{
var client = new System.Net.WebClient();
client.DownloadDataAsync(new Uri(this.victim));
totalHitCount++;
Debug.WriteLine("Hit number {0}!", this.totalHitCount);
}
});
return this.totalHitCount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment