Skip to content

Commit c434e6e

Browse files
authored
Merge pull request #211 from jafin/chore/benchmarkdotnet-perf
Chore/benchmarkdotnet perf
2 parents a54c7d8 + d4213e6 commit c434e6e

7 files changed

Lines changed: 99 additions & 108 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ The main idea behind AngleSharp.Css is to expose the CSSOM as it would be in the
7979
- Calculated values (i.e., `calc(20px + 50%)`)
8080
- Window-based declaration calculations, see `window.GetComputedStyle`
8181

82+
## Benchmarks
83+
84+
The `AngleSharp.Performance.Css` project uses [BenchmarkDotNet](https://benchmarkdotnet.org/) to compare CSS parsing performance across libraries. Run the benchmarks in Release mode:
85+
86+
```bash
87+
dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0
88+
```
89+
90+
To run a quick smoke test instead of a full benchmark:
91+
92+
```bash
93+
dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0 -- --job short
94+
```
95+
8296
## Participating
8397

8498
Participation in the project is highly welcome. For this project the same rules as for the AngleSharp core project may be applied.
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net472;net10.0</TargetFrameworks>
4-
<ApplicationIcon />
54
<OutputType>Exe</OutputType>
6-
<StartupObject />
7-
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
8-
<LangVersion>default</LangVersion>
9-
</PropertyGroup>
10-
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
11-
<PlatformTarget>AnyCPU</PlatformTarget>
5+
<LangVersion>9</LangVersion>
126
</PropertyGroup>
137

148
<ItemGroup>
@@ -18,19 +12,18 @@
1812
</ItemGroup>
1913

2014
<ItemGroup>
21-
<ProjectReference Include="..\AngleSharp.Performance.Common\AngleSharp.Performance.Common.csproj" />
22-
<ProjectReference Include="..\AngleSharp.Performance.Utilities\AngleSharp.Performance.Utilities.csproj" />
2315
<ProjectReference Include="..\AngleSharp.Css\AngleSharp.Css.csproj">
2416
<TargetFramework>netstandard2.0</TargetFramework>
2517
</ProjectReference>
2618
</ItemGroup>
2719

2820
<ItemGroup>
2921
<PackageReference Include="AngleSharp" Version="1.4.0" />
30-
<PackageReference Include="ExCSS" version="4.3.1" />
22+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
23+
<PackageReference Include="ExCSS" Version="4.3.1" />
3124
</ItemGroup>
3225

3326
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
34-
<PackageReference Include="Alba.CsCss" version="1.0.1.0" />
27+
<PackageReference Include="Alba.CsCss" Version="1.0.1.0" />
3528
</ItemGroup>
36-
</Project>
29+
</Project>

src/AngleSharp.Performance.Css/AngleSharpParser.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/AngleSharp.Performance.Css/CsCssParser.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
namespace AngleSharp.Performance.Css
2+
{
3+
using AngleSharp.Css.Parser;
4+
using BenchmarkDotNet.Attributes;
5+
using ExCSS;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
11+
[MemoryDiagnoser]
12+
public class CssParserBenchmarks
13+
{
14+
private static readonly CssParserOptions AngleSharpOptions = new CssParserOptions
15+
{
16+
IsIncludingUnknownDeclarations = true,
17+
IsIncludingUnknownRules = true,
18+
IsToleratingInvalidSelectors = true,
19+
};
20+
21+
private static readonly CssParser AngleSharpParser = new CssParser(AngleSharpOptions);
22+
23+
private string _source = null!;
24+
25+
[ParamsSource(nameof(CssFileNames))]
26+
public string CssFileName { get; set; } = null!;
27+
28+
public static IEnumerable<string> CssFileNames()
29+
{
30+
var dir = Path.Combine(AppContext.BaseDirectory, "Samples");
31+
32+
return Directory.GetFiles(dir, "*.css")
33+
.OrderBy(f => f)
34+
.Select(f => Path.GetFileNameWithoutExtension(f));
35+
}
36+
37+
[GlobalSetup]
38+
public void Setup()
39+
{
40+
var path = Path.Combine(AppContext.BaseDirectory, "Samples", CssFileName + ".css");
41+
_source = File.ReadAllText(path);
42+
}
43+
44+
[Benchmark(Baseline = true)]
45+
public object AngleSharp()
46+
{
47+
return AngleSharpParser.ParseStyleSheet(_source);
48+
}
49+
50+
[Benchmark]
51+
public object ExCss()
52+
{
53+
var parser = new StylesheetParser();
54+
return parser.Parse(_source);
55+
}
56+
57+
#if NET472
58+
[Benchmark]
59+
public object CsCss()
60+
{
61+
var parser = new Alba.CsCss.Style.CssLoader
62+
{
63+
Compatibility = Alba.CsCss.BrowserCompatibility.FullStandards
64+
};
65+
66+
try
67+
{
68+
return parser.ParseSheet(_source, new Uri("http://localhost/foo.css"), new Uri("http://localhost"));
69+
}
70+
catch
71+
{
72+
return null!;
73+
}
74+
}
75+
#endif
76+
}
77+
}

src/AngleSharp.Performance.Css/ExCssParser.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
11
namespace AngleSharp.Performance.Css
22
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.IO;
3+
using BenchmarkDotNet.Running;
64

75
class Program
86
{
9-
static void Main(String[] args)
7+
static void Main(string[] args)
108
{
11-
var samplesDir = Path.Combine(AppContext.BaseDirectory, "Samples");
12-
var stylesheets = new FileTests()
13-
.IncludeFromDirectory(samplesDir);
14-
15-
var parsers = new List<ITestee>
16-
{
17-
new AngleSharpParser(),
18-
new ExCssParser(),
19-
#if NET472
20-
new CsCssParser(),
21-
#endif
22-
};
23-
24-
var testsuite = new TestSuite(parsers, stylesheets.Tests, new Output(), new Warmup())
25-
{
26-
NumberOfRepeats = 5,
27-
NumberOfReRuns = 1,
28-
};
29-
30-
testsuite.Run();
9+
BenchmarkRunner.Run<CssParserBenchmarks>(args: args);
3110
}
3211
}
3312
}

0 commit comments

Comments
 (0)