Skip to content

Instantly share code, notes, and snippets.

@Azer0s

Azer0s/Benchmark.cs

Last active Jul 23, 2019
Embed
What would you like to do?
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using GraphQL.Client;
using GraphQL.Common.Request;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
var gqlClient = new GraphQLClient("http://localhost:8080/graphql");
var sw = new Stopwatch();
var str = string.Empty;
#region Request all
var getAllRequest = new GraphQLRequest
{
Query = @"
query {
users {
id
username
items {
id
name
description
}
}
}
"
};
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await gqlClient.GetAsync(getAllRequest);
str = result.Data.ToString();
}
sw.Stop();
Console.WriteLine("Request all users with all items - GraphQL");
Console.WriteLine(sw.Elapsed.ToString("g"));
var httpClient = new HttpClient();
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await httpClient.GetAsync("http://localhost:8080/user");
str = await result.Content.ReadAsStringAsync();
}
sw.Stop();
Console.WriteLine("Request all users with all items - REST");
Console.WriteLine(sw.Elapsed.ToString("g"));
#endregion
Console.WriteLine();
#region Single user
var singleUserRequest = new GraphQLRequest
{
Query = @"
query {
user(username: ""Test10"") {
id
username
items {
id
name
description
}
}
}
"
};
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await gqlClient.GetAsync(singleUserRequest);
str = result.Data.ToString();
}
sw.Stop();
Console.WriteLine("Request a single user with all items - GraphQL");
Console.WriteLine(sw.Elapsed.ToString("g"));
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await httpClient.GetAsync("http://localhost:8080/user/Test10");
str = await result.Content.ReadAsStringAsync();
}
sw.Stop();
Console.WriteLine("Request a single user with all items - REST");
Console.WriteLine(sw.Elapsed.ToString("g"));
#endregion
Console.WriteLine();
#region Two users
var doubleUserRequest = new GraphQLRequest
{
Query = @"
query {
first: user(username: ""Test10"") {
id
username
items {
id
name
description
}
}
second: user(username: ""Test5"") {
id
username
items {
id
name
description
}
}
}
"
};
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await gqlClient.GetAsync(doubleUserRequest);
str = result.Data.ToString();
}
sw.Stop();
Console.WriteLine("Request two users with all items - GraphQL");
Console.WriteLine(sw.Elapsed.ToString("g"));
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await httpClient.GetAsync("http://localhost:8080/user/Test10");
str = await result.Content.ReadAsStringAsync();
result = await httpClient.GetAsync("http://localhost:8080/user/Test5");
str = await result.Content.ReadAsStringAsync();
}
sw.Stop();
Console.WriteLine("Request two users with all items - REST");
Console.WriteLine(sw.Elapsed.ToString("g"));
#endregion
Console.WriteLine();
#region Four users
var fourUserRequest = new GraphQLRequest
{
Query = @"
query {
first: user(username: ""Test10"") {
id
username
items {
id
name
description
}
}
second: user(username: ""Test5"") {
id
username
items {
id
name
description
}
}
third: user(username: ""Test10"") {
id
username
items {
id
name
description
}
}
fourth: user(username: ""Test5"") {
id
username
items {
id
name
description
}
}
}
"
};
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await gqlClient.GetAsync(fourUserRequest);
str = result.Data.ToString();
}
sw.Stop();
Console.WriteLine("Request four users with all items - GraphQL");
Console.WriteLine(sw.Elapsed.ToString("g"));
sw.Reset();
sw.Start();
for (var i = 0; i < 1000; i++)
{
var result = await httpClient.GetAsync("http://localhost:8080/user/Test10");
str = await result.Content.ReadAsStringAsync();
result = await httpClient.GetAsync("http://localhost:8080/user/Test5");
str = await result.Content.ReadAsStringAsync();
result = await httpClient.GetAsync("http://localhost:8080/user/Test10");
str = await result.Content.ReadAsStringAsync();
result = await httpClient.GetAsync("http://localhost:8080/user/Test5");
str = await result.Content.ReadAsStringAsync();
}
sw.Stop();
Console.WriteLine("Request four users with all items - REST");
Console.WriteLine(sw.Elapsed.ToString("g"));
#endregion
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.