Browse Source

Fix benches

Bond_009 5 years ago
parent
commit
60cb256835

+ 13 - 16
benches/Jellyfin.Common.Benches/HexDecodeBenches.cs

@@ -9,20 +9,26 @@ namespace Jellyfin.Common.Benches
     [MemoryDiagnoser]
     public class HexDecodeBenches
     {
-        [Params(/*0,*/ 10, 100, 1000, 10000, 1000000)]
-        public int N { get; set; }
+        private string _data;
 
-        private string data;
+        [Params(0, 10, 100, 1000, 10000, 1000000)]
+        public int N { get; set; }
 
         [GlobalSetup]
         public void GlobalSetup()
         {
-            var tmp = new byte[N];
-            new Random(42).NextBytes(tmp);
-            data = Hex.Encode(tmp);
+            var bytes = new byte[N];
+            new Random(42).NextBytes(bytes);
+            _data = Hex.Encode(bytes);
         }
 
-        public static byte[] DecodeSubString(string str)
+        [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)
@@ -35,14 +41,5 @@ namespace Jellyfin.Common.Benches
 
             return bytes;
         }
-
-        [Benchmark]
-        public byte[] Decode() => Hex.Decode(data);
-
-        [Benchmark]
-        public byte[] Decode2() => Hex.Decode2(data);
-
-        //[Benchmark]
-        public byte[] DecodeSubString() => DecodeSubString(data);
     }
 }

+ 11 - 8
benches/Jellyfin.Common.Benches/HexEncodeBenches.cs

@@ -8,22 +8,25 @@ namespace Jellyfin.Common.Benches
     [MemoryDiagnoser]
     public class HexEncodeBenches
     {
-        private const int N = 1000;
-        private readonly byte[] data;
+        private byte[] _data;
 
-        public HexEncodeBenches()
+        [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);
+            _data = new byte[N];
+            new Random(42).NextBytes(_data);
         }
 
         [Benchmark]
-        public string HexEncode() => Hex.Encode(data);
+        public string HexEncode() => Hex.Encode(_data);
 
         [Benchmark]
-        public string BitConverterToString() => BitConverter.ToString(data);
+        public string BitConverterToString() => BitConverter.ToString(_data);
 
         [Benchmark]
-        public string BitConverterToStringWithReplace() => BitConverter.ToString(data).Replace("-", "");
+        public string BitConverterToStringWithReplace() => BitConverter.ToString(_data).Replace("-", "");
     }
 }

+ 1 - 1
benches/Jellyfin.Common.Benches/Program.cs

@@ -7,7 +7,7 @@ namespace Jellyfin.Common.Benches
     {
         public static void Main(string[] args)
         {
-            //_ = BenchmarkRunner.Run<HexEncodeBenches>();
+            _ = BenchmarkRunner.Run<HexEncodeBenches>();
             _ = BenchmarkRunner.Run<HexDecodeBenches>();
         }
     }