Skip to content

Commit

Permalink
Merge pull request #59 from OudomMunint/feature
Browse files Browse the repository at this point in the history
PR: feature => master
  • Loading branch information
OudomMunint authored Jun 28, 2024
2 parents 9956784 + 353bd91 commit fd3c774
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 321 deletions.
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

0 comments on commit fd3c774

Please sign in to comment.