From 50dd31a17d6dc989a6baf39103b2cb8d460a108b Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 11 Feb 2019 01:47:11 +0000 Subject: [PATCH] Add ReedSolomonBenchmark. --- Claunia.ReedSolomon.sln | 6 + ReedSolomonBenchmark/Program.cs | 14 + .../Properties/AssemblyInfo.cs | 35 +++ ReedSolomonBenchmark/ReedSolomonBenchmark.cs | 252 ++++++++++++++++++ .../ReedSolomonBenchmark.csproj | 60 +++++ 5 files changed, 367 insertions(+) create mode 100644 ReedSolomonBenchmark/Program.cs create mode 100644 ReedSolomonBenchmark/Properties/AssemblyInfo.cs create mode 100644 ReedSolomonBenchmark/ReedSolomonBenchmark.cs create mode 100644 ReedSolomonBenchmark/ReedSolomonBenchmark.csproj diff --git a/Claunia.ReedSolomon.sln b/Claunia.ReedSolomon.sln index e4c85f4..e302f9f 100644 --- a/Claunia.ReedSolomon.sln +++ b/Claunia.ReedSolomon.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Claunia.ReedSolomon", "Claunia.ReedSolomon\Claunia.ReedSolomon.csproj", "{4213781F-6943-446C-B7A4-4E93C3227389}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReedSolomonBenchmark", "ReedSolomonBenchmark\ReedSolomonBenchmark.csproj", "{D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {4213781F-6943-446C-B7A4-4E93C3227389}.Debug|Any CPU.Build.0 = Debug|Any CPU {4213781F-6943-446C-B7A4-4E93C3227389}.Release|Any CPU.ActiveCfg = Release|Any CPU {4213781F-6943-446C-B7A4-4E93C3227389}.Release|Any CPU.Build.0 = Release|Any CPU + {D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/ReedSolomonBenchmark/Program.cs b/ReedSolomonBenchmark/Program.cs new file mode 100644 index 0000000..78f091e --- /dev/null +++ b/ReedSolomonBenchmark/Program.cs @@ -0,0 +1,14 @@ +/** + * Benchmark of Reed-Solomon encoding. + * + * Copyright 2015, Backblaze, Inc. All rights reserved. + * Copyright © 2019 Natalia Portillo + */ + +namespace ReedSolomonBenchmark +{ + internal class Program + { + public static void Main(string[] args) => new ReedSolomonBenchmark().Run(); + } +} \ No newline at end of file diff --git a/ReedSolomonBenchmark/Properties/AssemblyInfo.cs b/ReedSolomonBenchmark/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a80a397 --- /dev/null +++ b/ReedSolomonBenchmark/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ReedSolomonBenchmark")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ReedSolomonBenchmark")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ReedSolomonBenchmark/ReedSolomonBenchmark.cs b/ReedSolomonBenchmark/ReedSolomonBenchmark.cs new file mode 100644 index 0000000..5ea9b89 --- /dev/null +++ b/ReedSolomonBenchmark/ReedSolomonBenchmark.cs @@ -0,0 +1,252 @@ +/** + * Benchmark of Reed-Solomon encoding. + * + * Copyright 2015, Backblaze, Inc. All rights reserved. + * Copyright © 2019 Natalia Portillo + */ + +using System; +using System.Collections.Generic; +using System.Text; +using Claunia.ReedSolomon; + +namespace ReedSolomonBenchmark +{ + internal sealed class ReedSolomonBenchmark + { + const int DATA_COUNT = 17; + const int PARITY_COUNT = 3; + const int TOTAL_COUNT = DATA_COUNT + PARITY_COUNT; + const int BUFFER_SIZE = 200 * 1000; + const int PROCESSOR_CACHE_SIZE = 10 * 1024 * 1024; + const int TWICE_PROCESSOR_CACHE_SIZE = 2 * PROCESSOR_CACHE_SIZE; + const int NUMBER_OF_BUFFER_SETS = TWICE_PROCESSOR_CACHE_SIZE / DATA_COUNT / BUFFER_SIZE + 1; + const long MEASUREMENT_DURATION = 2 * 1000; + + static readonly Random Random = new Random(); + + int nextBuffer; + + internal void Run() + { + Console.WriteLine("preparing..."); + BufferSet[] bufferSets = new BufferSet [NUMBER_OF_BUFFER_SETS]; + + for(int iBufferSet = 0; iBufferSet < NUMBER_OF_BUFFER_SETS; iBufferSet++) + bufferSets[iBufferSet] = new BufferSet(); + + byte[] tempBuffer = new byte [BUFFER_SIZE]; + + List summaryLines = new List(); + var csv = new StringBuilder(); + csv.Append("Outer,Middle,Inner,Multiply,Encode,Check\n"); + + foreach(ICodingLoop codingLoop in CodingLoopBase.ALL_CODING_LOOPS) + { + var encodeAverage = new Measurement(); + + { + string testName = codingLoop.GetType().Name + " encodeParity"; + Console.WriteLine("\nTEST: " + testName); + var codec = new ReedSolomon(DATA_COUNT, PARITY_COUNT, codingLoop); + Console.WriteLine(" warm up..."); + DoOneEncodeMeasurement(codec, bufferSets); + DoOneEncodeMeasurement(codec, bufferSets); + Console.WriteLine(" testing..."); + + for(int iMeasurement = 0; iMeasurement < 10; iMeasurement++) + encodeAverage.Add(DoOneEncodeMeasurement(codec, bufferSets)); + + Console.WriteLine("\nAVERAGE: {0}", encodeAverage); + summaryLines.Add($" {testName,-45} {encodeAverage}"); + } + + // The encoding test should have filled all of the buffers with + // correct parity, so we can benchmark parity checking. + var checkAverage = new Measurement(); + + { + string testName = codingLoop.GetType().Name + " isParityCorrect"; + Console.WriteLine("\nTEST: " + testName); + var codec = new ReedSolomon(DATA_COUNT, PARITY_COUNT, codingLoop); + Console.WriteLine(" warm up..."); + DoOneEncodeMeasurement(codec, bufferSets); + DoOneEncodeMeasurement(codec, bufferSets); + Console.WriteLine(" testing..."); + + for(int iMeasurement = 0; iMeasurement < 10; iMeasurement++) + checkAverage.Add(DoOneCheckMeasurement(codec, bufferSets, tempBuffer)); + + Console.WriteLine("\nAVERAGE: {0}", checkAverage); + summaryLines.Add($" {testName,-45} {checkAverage}"); + } + + csv.Append(CodingLoopNameToCsvPrefix(codingLoop.GetType().Name)); + csv.Append(encodeAverage.GetRate()); + csv.Append(","); + csv.Append(checkAverage.GetRate()); + csv.Append("\n"); + } + + Console.WriteLine("\n"); + Console.WriteLine(csv.ToString()); + + Console.WriteLine("\nSummary:\n"); + + foreach(string line in summaryLines) + Console.WriteLine(line); + } + + Measurement DoOneEncodeMeasurement(ReedSolomon codec, BufferSet[] bufferSets) + { + long passesCompleted = 0; + long bytesEncoded = 0; + long encodingTime = 0; + + while(encodingTime < MEASUREMENT_DURATION) + { + BufferSet bufferSet = bufferSets[nextBuffer]; + nextBuffer = (nextBuffer + 1) % bufferSets.Length; + byte[][] shards = bufferSet.Buffers; + DateTime startTime = DateTime.UtcNow; + codec.EncodeParity(shards, 0, BUFFER_SIZE); + DateTime endTime = DateTime.UtcNow; + encodingTime += (long)(endTime - startTime).TotalMilliseconds; + bytesEncoded += BUFFER_SIZE * DATA_COUNT; + passesCompleted += 1; + } + + double seconds = encodingTime / 1000.0; + double megabytes = bytesEncoded / 1000000.0; + var result = new Measurement(megabytes, seconds); + Console.WriteLine(" {0} passes, {1}", passesCompleted, result); + + return result; + } + + Measurement DoOneCheckMeasurement(ReedSolomon codec, BufferSet[] bufferSets, byte[] tempBuffer) + { + long passesCompleted = 0; + long bytesChecked = 0; + long checkingTime = 0; + + while(checkingTime < MEASUREMENT_DURATION) + { + BufferSet bufferSet = bufferSets[nextBuffer]; + nextBuffer = (nextBuffer + 1) % bufferSets.Length; + byte[][] shards = bufferSet.Buffers; + DateTime startTime = DateTime.UtcNow; + + if(!codec.IsParityCorrect(shards, 0, BUFFER_SIZE, tempBuffer)) + throw new Exception("parity not correct"); + + DateTime endTime = DateTime.UtcNow; + checkingTime += (long)(endTime - startTime).TotalMilliseconds; + bytesChecked += BUFFER_SIZE * DATA_COUNT; + passesCompleted += 1; + } + + double seconds = checkingTime / 1000.0; + double megabytes = bytesChecked / 1000000.0; + var result = new Measurement(megabytes, seconds); + Console.WriteLine(" {0} passes, {1}", passesCompleted, result); + + return result; + } + + /// Converts a name like "OutputByteInputTableCodingLoop" to "output,byte,input,table,". + static string CodingLoopNameToCsvPrefix(string className) + { + List names = SplitCamelCase(className); + + return names[0] + "," + names[1] + "," + names[2] + "," + names[3] + ","; + } + + /// + /// Converts a name like "OutputByteInputTableCodingLoop" to a List of words: { "output", "byte", "input", + /// "table", "coding", "loop" } + /// + /// + /// + static List SplitCamelCase(string className) + { + string remaining = className; + List result = new List(); + + while(!string.IsNullOrEmpty(remaining)) + { + bool found = false; + + for(int i = 1; i < remaining.Length; i++) + if(char.IsUpper(remaining[i])) + { + result.Add(remaining.Substring(0, i)); + remaining = remaining.Substring(i); + found = true; + + break; + } + + if(found) + continue; + + result.Add(remaining); + remaining = ""; + } + + return result; + } + + sealed class BufferSet + { + public readonly byte[][] Buffers; + + public BufferSet() + { + Buffers = new byte [TOTAL_COUNT][]; + + for(int iBuffer = 0; iBuffer < TOTAL_COUNT; iBuffer++) + { + Buffers[iBuffer] = new byte[BUFFER_SIZE]; + byte[] buffer = Buffers[iBuffer]; + + for(int iByte = 0; iByte < BUFFER_SIZE; iByte++) + buffer[iByte] = (byte)Random.Next(256); + } + + byte[] bigBuffer = new byte [TOTAL_COUNT * BUFFER_SIZE]; + + for(int i = 0; i < TOTAL_COUNT * BUFFER_SIZE; i++) + bigBuffer[i] = (byte)Random.Next(256); + } + } + + sealed class Measurement + { + double megabytes; + double seconds; + + public Measurement() + { + megabytes = 0.0; + seconds = 0.0; + } + + public Measurement(double megabytes, double seconds) + { + this.megabytes = megabytes; + this.seconds = seconds; + } + + public void Add(Measurement other) + { + megabytes += other.megabytes; + seconds += other.seconds; + } + + public double GetRate() => megabytes / seconds; + + public override string ToString() => $"{GetRate():F1} MB/s"; + } + } +} \ No newline at end of file diff --git a/ReedSolomonBenchmark/ReedSolomonBenchmark.csproj b/ReedSolomonBenchmark/ReedSolomonBenchmark.csproj new file mode 100644 index 0000000..570762f --- /dev/null +++ b/ReedSolomonBenchmark/ReedSolomonBenchmark.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {D29AB600-D6D9-464D-A0DE-C8CEE1FDE73C} + Exe + Properties + ReedSolomonBenchmark + ReedSolomonBenchmark + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + {4213781f-6943-446c-b7a4-4e93c3227389} + Claunia.ReedSolomon + + + + + +