Skip to content

Commit

Permalink
Merge pull request #78 from OudomMunint/feature
Browse files Browse the repository at this point in the history
PR: Feature => Master
  • Loading branch information
OudomMunint authored Feb 1, 2025
2 parents 7bda7b3 + dd27a4b commit 2fd1c35
Show file tree
Hide file tree
Showing 16 changed files with 536 additions and 157 deletions.
53 changes: 45 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI Build (Multi-OS)
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]
workflow_dispatch:
Expand All @@ -12,16 +11,26 @@ permissions:
contents: write

env:
DOTNET_NOLOGO: true # Disable the .NET logo
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true # Disable the .NET first time experience
DOTNET_CLI_TELEMETRY_OPTOUT: true # Disable sending .NET CLI telemetry
DOTNET_NOLOGO: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true

jobs:
Build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-15]
os: [windows-latest, macos-15]
include:
- os: windows-latest
artifact_name: Benchmark_win_x64
publish_rid: win-x64
file_extension: zip

- os: macos-15
artifact_name: Benchmark_osx_arm64
publish_rid: osx-arm64
file_extension: tar.gz

steps:
- name: Checkout
Expand All @@ -35,11 +44,39 @@ jobs:
- name: Restore dependencies
run: dotnet restore Benchmark.csproj

- name: Build sln
- name: Build solution
run: dotnet build Benchmark.sln -c Release -f net9.0

- name: Publish application
run: dotnet publish Benchmark.csproj -c Release -r ${{ matrix.publish_rid }} --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:DebugType=None -o publish

- name: Package artifacts
shell: bash
run: |
if [[ "${{ matrix.file_extension }}" == "zip" ]]; then
7z a ${{ matrix.artifact_name }}.zip ./publish/*
else
tar -czvf ${{ matrix.artifact_name }}.tar.gz -C publish .
fi
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.artifact_name }}
path: ${{ matrix.artifact_name }}.${{ matrix.file_extension }}

Release:
needs: Build
runs-on: ubuntu-latest
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts

- name: Create Release
if: matrix.os == 'windows-latest'
uses: ncipollo/release-action@v1.14.0
with:
tag: v1.4.11
tag: v1.5.0
artifacts: "artifacts/**"
artifactContentType: application/octet-stream
13 changes: 13 additions & 0 deletions .idea/.idea.Benchmark/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net8.0/Benchmark.dll",
"program": "${workspaceFolder}/bin/Debug/net9.0/Benchmark.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
29 changes: 16 additions & 13 deletions Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<Version>1.5.0</Version>
<FileVersion>$([System.DateTime]::Now.ToString('yyyyMMddHH'))</FileVersion>
<Author>Dom</Author>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Hardware.Info" Version="101.0.0" />
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="SharpDX" Version="4.2.0" />
<PackageReference Include="SharpDX.Direct3D11" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Hardware.Info" Version="101.0.0" />
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="SharpDX" Version="4.2.0" />
<PackageReference Include="SharpDX.Direct3D11" Version="4.2.0" />
</ItemGroup>

</Project>
225 changes: 225 additions & 0 deletions Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Security.Cryptography;

public class HashingBenchmark
{
private const int N = 2000000000;
private readonly byte[] data;

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 byte[] Sha256() => sha256.ComputeHash(data);

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

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

public void CombinedHashing()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Running Hashing operation...");
Stopwatch stopwatch = Stopwatch.StartNew();
ConsoleSpinner.Start();

Sha256();
Sha512();
Md5();

stopwatch.Stop();
ConsoleSpinner.Stop();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Hasing completed in {stopwatch.ElapsedMilliseconds} ms.");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-----------------------------------------------------------");
}
}

public class EncryptionBenchmark
{
private const long TotalSize = 16L * 1_000_000_000; // 16GB
private const int ChunkSize = 100_000_000; // 100MB per operation
private const int Iterations = (int)(TotalSize / ChunkSize); // Number of chunks needed
private readonly byte[] dataChunk;
private readonly byte[] key;
private readonly byte[] iv;
private readonly Aes aes;

public EncryptionBenchmark()
{
aes = Aes.Create();
aes.KeySize = 256;
aes.GenerateKey();
aes.GenerateIV();

key = aes.Key;
iv = aes.IV;

dataChunk = new byte[ChunkSize];
new Random().NextBytes(dataChunk); // Generate random data once
}

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

public byte[] AesDecrypt(byte[] encryptedData)
{
using var decryptor = aes.CreateDecryptor(key, iv);
return decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
}

public void RunEncryptBenchmark()
{
int threadCount = Environment.ProcessorCount; // Match CPU core count
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Running AES-256 Encryption... processing {TotalSize / 1_000_000_000} GB with {threadCount} threads...");

Stopwatch stopwatch = Stopwatch.StartNew();
ConsoleSpinner.Start();

Parallel.For(0, threadCount, _ =>
{
for (int i = 0; i < Iterations / threadCount; i++)
{
byte[] encrypted = AesEncrypt(dataChunk);
byte[] decrypted = AesDecrypt(encrypted);
}
});

stopwatch.Stop();
ConsoleSpinner.Stop();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Encryption completed in {stopwatch.ElapsedMilliseconds} ms.");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-----------------------------------------------------------");
}
}

class CPUBenchmark
{
public static void CpuPrimeCompute()
{
int taskCount = Environment.ProcessorCount;
int iterations = 400_000_000;
int iterationsPerThread = iterations / taskCount;

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Running Prime Compute with {taskCount} threads...");

var options = new ParallelOptions
{
MaxDegreeOfParallelism = taskCount
};

ConsoleSpinner.Start();
Stopwatch stopwatch = Stopwatch.StartNew();

Parallel.For(0, taskCount, options, _ =>
{
ComputePrimes(iterationsPerThread);
});

stopwatch.Stop();
ConsoleSpinner.Stop();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Prime compute completed in {stopwatch.ElapsedMilliseconds} ms.");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-----------------------------------------------------------");
}

private static int ComputePrimes(int limit)
{
int count = 0;
for (int i = 2; i < limit; i++)
{
if (IsPrime(i))
count++;
}
return count;
}

private static bool IsPrime(int number)
{
if (number < 2) return false;
if (number % 2 == 0 && number != 2) return false;
for (int i = 3; i * i <= number; i += 2)
{
if (number % i == 0)
return false;
}
return true;
}
}

class MatrixMultiplicationBenchmark
{
private const int N = 2048; // Matrix size
private readonly double[,] matrixA;
private readonly double[,] matrixB;
private readonly double[,] result;

public MatrixMultiplicationBenchmark()
{
matrixA = new double[N, N];
matrixB = new double[N, N];
result = new double[N, N];

Random random = new Random(42);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
matrixA[i, j] = random.NextDouble() * 100;
matrixB[i, j] = random.NextDouble() * 100;
}
}
}

public void MultiplyMatrix()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Running Matrix Multiplication with {Environment.ProcessorCount} threads...");
ConsoleSpinner.Start();
Stopwatch stopwatch = Stopwatch.StartNew();

var options = new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};

Parallel.For(0, N, options, i =>
{
for (int j = 0; j < N; j++)
{
double sum = 0;
for (int k = 0; k < N; k++)
{
sum += matrixA[i, k] * matrixB[k, j];
}
result[i, j] = sum;
}
});

stopwatch.Stop();
ConsoleSpinner.Stop();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Matrix multiplication completed in {stopwatch.ElapsedMilliseconds} ms.");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-----------------------------------------------------------");
}
}
Loading

0 comments on commit 2fd1c35

Please sign in to comment.