Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: feature => master #59

Merged
merged 8 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
if: matrix.os == 'windows-latest'
uses: ncipollo/[email protected]
with:
tag: v1.4.9.3
tag: v1.4.9.4
74 changes: 32 additions & 42 deletions MyBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System.Threading;
using System.Timers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
Expand All @@ -20,14 +21,19 @@ public HashingBenchmark()
new Random(42).NextBytes(data);
}

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

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

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

[Benchmark]
public void CombinedHashing()
{
Sha256();
Sha512();
Md5();
}
}

public class EncryptionBenchmark
Expand All @@ -47,74 +53,58 @@ public EncryptionBenchmark()
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
{
private const int NumThreads = 8;
private const int NumIterations = 1000000;
private readonly int[] array;

private DateTime startTime = DateTime.Now;
private DateTime endTime = DateTime.Now;

public MultithreadingBenchmark()
[Benchmark]
public void CombinedEncryption()
{
array = new int[NumIterations];
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
SingleThread();
MultiThread();
AesEncrypt();
AesDecrypt();
}
}

public class CPUBenchmark
{
private const int NumIterations = 1000;

[Benchmark]
public void SingleThread()
public void FullCpuLoad()
{
for (int i = 0; i < array.Length; i++)
var options = new ParallelOptions
{
DoWork(i);
}
}
MaxDegreeOfParallelism = Environment.ProcessorCount,
};

[Benchmark]
public void MultiThread()
{
var options = new ParallelOptions { MaxDegreeOfParallelism = NumThreads };
Parallel.For(0, array.Length, options, i =>
Parallel.For(0, NumIterations, options, i =>
{
DoWork(i);
});
}

private static void DoWork(int index)
private void DoWork(int index)
{
// simulate some work
var result = Math.Pow(index, 2);
result = Math.Pow(result, 2);
_ = Math.Pow(result, 2);
Thread.CurrentThread.Priority = ThreadPriority.Normal;

double result = 1;
for (int i = 1; i <= 100; i++)
{
result = Math.Sin(index * result) * Math.Tan(index * result);
}
}
}

public class Program
{
// public static void Main(string[] args)
// {
// _ = BenchmarkRunner.Run<MultithreadingBenchmark>();
// }

}
}
Loading
Loading