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

feat: add benchmarks #92

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Tomlyn.Benchmarks/Tomlyn.Benchmarks/BasicBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using BenchmarkDotNet.Attributes;
using Tomlyn.Model;

namespace Tomlyn.Benchmarks;

[MemoryDiagnoser(false)]
public class BasicBenchmark
{
// spec-example-1.toml
private const string TomlString = """
# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[database]
server = "192.168.1.1"
ports = [ 8000, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]

# Indentation (tabs and/or spaces) is allowed but not required
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"

[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
""";

private static readonly TomlTable TomlObject = Toml.ToModel(TomlString);

[Benchmark]
public TomlTable StringToModel() => Toml.ToModel(TomlString);

[Benchmark]
public string ModelToString() => Toml.FromModel(TomlObject);
}
4 changes: 4 additions & 0 deletions src/Tomlyn.Benchmarks/Tomlyn.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using BenchmarkDotNet.Running;
using Tomlyn.Benchmarks;

BenchmarkRunner.Run<BasicBenchmark>();
25 changes: 25 additions & 0 deletions src/Tomlyn.Benchmarks/Tomlyn.Benchmarks/Tomlyn.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
xoofx marked this conversation as resolved.
Show resolved Hide resolved
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Tomlyn\Tomlyn.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="twitter.toml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions src/Tomlyn.Benchmarks/Tomlyn.Benchmarks/TwitterBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using BenchmarkDotNet.Attributes;
using Tomlyn.Model;

namespace Tomlyn.Benchmarks;

[MemoryDiagnoser(false)]
public class TwitterBenchmark
{
// twitter.json -> twitter.toml
private string? _tomlString;
private TomlTable? _tomlObject;

[GlobalSetup]
public async Task Setup()
{
_tomlString = await File.ReadAllTextAsync("./twitter.toml");
_tomlObject = Toml.ToModel(_tomlString);
}

[Benchmark]
public TomlTable StringToModel() => Toml.ToModel(_tomlString!);

[Benchmark]
public string ModelToString() => Toml.FromModel(_tomlObject!);
}
Loading
Loading