aboutsummaryrefslogtreecommitdiff
path: root/benches
diff options
context:
space:
mode:
Diffstat (limited to 'benches')
-rw-r--r--benches/Jellyfin.Common.Benches/HexDecodeBenches.cs45
-rw-r--r--benches/Jellyfin.Common.Benches/HexEncodeBenches.cs32
-rw-r--r--benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj16
-rw-r--r--benches/Jellyfin.Common.Benches/Program.cs14
4 files changed, 107 insertions, 0 deletions
diff --git a/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs b/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
new file mode 100644
index 000000000..d9a107b69
--- /dev/null
+++ b/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
@@ -0,0 +1,45 @@
+using System;
+using System.Globalization;
+using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Running;
+using MediaBrowser.Common;
+
+namespace Jellyfin.Common.Benches
+{
+ [MemoryDiagnoser]
+ public class HexDecodeBenches
+ {
+ private string _data;
+
+ [Params(0, 10, 100, 1000, 10000, 1000000)]
+ public int N { get; set; }
+
+ [GlobalSetup]
+ public void GlobalSetup()
+ {
+ var bytes = new byte[N];
+ new Random(42).NextBytes(bytes);
+ _data = Hex.Encode(bytes);
+ }
+
+ [Benchmark]
+ public byte[] Decode() => Hex.Decode(_data);
+
+ [Benchmark]
+ public byte[] DecodeSubString() => DecodeSubString(_data);
+
+ private static byte[] DecodeSubString(string str)
+ {
+ byte[] bytes = new byte[str.Length / 2];
+ for (int i = 0; i < str.Length; i += 2)
+ {
+ bytes[i / 2] = byte.Parse(
+ str.Substring(i, 2),
+ NumberStyles.HexNumber,
+ CultureInfo.InvariantCulture);
+ }
+
+ return bytes;
+ }
+ }
+}
diff --git a/benches/Jellyfin.Common.Benches/HexEncodeBenches.cs b/benches/Jellyfin.Common.Benches/HexEncodeBenches.cs
new file mode 100644
index 000000000..7abf93c51
--- /dev/null
+++ b/benches/Jellyfin.Common.Benches/HexEncodeBenches.cs
@@ -0,0 +1,32 @@
+using System;
+using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Running;
+using MediaBrowser.Common;
+
+namespace Jellyfin.Common.Benches
+{
+ [MemoryDiagnoser]
+ public class HexEncodeBenches
+ {
+ private byte[] _data;
+
+ [Params(0, 10, 100, 1000, 10000, 1000000)]
+ public int N { get; set; }
+
+ [GlobalSetup]
+ public void GlobalSetup()
+ {
+ _data = new byte[N];
+ new Random(42).NextBytes(_data);
+ }
+
+ [Benchmark]
+ public string HexEncode() => Hex.Encode(_data);
+
+ [Benchmark]
+ public string BitConverterToString() => BitConverter.ToString(_data);
+
+ [Benchmark]
+ public string BitConverterToStringWithReplace() => BitConverter.ToString(_data).Replace("-", "");
+ }
+}
diff --git a/benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj b/benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj
new file mode 100644
index 000000000..bea2e6f0f
--- /dev/null
+++ b/benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj
@@ -0,0 +1,16 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>netcoreapp3.0</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/benches/Jellyfin.Common.Benches/Program.cs b/benches/Jellyfin.Common.Benches/Program.cs
new file mode 100644
index 000000000..b218b0dc1
--- /dev/null
+++ b/benches/Jellyfin.Common.Benches/Program.cs
@@ -0,0 +1,14 @@
+using System;
+using BenchmarkDotNet.Running;
+
+namespace Jellyfin.Common.Benches
+{
+ public static class Program
+ {
+ public static void Main(string[] args)
+ {
+ _ = BenchmarkRunner.Run<HexEncodeBenches>();
+ _ = BenchmarkRunner.Run<HexDecodeBenches>();
+ }
+ }
+}