Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BenchmarkSwitcher should support generic types with parameterless public ctors #652

Closed
adamsitnik opened this issue Feb 16, 2018 · 4 comments

Comments

@adamsitnik
Copy link
Member

It's possible to run generic benchmarks via BenchmarkRunner.Run<T> but not via BenchmarkSwitcher.FromTypes

Case: I am currently benchmarking the serializers. I don't want to create new class per type.

BenchmarkSwitcher.FromTypes
    (
        new[]
        {
            typeof(Json_SerializeToString<LoginViewModel>),
            typeof(Json_SerializeToString<Location>),

            typeof(Json_SerializeToStream<LoginViewModel>),
            typeof(Json_SerializeToStream<Location>),
        }
    )
    .Run(args, new BenchmarkConfig());

public class Json_SerializeToString<T>
{
    private readonly T value;

    public Json_SerializeToString() => value = DataGenerator.Generate<T>();

    [Benchmark(Description = "Jil")]
    public string Jil_() => Jil.JSON.Serialize<T>(value);

    [Benchmark(Description = "JSON.NET")]
    public string JsonNet_() => Newtonsoft.Json.JsonConvert.SerializeObject(value);

    [Benchmark(Description = "Utf8Json")]
    public string Utf8Json_() => Utf8Json.JsonSerializer.ToJsonString(value);
}

@AndreyAkinshin to prevent some bugs I am going to add a requirement: generics are ok if the type has a public parameterless ctor.

@adamsitnik adamsitnik self-assigned this Feb 16, 2018
@AndreyAkinshin
Copy link
Member

AndreyAkinshin commented Feb 16, 2018

to prevent some bugs I am going to add a requirement: generics are ok if the type has a public parameterless ctor.

LGTM

@adamsitnik
Copy link
Member Author

Done! @nietras @xoofx guys you might be interested in updating to 0.10.12.443 since it has better generics support (full names are used everywhere, not Generic`1 thing)

static void Main(string[] args)
    => BenchmarkSwitcher.FromTypes
        (
            new[]
            {
                typeof(Json_ToString<LoginViewModel>),
                typeof(Json_ToString<Location>),
                typeof(Json_ToString<IndexViewModel>),

                typeof(Json_ToStream<LoginViewModel>),
                typeof(Json_ToStream<Location>),
                typeof(Json_ToStream<IndexViewModel>),

                typeof(Json_FromString<LoginViewModel>),
                typeof(Json_FromString<Location>),
                typeof(Json_FromString<IndexViewModel>),

                typeof(Json_FromStream<LoginViewModel>),
                typeof(Json_FromStream<Location>),
                typeof(Json_FromStream<IndexViewModel>),
            }
        )
        .Run(args, new BenchmarkConfig());

image

@nietras
Copy link
Contributor

nietras commented Feb 19, 2018 via email

@xoofx
Copy link
Member

xoofx commented Feb 23, 2018

Amazing, thanks @adamsitnik!

alinasmirnova pushed a commit to alinasmirnova/BenchmarkDotNet that referenced this issue Sep 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants