Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created July 21, 2012 11:51
Show Gist options
  • Save benfoster/3155607 to your computer and use it in GitHub Desktop.
Save benfoster/3155607 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type q to Quit");
string input;
while ((input = Console.ReadLine()) != "q")
{
var sw = new Stopwatch();
sw.Start();
BeginLoadTestAsync(1000).Wait();
//BeginLoadTestSync(1000);
sw.Stop();
Console.WriteLine("Total time: " + sw.Elapsed.ToString());
}
}
static void BeginLoadTestSync(int numberOfClients = 1)
{
using (var httpClient = new HttpClient())
{
for (int i = 0; i < numberOfClients; i++)
{
var response = httpClient.GetAsync("http://localhost:62351/api/posts").Result;
var content = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Client {0}: {1}", i, response.StatusCode);
}
}
}
static async Task BeginLoadTestAsync(int numberOfClients = 1)
{
List<Task> tasks = new List<Task>();
using (var httpClient = new HttpClient())
{
for (int i = 0; i < numberOfClients; i++)
{
tasks.Add(GetResponse(i, httpClient));
}
await Task.WhenAll(tasks);
}
}
static async Task GetResponse(int index, HttpClient httpClient)
{
var response = await httpClient.GetAsync("http://localhost:62351/api/posts");
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine("Client {0}: {1}", index, response.StatusCode);
}
}
// Controller methods:
public IEnumerable<Post> Get([FromUri]GetPostsCommand parameters)
{
var posts = session.Query<Post>();
if (parameters.SiteId.HasValue)
posts = posts.Where(p => p.SiteId == parameters.SiteId.Value);
return posts
.Skip(parameters.PageSize * (parameters.Page - 1))
.Take(parameters.PageSize)
.ToList();
}
//public async Task<IEnumerable<Post>> Get([FromUri]GetPostsCommand parameters)
//{
// using (var session = store.OpenAsyncSession())
// {
// var posts = session.Query<Post>();
// if (parameters.SiteId.HasValue)
// posts = posts.Where(p => p.SiteId == parameters.SiteId.Value);
// return await posts
// .Skip(parameters.PageSize * (parameters.Page - 1))
// .Take(parameters.PageSize)
// .ToListAsync();
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment