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 #63

Merged
merged 7 commits into from
Jul 6, 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/release-action@v1.14.0
with:
tag: v1.4.9.4
tag: v1.4.9.5
141 changes: 141 additions & 0 deletions ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Threading;

class Spinner : IDisposable
{
private const string Sequence = @"/-\|";
private int counter = 0;
private readonly int left;
private readonly int top;
private readonly int delay;
private bool active;
private readonly Thread thread;

public Spinner(int left, int top, int delay = 100)
{
this.left = left;
this.top = top;
this.delay = delay;
thread = new Thread(Spin);
}

public void Start()
{
active = true;
if (!thread.IsAlive)
thread.Start();
}

public void Stop()
{
active = false;
Draw(' ');
}

private void Spin()
{
while (active)
{
Turn();
Thread.Sleep(delay);
}
}

private void Draw(char c)
{
Console.SetCursorPosition(left, top);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
}

private void Turn()
{
Draw(Sequence[++counter % Sequence.Length]);
}

public void Dispose()
{
Stop();
}
}
class ConsoleSpinner
{
private static readonly string[] SpinnerFrames = { "/", "-", "\\", "|" };
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(0.3);
private static Thread? spinnerThread;
private static bool stopSpinner;

public static void Start()
{
Console.CursorVisible = false;
stopSpinner = false;
spinnerThread = new Thread(Spin);
spinnerThread.Start();
}

public static void Stop()
{
stopSpinner = true;
spinnerThread?.Join();
}

private static void Spin()
{
Console.CursorVisible = false;
int frameIndex = 0;
while (!stopSpinner)
{
Console.Write(SpinnerFrames[frameIndex]);
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
frameIndex = (frameIndex + 1) % SpinnerFrames.Length;
Thread.Sleep(Interval);
}
}
}

class ConsoleProgressBar
{
private const int ProgressBarWidth = 50;
private const char ProgressBarCharacter = '#';
private const char BackgroundCharacter = '-';

public static void DisplayProgressBar(int progressPercentage)
{
Console.CursorVisible = false;

int completedWidth = (int)Math.Round(progressPercentage / 100.0 * ProgressBarWidth);
int remainingWidth = ProgressBarWidth - completedWidth;

string progressBar = new string(ProgressBarCharacter, completedWidth) + new string(BackgroundCharacter, remainingWidth);

Console.Write("[{0}] {1}%", progressBar, progressPercentage);

Console.SetCursorPosition(0, Console.CursorTop);
}
}

class ConsoleIndeterminateBar
{
private const int ProgressBarWidth = 50;
private const char ProgressBarCharacter = '#';
private const char BackgroundCharacter = '-';

public static void DisplayProgressBar()
{
Console.CursorVisible = false;

while (true)
{
for (int i = 0; i <= ProgressBarWidth; i++)
{
Console.SetCursorPosition(i, Console.CursorTop);
Console.Write(ProgressBarCharacter);
Thread.Sleep(50);
}

Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(BackgroundCharacter, ProgressBarWidth + 1));
Console.SetCursorPosition(0, Console.CursorTop);
}
}
}
52 changes: 50 additions & 2 deletions MyBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
using System.Timers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using NvAPIWrapper;
using NvAPIWrapper.Display;
using NvAPIWrapper.GPU;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using Device = SharpDX.Direct3D11.Device;

namespace Benchmark
{
Expand Down Expand Up @@ -86,8 +92,11 @@ public void FullCpuLoad()
{
MaxDegreeOfParallelism = Environment.ProcessorCount,
};

Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)0xFF;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)0xFF;
}

Parallel.For(0, NumIterations, options, i =>
{
Expand All @@ -109,6 +118,45 @@ private void DoWork(int index)
}
}
}

public class GpuBenchmark
{
private const int NumIterations = 1000;
private Device? device;

[GlobalSetup]
public void Setup()
{
device = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.None);
}

[GlobalCleanup]
public void Cleanup()
{
device?.Dispose();
}

[Benchmark]
public void FullGpuLoad()
{
for (int i = 0; i < NumIterations; i++)
{
using var texture = new SharpDX.Direct3D11.Texture2D(device!, new SharpDX.Direct3D11.Texture2DDescription
{
Width = 1920,
Height = 1080,
ArraySize = 1,
BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget,
Usage = SharpDX.Direct3D11.ResourceUsage.Default,
CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
Format = Format.R8G8B8A8_UNorm,
MipLevels = 1,
OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
});
}
}
}

public class Program
{
Expand Down
11 changes: 8 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ static void DisplayMacInfo()

static void DisplayWindowsInfo()
{
ConsoleSpinner.Start();
DisplayCpuInfo();
DisplayRamInfo();
DisplayGpuInfo();
ConsoleSpinner.Stop();
}

static void DisplayCpuInfo()
static async void DisplayCpuInfo()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand Down Expand Up @@ -154,11 +156,12 @@ static void DisplayCpuInfo()
{
Console.WriteLine("An error occurred while retrieving CPU information: " + ex.Message);
}
await Task.Delay(500);
}
}
}

static void DisplayRamInfo()
static async void DisplayRamInfo()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand Down Expand Up @@ -211,10 +214,11 @@ static void DisplayRamInfo()
{
Console.WriteLine("An error occurred while retrieving memory information: " + ex.Message);
}
await Task.Delay(500);
}
}

static void DisplayGpuInfo()
static async void DisplayGpuInfo()
{
try
{
Expand Down Expand Up @@ -373,6 +377,7 @@ static void DisplayGpuInfo()
{
Console.WriteLine("An error occurred while retrieving GPU information: " + ex.Message);
}
await Task.Delay(500);
}

static void RunBenchmark()
Expand Down
Loading