Skip to content
Merged
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ The main idea behind AngleSharp.Css is to expose the CSSOM as it would be in the
- Calculated values (i.e., `calc(20px + 50%)`)
- Window-based declaration calculations, see `window.GetComputedStyle`

## Benchmarks

The `AngleSharp.Performance.Css` project uses [BenchmarkDotNet](https://benchmarkdotnet.org/) to compare CSS parsing performance across libraries. Run the benchmarks in Release mode:

```bash
dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0
```

To run a quick smoke test instead of a full benchmark:

```bash
dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0 -- --job short
```

## Participating

Participation in the project is highly welcome. For this project the same rules as for the AngleSharp core project may be applied.
Expand Down
22 changes: 9 additions & 13 deletions src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ApplicationIcon />
<TargetFrameworks>net472;net10.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<StartupObject />
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<LangVersion>default</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<LangVersion>9</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,16 +12,18 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AngleSharp.Performance.Common\AngleSharp.Performance.Common.csproj" />
<ProjectReference Include="..\AngleSharp.Performance.Utilities\AngleSharp.Performance.Utilities.csproj" />
<ProjectReference Include="..\AngleSharp.Css\AngleSharp.Css.csproj">
<TargetFramework>netstandard2.0</TargetFramework>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Alba.CsCss" version="1.0.1.0" />
<PackageReference Include="AngleSharp" Version="1.4.0" />
<PackageReference Include="ExCSS" version="2.0.6" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
<PackageReference Include="ExCSS" Version="4.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<PackageReference Include="Alba.CsCss" Version="1.0.1.0" />
</ItemGroup>
</Project>
</Project>
26 changes: 0 additions & 26 deletions src/AngleSharp.Performance.Css/AngleSharpParser.cs

This file was deleted.

26 changes: 0 additions & 26 deletions src/AngleSharp.Performance.Css/CsCssParser.cs

This file was deleted.

77 changes: 77 additions & 0 deletions src/AngleSharp.Performance.Css/CssParserBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace AngleSharp.Performance.Css
{
using AngleSharp.Css.Parser;
using BenchmarkDotNet.Attributes;
using ExCSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

[MemoryDiagnoser]
public class CssParserBenchmarks
{
private static readonly CssParserOptions AngleSharpOptions = new CssParserOptions
{
IsIncludingUnknownDeclarations = true,
IsIncludingUnknownRules = true,
IsToleratingInvalidSelectors = true,
};

private static readonly CssParser AngleSharpParser = new CssParser(AngleSharpOptions);

private string _source = null!;

[ParamsSource(nameof(CssFileNames))]
public string CssFileName { get; set; } = null!;

public static IEnumerable<string> CssFileNames()
{
var dir = Path.Combine(AppContext.BaseDirectory, "Samples");

return Directory.GetFiles(dir, "*.css")
.OrderBy(f => f)
.Select(f => Path.GetFileNameWithoutExtension(f));
}

[GlobalSetup]
public void Setup()
{
var path = Path.Combine(AppContext.BaseDirectory, "Samples", CssFileName + ".css");
_source = File.ReadAllText(path);
}

[Benchmark(Baseline = true)]
public object AngleSharp()
{
return AngleSharpParser.ParseStyleSheet(_source);
}

[Benchmark]
public object ExCss()
{
var parser = new StylesheetParser();
return parser.Parse(_source);
}

#if NET472
[Benchmark]
public object CsCss()
{
var parser = new Alba.CsCss.Style.CssLoader
{
Compatibility = Alba.CsCss.BrowserCompatibility.FullStandards
};

try
{
return parser.ParseSheet(_source, new Uri("http://localhost/foo.css"), new Uri("http://localhost"));
}
catch
{
return null!;
}
}
#endif
}
}
18 changes: 0 additions & 18 deletions src/AngleSharp.Performance.Css/ExCssParser.cs

This file was deleted.

25 changes: 3 additions & 22 deletions src/AngleSharp.Performance.Css/Program.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
namespace AngleSharp.Performance.Css
{
using System;
using System.Collections.Generic;
using System.IO;
using BenchmarkDotNet.Running;

class Program
{
static void Main(String[] args)
static void Main(string[] args)
{
var samplesDir = Path.Combine(AppContext.BaseDirectory, "Samples");
var stylesheets = new FileTests()
.IncludeFromDirectory(samplesDir);

var parsers = new List<ITestee>
{
new AngleSharpParser(),
new ExCssParser(),
new CsCssParser(),
};

var testsuite = new TestSuite(parsers, stylesheets.Tests, new Output(), new Warmup())
{
NumberOfRepeats = 5,
NumberOfReRuns = 1,
};

testsuite.Run();
BenchmarkRunner.Run<CssParserBenchmarks>(args: args);
}
}
}
Loading