Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

Refactor GPU info rendering in SystemInfoService

itsfatduckce658fcaeef8
diff --git a/optimizerDuck.Test/SystemInfoServiceTests.cs b/optimizerDuck.Test/SystemInfoServiceTests.cs
new file mode 100644
index 0000000..ca20c9e
--- /dev/null
+++ b/optimizerDuck.Test/SystemInfoServiceTests.cs
@@ -0,0 +1,156 @@
+using Spectre.Console;
+using optimizerDuck.Core.Services;
+
+namespace optimizerDuck.Test.Core.Services;
+
+public class SystemInfoServiceTests
+{
+ [Fact]
+ public void GetDetailedPanel_WithValidSnapshot_ReturnsPanel()
+ {
+ // Arrange
+ var snapshot = CreateValidSystemSnapshot();
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ Assert.IsType<Panel>(panel);
+ Assert.Equal(90, panel.Width);
+ Assert.Equal(BoxBorder.Rounded, panel.Border);
+ }
+
+ [Fact]
+ public void GetDetailedPanel_WithSingleGpu_RendersGpuInformation()
+ {
+ // Arrange
+ var gpu = new GpuInfo("NVIDIA RTX 3080", "537.13", GpuVendor.NVIDIA, 10240, "PCI\\VEN_10DE", "PCI_VEN_10DE");
+ var snapshot = CreateValidSystemSnapshot() with { Gpus = [gpu], PrimaryGpu = gpu };
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ }
+
+ [Fact]
+ public void GetDetailedPanel_WithMultipleGpus_RendersAllGpus()
+ {
+ // Arrange
+ var gpu1 = new GpuInfo("NVIDIA RTX 3080", "537.13", GpuVendor.NVIDIA, 10240, "PCI\\VEN_10DE", "PCI_VEN_10DE");
+ var gpu2 = new GpuInfo("Intel UHD Graphics", "27.20", GpuVendor.Intel, 1536, "PCI\\VEN_8086", "PCI_VEN_8086");
+ var snapshot = CreateValidSystemSnapshot() with { Gpus = [gpu1, gpu2], PrimaryGpu = gpu1 };
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ }
+
+ [Fact]
+ public void GetDetailedPanel_WithNoGpus_HandlesGracefully()
+ {
+ // Arrange
+ var snapshot = CreateValidSystemSnapshot() with { Gpus = [], PrimaryGpu = null };
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ }
+
+ [Fact]
+ public void GetDetailedPanel_WithUnknownGpu_HandlesGracefully()
+ {
+ // Arrange
+ var snapshot = CreateValidSystemSnapshot() with { Gpus = [GpuInfo.Unknown], PrimaryGpu = null };
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ }
+
+ [Fact]
+ public void GetDetailedPanel_WithHighMemoryUsage_ReturnsPanel()
+ {
+ // Arrange
+ var ram = new RamInfo(32, 32768, 33554432, 5.5, 85.0, []);
+ var snapshot = CreateValidSystemSnapshot() with { Ram = ram };
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ }
+
+ [Fact]
+ public void GetDetailedPanel_WithRamModules_RendersModuleDetails()
+ {
+ // Arrange
+ var module1 = new RamModule(16.0, "3200", "Corsair", "CMK16GX4M2B3200C16", "DIMM 1");
+ var module2 = new RamModule(16.0, "3200", "Kingston", "KF432C16BB2K2/32", "DIMM 2");
+ var ram = new RamInfo(32, 32768, 33554432, 16.0, 50.0, [module1, module2]);
+ var snapshot = CreateValidSystemSnapshot() with { Ram = ram };
+
+ // Act
+ var panel = SystemInfoService.GetDetailedPanel(snapshot);
+
+ // Assert
+ Assert.NotNull(panel);
+ }
+
+ private static SystemSnapshot CreateValidSystemSnapshot()
+ {
+ var cpu = new CpuInfo(
+ "Intel(R) Core(TM) i9-12900K",
+ "GenuineIntel",
+ "Intel",
+ "64-bit",
+ 16,
+ 24,
+ 5200,
+ 3200,
+ 8192,
+ 30720
+ );
+
+ var ram = new RamInfo(32, 32768, 33554432, 20.0, 50.0, []);
+
+ var os = new OsInfo(
+ "Windows 11",
+ "11",
+ "22621",
+ "Pro",
+ "64-bit",
+ "Desktop",
+ "2022-01-15",
+ "2025-11-03 10:30"
+ );
+
+ var bios = new BiosInfo(
+ "American Megatrends Inc.",
+ "F14",
+ "2022-01-10",
+ "3.4",
+ "ABCDEF123456"
+ );
+
+ var gpu = new GpuInfo(
+ "NVIDIA GeForce RTX 3080",
+ "537.13",
+ GpuVendor.NVIDIA,
+ 10240,
+ "PCI\\VEN_10DE",
+ "PCI_VEN_10DE"
+ );
+
+ return new SystemSnapshot(cpu, ram, os, bios, [gpu], gpu);
+ }
+}
\ No newline at end of file
diff --git a/optimizerDuck/src/Core/Extensions/TextExtensions.cs b/optimizerDuck/src/Core/Extensions/TextExtensions.cs
index 6e295fa..d148dd6 100644
--- a/optimizerDuck/src/Core/Extensions/TextExtensions.cs
+++ b/optimizerDuck/src/Core/Extensions/TextExtensions.cs
@@ -83,7 +83,7 @@ public static class TextExtensions
.ToList();
if (values.Count == 0)
- values.Add(doc.Root?.Value?.Trim() ?? string.Empty);
+ values.Add(doc.Root?.Value.Trim() ?? string.Empty);
return string.Join(Environment.NewLine, values)
.Replace("_x000D__x000A_", "\n")
diff --git a/optimizerDuck/src/Core/Services/SystemInfoService.cs b/optimizerDuck/src/Core/Services/SystemInfoService.cs
index 09dcbc5..856c500 100644
--- a/optimizerDuck/src/Core/Services/SystemInfoService.cs
+++ b/optimizerDuck/src/Core/Services/SystemInfoService.cs
@@ -608,7 +608,7 @@ public static class SystemInfoService
var memoryInfo = gpu.MemoryMB.HasValue
? $" [{gpu.MemoryMB.Value / 1024.0:F0} GB]"
: "";
- log.LogDebug("GPU ({GpuIndex:D4}): {GpuName} ({GpuVendor}){MemoryInfo}",
+ log.LogDebug("GPU ({GpuIndex}): {GpuName} ({GpuVendor}){MemoryInfo}",
index + 1, gpu.Name, gpu.Vendor, memoryInfo);
}
log.LogDebug("Total GPUs : {GpuCount}", gpuCount);
@@ -693,44 +693,17 @@ public static class SystemInfoService
var gpuCount = s.Gpus.Count;
var gpuGroup = new List<IRenderable>();
- if (gpuCount == 0 || (gpuCount == 1 && s.Gpus[0] == GpuInfo.Unknown))
+ switch (gpuCount)
{
- gpuGroup.Add(new Rule("GPU Information") { Style = new Style(Color.Red) });
- gpuGroup.Add(new Markup($"[{Theme.Error}]No GPUs detected[/]"));
- }
- else if (gpuCount == 1)
- {
- gpuGroup.Add(new Rule("GPU Information") { Style = new Style(Color.Green) });
- var gpu = s.Gpus[0];
- var vendorColor = gpu.Vendor switch
+ case 0:
+ case 1 when s.Gpus[0] == GpuInfo.Unknown:
+ gpuGroup.Add(new Rule("GPU Information") { Style = new Style(Color.Red) });
+ gpuGroup.Add(new Markup($"[{Theme.Error}]No GPUs detected[/]"));
+ break;
+ case 1:
{
- GpuVendor.NVIDIA => Theme.Success,
- GpuVendor.AMD => Theme.Error,
- GpuVendor.Intel => Theme.Accent,
- _ => Theme.Muted
- };
-
- gpuGroup.Add(new Markup($"[{Theme.Info}]Name :[/] [bold]{Safe(gpu.Name)}[/]"));
- gpuGroup.Add(new Markup($"[{Theme.Info}]Vendor :[/] [{vendorColor}]{gpu.Vendor}[/]"));
- gpuGroup.Add(new Markup($"[{Theme.Info}]Driver Version :[/] [{Theme.Warning}]{Safe(gpu.DriverVersion)}[/]"));
-
- if (gpu.MemoryMB.HasValue)
- gpuGroup.Add(new Markup(
- $"[{Theme.Info}]Memory :[/] [{Theme.Success}]{gpu.MemoryMB.Value}[/] [dim]MB[/] [dim]([/][{Theme.Success}]{gpu.MemoryMB.Value / 1024.0:F2}[/] [dim]GB)[/]"));
-
- if (!string.IsNullOrEmpty(gpu.DeviceId))
- gpuGroup.Add(new Markup($"[{Theme.Info}]Device ID :[/] [dim]{Safe(gpu.DeviceId)}[/]"));
-
- if (!string.IsNullOrEmpty(gpu.PnpDeviceId))
- gpuGroup.Add(new Markup($"[{Theme.Info}]PCI Device ID :[/] [dim]{Safe(gpu.PnpDeviceId)}[/]"));
- }
- else
- {
- gpuGroup.Add(new Rule($"GPU Information ({gpuCount} detected)") { Style = new Style(Color.Green) });
- for (var i = 0; i < s.Gpus.Count; i++)
- {
- var gpu = s.Gpus[i];
- var isPrimary = s.PrimaryGpu != null && gpu.Name == s.PrimaryGpu.Name ? " (Primary)" : "";
+ gpuGroup.Add(new Rule("GPU Information") { Style = new Style(Color.Green) });
+ var gpu = s.Gpus[0];
var vendorColor = gpu.Vendor switch
{
GpuVendor.NVIDIA => Theme.Success,
@@ -739,21 +712,54 @@ public static class SystemInfoService
_ => Theme.Muted
};
- gpuGroup.Add(new Markup($"[{Theme.Info}]GPU {i + 1}{isPrimary}:[/]"));
- gpuGroup.Add(new Markup($" [{Theme.Info}]Name :[/] [bold]{gpu.Name}[/]"));
- gpuGroup.Add(
- new Markup($" [{Theme.Info}]Vendor :[/] [{vendorColor}]{gpu.Vendor}[/{vendorColor}]"));
- gpuGroup.Add(new Markup($" [{Theme.Info}]Driver :[/] [{Theme.Warning}]{gpu.DriverVersion}[/]"));
+ gpuGroup.Add(new Markup($"[{Theme.Info}]Name :[/] [bold]{Safe(gpu.Name)}[/]"));
+ gpuGroup.Add(new Markup($"[{Theme.Info}]Vendor :[/] [{vendorColor}]{gpu.Vendor}[/]"));
+ gpuGroup.Add(new Markup($"[{Theme.Info}]Driver Version :[/] [{Theme.Warning}]{Safe(gpu.DriverVersion)}[/]"));
if (gpu.MemoryMB.HasValue)
gpuGroup.Add(new Markup(
- $" [{Theme.Info}]Memory :[/] [{Theme.Success}]{gpu.MemoryMB.Value}[/] [dim]MB[/] [dim]([/][{Theme.Success}]{gpu.MemoryMB.Value / 1024.0:F2}[/] [dim]GB)[/]"));
+ $"[{Theme.Info}]Memory :[/] [{Theme.Success}]{gpu.MemoryMB.Value}[/] [dim]MB[/] [dim]([/][{Theme.Success}]{gpu.MemoryMB.Value / 1024.0:F2}[/] [dim]GB)[/]"));
if (!string.IsNullOrEmpty(gpu.DeviceId))
- gpuGroup.Add(new Markup($" [{Theme.Info}]Device ID :[/] [dim]{gpu.DeviceId}[/]"));
+ gpuGroup.Add(new Markup($"[{Theme.Info}]Device ID :[/] [dim]{Safe(gpu.DeviceId)}[/]"));
if (!string.IsNullOrEmpty(gpu.PnpDeviceId))
- gpuGroup.Add(new Markup($" [{Theme.Info}]PCI Device ID:[/] [dim]{gpu.PnpDeviceId}[/]"));
+ gpuGroup.Add(new Markup($"[{Theme.Info}]PCI Device ID :[/] [dim]{Safe(gpu.PnpDeviceId)}[/]"));
+ break;
+ }
+ default:
+ {
+ gpuGroup.Add(new Rule($"GPU Information ({gpuCount} detected)") { Style = new Style(Color.Green) });
+ for (var i = 0; i < s.Gpus.Count; i++)
+ {
+ var gpu = s.Gpus[i];
+ var isPrimary = s.PrimaryGpu != null && gpu.Name == s.PrimaryGpu.Name ? " (Primary)" : "";
+ var vendorColor = gpu.Vendor switch
+ {
+ GpuVendor.NVIDIA => Theme.Success,
+ GpuVendor.AMD => Theme.Error,
+ GpuVendor.Intel => Theme.Accent,
+ _ => Theme.Muted
+ };
+
+ gpuGroup.Add(new Markup($"[{Theme.Info}]GPU {i + 1}{isPrimary}:[/]"));
+ gpuGroup.Add(new Markup($" [{Theme.Info}]Name :[/] [bold]{gpu.Name}[/]"));
+ gpuGroup.Add(
+ new Markup($" [{Theme.Info}]Vendor :[/] [{vendorColor}]{gpu.Vendor}[/]"));
+ gpuGroup.Add(new Markup($" [{Theme.Info}]Driver :[/] [{Theme.Warning}]{gpu.DriverVersion}[/]"));
+
+ if (gpu.MemoryMB.HasValue)
+ gpuGroup.Add(new Markup(
+ $" [{Theme.Info}]Memory :[/] [{Theme.Success}]{gpu.MemoryMB.Value}[/] [dim]MB[/] [dim]([/][{Theme.Success}]{gpu.MemoryMB.Value / 1024.0:F2}[/] [dim]GB)[/]"));
+
+ if (!string.IsNullOrEmpty(gpu.DeviceId))
+ gpuGroup.Add(new Markup($" [{Theme.Info}]Device ID :[/] [dim]{gpu.DeviceId}[/]"));
+
+ if (!string.IsNullOrEmpty(gpu.PnpDeviceId))
+ gpuGroup.Add(new Markup($" [{Theme.Info}]PCI Device ID:[/] [dim]{gpu.PnpDeviceId}[/]"));
+ }
+
+ break;
}
}
diff --git a/optimizerDuck/src/Core/Services/UpdateService.cs b/optimizerDuck/src/Core/Services/UpdateService.cs
index a7f700c..3c80ae8 100644
--- a/optimizerDuck/src/Core/Services/UpdateService.cs
+++ b/optimizerDuck/src/Core/Services/UpdateService.cs
@@ -173,7 +173,7 @@ public class UpdateService
Write-Host "Updater script finished."
""";
- var psi = new Process
+ var process = new Process
{
StartInfo = new ProcessStartInfo
{
@@ -184,7 +184,7 @@ public class UpdateService
}
};
- psi.Start();
+ process.Start();
Environment.Exit(0);
}