Skip to content

Commit

Permalink
Merge pull request #36 from OudomMunint/feature
Browse files Browse the repository at this point in the history
Feature > Master
  • Loading branch information
OudomMunint authored Mar 3, 2024
2 parents 590f2ef + a18103a commit 57990cf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ jobs:
if: matrix.os == 'windows-latest'
uses: ncipollo/[email protected]
with:
tag: v1.4.8.1
tag: v1.4.8.2
94 changes: 47 additions & 47 deletions MyBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,62 @@
using System.Security.Cryptography;
using System.Timers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace Benchmark
{
// [Config(typeof(SingleThreadPerCoreConfig))]
// 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 HashingBenchmark
{
private const int N = 1000000000;
private readonly byte[] data;
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();
private readonly SHA256 sha256 = SHA256.Create();
private readonly SHA512 sha512 = SHA512.Create();
private readonly MD5 md5 = MD5.Create();

public HashingBenchmark()
{
data = new byte[N];
new Random(42).NextBytes(data);
}
public HashingBenchmark()
{
data = new byte[N];
new Random(42).NextBytes(data);
}

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

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

[Benchmark]
public byte[] Md5() => md5.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 const int N = 1000000;
private readonly byte[] plainData;
private readonly byte[] key;

private readonly Aes aes = Aes.Create();
private readonly Aes aes = Aes.Create();

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

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

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

[Benchmark]
public byte[] AesDecrypt()
{
[Benchmark]
public byte[] AesDecrypt()
{
using var decryptor = aes.CreateDecryptor(key, aes.IV);
return decryptor.TransformFinalBlock(AesEncrypt(), 0, N);
}
Expand All @@ -80,32 +68,45 @@ public class MultithreadingBenchmark
private const int NumIterations = 1000000;
private readonly int[] array;

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

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

[Benchmark]
public void SingleThread()
{
//Console.WriteLine($"Benchmark started at: {startTime}");
for (int i = 0; i < array.Length; i++)
{
DoWork(i);
}
//Console.WriteLine($"Benchmark finished at: {endTime}");
//var TimeTaken = endTime - startTime;
//Console.WriteLine($"Total elapsed time: {TimeTaken}");
}

[Benchmark]
public void MultiThread()
{
//Console.WriteLine($"Benchmark started at: {startTime}");
var options = new ParallelOptions { MaxDegreeOfParallelism = NumThreads };
Parallel.For(0, array.Length, options, i =>
{
DoWork(i);
});
//Console.WriteLine($"Benchmark finished at: {endTime}");
//var TimeTaken = endTime - startTime;
//Console.WriteLine($"Total elapsed time: {TimeTaken}");
}

private static void DoWork(int index)
Expand All @@ -117,7 +118,6 @@ private static void DoWork(int index)
}
}


public class Program
{
public static void Main(string[] args)

Check warning on line 123 in MyBenchmark.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.

Check warning on line 123 in MyBenchmark.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.

Check warning on line 123 in MyBenchmark.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.

Check warning on line 123 in MyBenchmark.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.

Check warning on line 123 in MyBenchmark.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.

Check warning on line 123 in MyBenchmark.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.
Expand Down
42 changes: 28 additions & 14 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();

Check warning on line 31 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Converting null literal or possible null value to non-nullable type.

Check warning on line 31 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Converting null literal or possible null value to non-nullable type.

Check warning on line 31 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Converting null literal or possible null value to non-nullable type.

Check warning on line 31 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Converting null literal or possible null value to non-nullable type.

Check warning on line 31 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Converting null literal or possible null value to non-nullable type.

Check warning on line 31 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Converting null literal or possible null value to non-nullable type.
if (!line.Contains("Serial Number (system):") && !line.Contains("Hardware UUID:") &&!line.Contains("Model Number:") && !line.Contains("Provisioning UDID:") && !line.Contains("Boot Volume:")
if (!line.Contains("Serial Number (system):") && !line.Contains("Hardware UUID:") && !line.Contains("Model Number:") && !line.Contains("Provisioning UDID:") && !line.Contains("Boot Volume:")

Check warning on line 32 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 32 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Dereference of a possibly null reference.

Check warning on line 32 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Dereference of a possibly null reference.

Check warning on line 32 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Dereference of a possibly null reference.

Check warning on line 32 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 32 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Dereference of a possibly null reference.
&& !line.Contains("Boot Mode:") && !line.Contains("Computer Name:") && !line.Contains("User Name:"))
{
Console.WriteLine(line);
Expand Down Expand Up @@ -126,23 +126,37 @@
}
else
{
// shorten Advanced Micro Devices, Inc. to AMD
if (manufacturer.ToLower().Contains("advanced micro devices"))
{
manufacturer = "AMD";
}

Console.WriteLine("[Dedicated GPU]");
Console.WriteLine("Number of GPUs: {0}", searcher.Get().Count);

Check warning on line 136 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

This call site is reachable on all platforms. 'ManagementObjectSearcher.Get()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 136 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

This call site is reachable on all platforms. 'ManagementObjectSearcher.Get()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
Console.WriteLine("Name: {0}", item["Name"]);
Console.WriteLine("Manufacturer: {0}", manufacturer);
Console.WriteLine("Driver Version: {0}", item["DriverVersion"]);

Check warning on line 139 in Program.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

This call site is reachable on all platforms. 'ManagementBaseObject.this[string]' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
}
}

using (var factory = new Factory1())
{
using (var adapter = factory.GetAdapter(0))
{
var desc = adapter.Description;
Console.WriteLine("Dedicated GPU Memory", desc.DedicatedVideoMemory / (1024 * 1024));
Console.WriteLine("Shared GPU Memory: {0}MB", desc.SharedSystemMemory / (1024 * 1024));
Console.WriteLine("-----------------------------------------------------------");
using (var factory = new Factory1())
{
using (var adapter = factory.GetAdapter(0))
{
var desc = adapter.Description;

Console.WriteLine("Shared GPU Memory: {0}MB", desc.SharedSystemMemory / (1024 * 1024));
if (desc.DedicatedVideoMemory == 0)
{
Console.WriteLine("No dedicated GPU memory found");
}
else
{
Console.WriteLine("Dedicated GPU Memory: {0}MB", desc.DedicatedVideoMemory / (1024 * 1024));
}
Console.WriteLine("-----------------------------------------------------------");
}
}
}
}
}
}
Expand All @@ -151,8 +165,8 @@
Console.WriteLine("An error occurred while retrieving GPU information: " + ex.Message);
}

Console.Write("Continue to benchmark? (y/n): ");
var input = Console.ReadLine();
Console.Write("Continue to benchmark? (y/n): ");
var input = Console.ReadLine();

if (string.Equals(input, "y", StringComparison.OrdinalIgnoreCase))
{
Expand Down

0 comments on commit 57990cf

Please sign in to comment.