XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.XUnit

【DLL】提供方便快捷的测试,无需编写Main方法,可直接添加特性在类或方法上进行测试

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using XFEExtension.NetCore.XUnit.Runtime;

namespace XFEExtension.NetCore.XUnit.Benchmarking;

internal static class EnvironmentInspector
{
    public static EnvironmentSnapshot Capture()
    {
        var processor = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER") ?? RuntimeInformation.ProcessArchitecture.ToString();
        var source = string.Join('|', RuntimeInformation.OSDescription, RuntimeInformation.FrameworkDescription,
            RuntimeInformation.ProcessArchitecture, processor, Environment.ProcessorCount, GCSettings.IsServerGC,
            Stopwatch.Frequency);
        var fingerprint = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(source)));
        return new EnvironmentSnapshot
        {
            OperatingSystem = RuntimeInformation.OSDescription,
            Framework = RuntimeInformation.FrameworkDescription,
            Architecture = RuntimeInformation.ProcessArchitecture.ToString(),
            Processor = processor,
            ProcessorCount = Environment.ProcessorCount,
            ServerGc = GCSettings.IsServerGC,
            StopwatchIsHighResolution = Stopwatch.IsHighResolution,
            StopwatchFrequency = Stopwatch.Frequency,
            Fingerprint = fingerprint
        };
    }

    public static IReadOnlyList<string> ValidateBenchmarkEnvironment()
    {
        var warnings = new List<string>();
        if (Debugger.IsAttached)
            warnings.Add("DebuggerAttached: attached debuggers make benchmark measurements unreliable.");
        var entry = Assembly.GetEntryAssembly();
        if (entry?.GetCustomAttribute<DebuggableAttribute>() is { IsJITOptimizerDisabled: true })
            warnings.Add("NonOptimizedAssembly: run benchmarks from a Release build.");
        if (!Stopwatch.IsHighResolution)
            warnings.Add("LowResolutionClock: Stopwatch is not using a high-resolution performance counter.");
        return warnings;
    }
}