Skip to content

Commit

Permalink
Merge pull request #6 from OudomMunint/feature
Browse files Browse the repository at this point in the history
PR#3: feature => master
  • Loading branch information
OudomMunint authored Jul 19, 2023
2 parents edc24e5 + 34c07cc commit d8d1012
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Install Node.js v16
- name: Install Node.js v18
uses: actions/setup-node@v2
with:
node-version: '18.x'
Expand All @@ -49,4 +49,4 @@ jobs:
if: matrix.os == 'windows-latest'
uses: ncipollo/[email protected]
with:
tag: v1.3.4
tag: v1.4.0
131 changes: 65 additions & 66 deletions MyBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,79 @@

namespace Benchmark
{
//[Config(typeof(SingleThreadPerCoreConfig))]
//public class MyBenchmark
//{
// private const int N = 1000000000;
// private readonly byte[] data;

// private readonly SHA256 sha256 = SHA256.Create();
// private readonly SHA512 sha512 = SHA512.Create();
// private readonly MD5 md5 = MD5.Create();

// public MyBenchmark()
// [Config(typeof(SingleThreadPerCoreConfig))]
// public class SingleThreadPerCoreConfig : ManualConfig
// {
// [Obsolete]
// public SingleThreadPerCoreConfig()
// {
// data = new byte[N];
// new Random(42).NextBytes(data);
// var job = Job.Default.WithRuntime(CoreRuntime.Core70).WithId("SingleThreadPerCore")
// .WithJit(Jit.RyuJit).WithGcServer(false).WithGcConcurrent(false)
// .WithEnvironmentVariable("COMPlus_EnablePreferredAffinity", "1")
// .WithEnvironmentVariable("COMPlus_ThreadPool_ForceMinWorkerThreads", "20");
// Add(job);
// }
// }
public class HashingBenchmark
{
private const int N = 1000000000;
private readonly byte[] data;

// [Benchmark]
// public byte[] Sha256() => sha256.ComputeHash(data);

// [Benchmark]
// public byte[] Sha512() => sha512.ComputeHash(data);

// [Benchmark]
// public byte[] Md5() => md5.ComputeHash(data);
//}

//public class EncryptionBenchmark
//{
// private const int N = 1000000;
// private readonly byte[] plainData;
// private readonly byte[] key;
private readonly SHA256 sha256 = SHA256.Create();
private readonly SHA512 sha512 = SHA512.Create();
private readonly MD5 md5 = MD5.Create();

// private readonly Aes aes = Aes.Create();
public HashingBenchmark()
{
data = new byte[N];
new Random(42).NextBytes(data);
}

// public EncryptionBenchmark()
// {
// plainData = new byte[N];
// key = new byte[aes.KeySize / 8];

// new Random(42).NextBytes(plainData);
// new Random(42).NextBytes(key);
// }
[Benchmark]
public byte[] Sha256() => sha256.ComputeHash(data);

// [Benchmark]
// public byte[] AesEncrypt()
// {
// using (var encryptor = aes.CreateEncryptor(key, aes.IV))
// {
// return encryptor.TransformFinalBlock(plainData, 0, plainData.Length);
// }
// }
[Benchmark]
public byte[] Sha512() => sha512.ComputeHash(data);

// [Benchmark]
// public byte[] AesDecrypt()
// {
// using (var decryptor = aes.CreateDecryptor(key, aes.IV))
// {
// return decryptor.TransformFinalBlock(AesEncrypt(), 0, N);
// }
// }
//}
[Benchmark]
public byte[] Md5() => md5.ComputeHash(data);
}

//public class SingleThreadPerCoreConfig : ManualConfig
//{
// [Obsolete]
// public SingleThreadPerCoreConfig()
// {
// //var job = Job.Default.WithRuntime(CoreRuntime.Core70).WithId("SingleThreadPerCore")
// // .WithJit(Jit.RyuJit).WithGcServer(false).WithGcConcurrent(false)
// // .WithEnvironmentVariable("COMPlus_EnablePreferredAffinity", "1")
// // .WithEnvironmentVariable("COMPlus_ThreadPool_ForceMinWorkerThreads", "20");
// //Add(job);
// }
//}
public class EncryptionBenchmark
{
private const int N = 1000000;
private readonly byte[] plainData;
private readonly byte[] key;

private readonly Aes aes = Aes.Create();

public EncryptionBenchmark()
{
plainData = new byte[N];
key = new byte[aes.KeySize / 8];

new Random(42).NextBytes(plainData);
new Random(42).NextBytes(key);
}

[Benchmark]
public byte[] AesEncrypt()
{
using (var encryptor = aes.CreateEncryptor(key, aes.IV))
{
return encryptor.TransformFinalBlock(plainData, 0, plainData.Length);
}
}

[Benchmark]
public byte[] AesDecrypt()
{
using (var decryptor = aes.CreateDecryptor(key, aes.IV))
{
return decryptor.TransformFinalBlock(AesEncrypt(), 0, N);
}
}
}

public class MultithreadingBenchmark
{
Expand Down
32 changes: 27 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// See https://aka.ms/new-console-template for more information
using Benchmark;
using Benchmark;
using BenchmarkDotNet.Running;
using System.Management;
using System.Diagnostics;
Expand Down Expand Up @@ -117,6 +116,29 @@
var input = Console.ReadLine();
if (input.ToLower() == "y")
{
// BenchmarkRunner.Run<MyBenchmark>();
BenchmarkRunner.Run<MultithreadingBenchmark>();
}

Console.WriteLine("Choose a benchmark to run:");
Console.WriteLine("1. Hashing Benchmark");
Console.WriteLine("2. Encryption Benchmark");
Console.WriteLine("3. Multithreading Benchmark");
Console.Write("Enter the number of your choice: ");

string choice = Console.ReadLine();

switch (choice)
{
case "1":
var hashingSummary = BenchmarkRunner.Run<HashingBenchmark>();
break;
case "2":
var encryptionSummary = BenchmarkRunner.Run<EncryptionBenchmark>();
break;
case "3":
var multithreadingSummary = BenchmarkRunner.Run<MultithreadingBenchmark>();
break;
default:
Console.WriteLine("Invalid choice.");
break;
}

}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Run the benchmark in `Release` mode.
- Check your if system specs is correct
- `Y` to continue
- Use 1, 2 or 3 to select which benchmarks to run
- For `VSCode` you will need to install the `C#` extention for vscode
- For `VSCode` you also need to create `launch.JSON` and `task.JSON` files if you want to run in different configurations.
- If not you can use the provided JSON files.
Expand Down Expand Up @@ -79,4 +80,4 @@ Intel Core i7-12800H CPU 1.80GHz (Alder Lake), 1 CPU, 20 logical and 14 physical
|------- |---------:|--------:|--------:|
| Sha256 | 192.9 ns | 3.86 ns | 5.28 ns |
| Sha512 | 449.0 ns | 8.52 ns | 7.97 ns |
| Md5 | 271.1 ns | 5.49 ns | 7.14 ns |
| Md5 | 271.1 ns | 5.49 ns | 7.14 ns |

0 comments on commit d8d1012

Please sign in to comment.