Skip to content

Commit

Permalink
Merge branch 'lahma-add-benchmarks'
Browse files Browse the repository at this point in the history
  • Loading branch information
mganss committed Apr 23, 2021
2 parents 91ea0ca + e3505d5 commit ebd153f
Show file tree
Hide file tree
Showing 7 changed files with 14,546 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,5 @@ coverity.zip
*.lock.json
*.nuget.props
*.nuget.targets

/.idea
6 changes: 6 additions & 0 deletions HtmlSanitizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlSanitizer", "src\HtmlSa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlSanitizer.Tests", "test\HtmlSanitizer.Tests\HtmlSanitizer.Tests.csproj", "{55D772A0-8D8C-4CF7-A876-E6DAB8ED42C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlSanitizer.Benchmark", "test\HtmlSanitizer.Benchmark\HtmlSanitizer.Benchmark.csproj", "{5445D00F-08E4-4D04-B46E-9077FE46ED27}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{55D772A0-8D8C-4CF7-A876-E6DAB8ED42C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55D772A0-8D8C-4CF7-A876-E6DAB8ED42C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55D772A0-8D8C-4CF7-A876-E6DAB8ED42C0}.Release|Any CPU.Build.0 = Release|Any CPU
{5445D00F-08E4-4D04-B46E-9077FE46ED27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5445D00F-08E4-4D04-B46E-9077FE46ED27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5445D00F-08E4-4D04-B46E-9077FE46ED27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5445D00F-08E4-4D04-B46E-9077FE46ED27}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 22 additions & 0 deletions test/HtmlSanitizer.Benchmark/HtmlSanitizer.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<SonarQubeExclude>true</SonarQubeExclude>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\HtmlSanitizer\HtmlSanitizer.csproj" />
</ItemGroup>

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

<ItemGroup>
<None Update="*.html" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions test/HtmlSanitizer.Benchmark/HtmlSanitizerBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.IO;
using BenchmarkDotNet.Attributes;

namespace Ganss.XSS.Benchmark
{
[MemoryDiagnoser]
public class HtmlSanitizerBenchmark
{
private HtmlSanitizer sanitizer;
private string googleFileContent;
private string largeFileContent;

[GlobalSetup]
public void GlobalSetup()
{
googleFileContent = File.ReadAllText("google.html");
largeFileContent = File.ReadAllText("ecmascript.html");
sanitizer = new HtmlSanitizer();
}

/// <summary>
/// Small content produced by for example Orchard, nothing to sanitize.
/// </summary>
[Benchmark]
public void SanitizeSmall()
{
sanitizer.Sanitize("<p>Never in all their history have men been able truly to conceive of the world as one: a single sphere, a globe, having the qualities of a globe, a round earth in which all the directions eventually meet, in which there is no center because every point, or none, is center — an equal earth which all men occupy as equals. The airman's earth, if free men make it, will be truly round: a globe in practice, not in theory.</p>\n<p>Science cuts two ways, of course; its products can be used for both good and evil. But there's no turning back from science. The early warnings about technological dangers also come from science.</p>\n<p>What was most significant about the lunar voyage was not that man set foot on the Moon but that they set eye on the earth.</p>\n<p>A Chinese tale tells of some men sent to harm a young girl who, upon seeing her beauty, become her protectors rather than her violators. That's how I felt seeing the Earth for the first time. I could not help but love and cherish her.</p>\n<p>For those who have seen the Earth from space, and for the hundreds and perhaps thousands more who will, the experience most certainly changes your perspective. The things that we share in our world are far more valuable than those which divide us.</p>\n");
}

/// <summary>
/// Google is script-heavy.
/// </summary>
[Benchmark]
public void SanitizeGoogle()
{
sanitizer.Sanitize(googleFileContent);
}

/// <summary>
/// Partial ECMAScript is DOM-heavy.
/// </summary>
[Benchmark]
public void SanitizeLarge()
{
sanitizer.Sanitize(largeFileContent);
}
}
}
12 changes: 12 additions & 0 deletions test/HtmlSanitizer.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;

namespace Ganss.XSS.Benchmark
{
public static class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(HtmlSanitizerBenchmark).Assembly).Run(args);
}
}
}
14,243 changes: 14,243 additions & 0 deletions test/HtmlSanitizer.Benchmark/ecmascript.html

Large diffs are not rendered by default.

213 changes: 213 additions & 0 deletions test/HtmlSanitizer.Benchmark/google.html

Large diffs are not rendered by default.

0 comments on commit ebd153f

Please sign in to comment.