Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BenchmarkDotNet with async task

I'm trying to run this code :

public class Parsing
{
    private const string Url ="blabla";
    private static HttpClient client = new HttpClient();

    private static Task<string> newton = ParseNewton();
    private static Task<string> servicestack = ParseServiceStack();

    [Benchmark]
    private static async Task<string> ParseNewton()
    {

        var response = client.GetAsync(Url).Result;

        var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

        var serializer = new Newtonsoft.Json.JsonSerializer();

        using (var sr = new StreamReader(stream))
        using (var jsonTextReader = new JsonTextReader(sr))
        {
            return serializer.Deserialize<string>(jsonTextReader);
        }

    }

    [Benchmark]
    private static async Task<string> ParseServiceStack()
    {

        var response = client.GetAsync(Url).Result;

        var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

        return ServiceStack.Text.JsonSerializer.DeserializeFromStream<string>(stream);

    }
}

And the call is

internal class Program
{
    public static void Main(string[] args)
    {
        var summary = BenchmarkRunner.Run<Parsing>();

        Console.ReadKey();
    }
}

I'm pretty sure I did many things wrong (since it doesn't work) ; I always get the message No Benchmark found and from the samples I found I could not find how to make it work.

I'd like to deserialise like 1000 times the same response from the url given with both NewtonSoft & ServiceStack and get a good benchmark from it. How can I make this code work and what did I do wrong ?

like image 725
Cholesterol Avatar asked Apr 29 '26 02:04

Cholesterol


1 Answers

Both the class and the methods need to be public and can not be static. The class must also not be sealed.

like image 104
Adam Sitnik Avatar answered Apr 30 '26 15:04

Adam Sitnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!