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

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text.Json;
using XFEToolBox.Client.Core.Search.Service;

namespace XFEToolBox.Indexing.LiveValidation;

internal static class Program
{
    public static async Task<int> Main(string[] args)
    {
        if (args.Contains("--verify-fixture", StringComparer.Ordinal))
            return await FixtureVerifier.RunAsync(args).ConfigureAwait(false);
        Options options;
        try { options = Options.Parse(args); }
        catch (ArgumentException error) { Console.Error.WriteLine(error.Message); PrintUsage(); return 2; }
        if (options.Help) { PrintUsage(); return 0; }
        if (!OperatingSystem.IsWindows()) { Console.Error.WriteLine("Windows only."); return 2; }
        using var identity = WindowsIdentity.GetCurrent();
        if (new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator))
        {
            Console.Error.WriteLine("Run this validation in an ordinary, non-elevated terminal.");
            return 2;
        }
        try
        {
            // CreateNew prevents accidental replacement of any previous measurements.
            await using var output = new FileStream(options.Output!, FileMode.CreateNew, FileAccess.Write, FileShare.Read,
                16 * 1024, FileOptions.Asynchronous);
            await using var writer = new StreamWriter(output);
            var log = new JsonLines(writer);
            using var stop = new CancellationTokenSource(TimeSpan.FromSeconds(options.DurationSeconds));
            Console.CancelKeyPress += (_, eventArgs) => { eventArgs.Cancel = true; _ = stop.CancelAsync(); };
            await log.WriteAsync(new
            {
                type = "begin", timestampUtc = DateTimeOffset.UtcNow, phase = options.Phase,
                clientPid = Environment.ProcessId, elevated = false, processorCount = Environment.ProcessorCount,
                durationSeconds = options.DurationSeconds, limit = options.Limit,
                queryIntervalMs = options.IntervalMilliseconds, queries = options.Queries, scope = options.Scope
            });
            var client = new IndexingServiceClient();
            var summary = new Measurements();
            var wall = Stopwatch.StartNew();
            await Task.WhenAll(StatusLoopAsync(client, options, log, summary, stop.Token),
                SearchLoopAsync(client, options, log, summary, stop.Token));
            await log.WriteAsync(new
            {
                type = "end", timestampUtc = DateTimeOffset.UtcNow, phase = options.Phase,
                wallMs = wall.Elapsed.TotalMilliseconds, statusSamples = summary.StatusSamples,
                searchRequests = summary.Requests, successfulSearches = summary.Successes,
                unavailableSearches = summary.Unavailable, likelyTimeouts = summary.Timeouts,
                cancelledSearches = summary.Cancelled, errors = summary.Errors,
                wallMedianMs = Percentile(summary.WallLatencies, 0.5),
                wallP95Ms = Percentile(summary.WallLatencies, 0.95),
                serverMedianMs = Percentile(summary.ServerLatencies, 0.5),
                serverP95Ms = Percentile(summary.ServerLatencies, 0.95)
            });
            Console.WriteLine($"Completed phase '{options.Phase}': {summary.StatusSamples} status samples, " +
                $"{summary.Requests} searches, {summary.Successes} successful, {summary.Timeouts} likely timeouts. JSONL saved.");
            return 0;
        }
        catch (Exception error) when (error is IOException or UnauthorizedAccessException)
        {
            // Avoid dumping exception messages containing paths to the console.
            Console.Error.WriteLine("Cannot create/write the requested new output file: " + error.GetType().Name);
            return 3;
        }
    }

    private static async Task StatusLoopAsync(IndexingServiceClient client, Options options, JsonLines log,
        Measurements summary, CancellationToken token)
    {
        var process = new ProcessSampler();
        var next = Stopwatch.StartNew();
        var sample = 0;
        while (!token.IsCancellationRequested)
        {
            var watch = Stopwatch.StartNew();
            IndexingServiceStatus? status = null;
            string? error = null;
            try { status = await client.GetStatusAsync(token).ConfigureAwait(false); }
            catch (Exception exception) { error = exception.GetType().Name; Interlocked.Increment(ref summary.Errors); }
            var usage = process.Read();
            Interlocked.Increment(ref summary.StatusSamples);
            await log.WriteAsync(new
            {
                type = "status", timestampUtc = DateTimeOffset.UtcNow, phase = options.Phase,
                sample = sample++, elapsedMs = next.Elapsed.TotalMilliseconds,
                probeWallMs = watch.Elapsed.TotalMilliseconds, available = status is not null, error,
                isIndexing = status?.IsIndexing, volumes = status?.Volumes, readyVolumes = status?.ReadyVolumes,
                coveredRootCount = status?.IndexedRoots.Count, unavailableRootCount = status?.UnavailableRoots.Count,
                hasServiceError = status?.Error is not null,
                servicePid = usage?.Pid, serviceCpuTotalMs = usage?.TotalCpuMs,
                serviceCpuMachinePercent = usage?.MachinePercent, serviceCpuCorePercent = usage?.CorePercent,
                serviceWorkingSetMiB = usage?.WorkingSetMiB
            });
            // Fixed one-second target, without queuing extra probes after a slow one.
            var dueMs = sample * 1000d - next.Elapsed.TotalMilliseconds;
            if (dueMs > 0 && !await DelayAsync(TimeSpan.FromMilliseconds(dueMs), token)) return;
        }
    }

    private static async Task SearchLoopAsync(IndexingServiceClient client, Options options, JsonLines log,
        Measurements summary, CancellationToken token)
    {
        var queryIndex = 0;
        while (!token.IsCancellationRequested)
        {
            var query = options.Queries[queryIndex++ % options.Queries.Count];
            var watch = Stopwatch.StartNew();
            IndexingServiceSearchResponse? result = null;
            string? error = null;
            try { result = await client.SearchAsync(query, options.Scope, options.Limit, token).ConfigureAwait(false); }
            catch (Exception exception) { error = exception.GetType().Name; Interlocked.Increment(ref summary.Errors); }
            var elapsed = watch.Elapsed.TotalMilliseconds;
            var cancelled = token.IsCancellationRequested;
            // IPC intentionally returns null for all unavailable conditions. Preserve
            // that distinction: this threshold is only a likely timeout, not proof.
            var likelyTimeout = result is null && !cancelled && elapsed >= 3500;
            var outcome = cancelled ? "cancelled" : result is null ? likelyTimeout ? "likely_timeout" : "unavailable"
                : result.Error is not null ? "query_error" : "ok";
            Interlocked.Increment(ref summary.Requests);
            if (cancelled) Interlocked.Increment(ref summary.Cancelled);
            else if (likelyTimeout) Interlocked.Increment(ref summary.Timeouts);
            else if (result is null) Interlocked.Increment(ref summary.Unavailable);
            else if (result.Error is not null) Interlocked.Increment(ref summary.Errors);
            else
            {
                Interlocked.Increment(ref summary.Successes);
                summary.WallLatencies.Add(elapsed);
                summary.ServerLatencies.Add(result.Elapsed.TotalMilliseconds);
            }
            await log.WriteAsync(new
            {
                type = "search", timestampUtc = DateTimeOffset.UtcNow, phase = options.Phase,
                sequence = summary.Requests, query, limit = options.Limit, outcome,
                wallMs = elapsed, serverMs = result?.Elapsed.TotalMilliseconds,
                resultCount = result?.Items.Length, hasMore = result?.HasMore,
                isIndexing = result?.Status.IsIndexing, readyVolumes = result?.Status.ReadyVolumes,
                volumes = result?.Status.Volumes, error
            });
            // Never serialize the returned FileSearchMatch objects or their paths.
            var pause = Math.Max(0, options.IntervalMilliseconds - elapsed);
            if (pause > 0 && !await DelayAsync(TimeSpan.FromMilliseconds(pause), token)) return;
        }
    }

    private static async Task<bool> DelayAsync(TimeSpan duration, CancellationToken token)
    {
        try { await Task.Delay(duration, token).ConfigureAwait(false); return true; }
        catch (OperationCanceledException) when (token.IsCancellationRequested) { return false; }
    }

    private static double? Percentile(List<double> values, double fraction)
    {
        if (values.Count == 0) return null;
        var sorted = values.Order().ToArray();
        return sorted[(int)Math.Clamp(Math.Ceiling(sorted.Length * fraction) - 1, 0, sorted.Length - 1)];
    }

    private static void PrintUsage() => Console.WriteLine(
        "Indexing.LiveValidation --output <new-existing-parent-dir/file.jsonl> --phase <name> " +
        "[--duration-seconds 60] [--limit 100|1000] [--interval-ms 1000] [--scope <local-folder>] [--query \"*.txt\"] [--query \"*\"]\n" +
        "Indexing.LiveValidation --verify-fixture <local-folder> --fixture-id <GUID-N> --fixture-phase before|after\n" +
        "Read-only ordinary-client service validation. Does not install, stop, rebuild or scan volumes.\n" +
        "Output file must not already exist; its parent directory must exist. Ctrl+C ends cleanly.");

    private sealed class JsonLines(StreamWriter writer)
    {
        private readonly SemaphoreSlim gate = new(1, 1);
        public async Task WriteAsync<T>(T value)
        {
            var line = JsonSerializer.Serialize(value);
            await gate.WaitAsync().ConfigureAwait(false);
            try { await writer.WriteLineAsync(line).ConfigureAwait(false); await writer.FlushAsync().ConfigureAwait(false); }
            finally { gate.Release(); }
        }
    }

    private sealed class Measurements
    {
        public int StatusSamples, Requests, Successes, Unavailable, Timeouts, Cancelled, Errors;
        public readonly List<double> WallLatencies = [], ServerLatencies = [];
    }

    private sealed record Options(string? Output, string Phase, int DurationSeconds, int Limit,
        int IntervalMilliseconds, IReadOnlyList<string> Queries, string? Scope = null, bool Help = false)
    {
        public static Options Parse(string[] args)
        {
            string? output = null;
            string? scope = null;
            var phase = "measurement";
            var duration = 60;
            var limit = 100;
            var interval = 1000;
            var queries = new List<string>();
            for (var index = 0; index < args.Length; index++)
            {
                var option = args[index];
                if (option is "--help" or "-h") return new(null, phase, duration, limit, interval, [], null, true);
                if (++index >= args.Length) throw new ArgumentException("Every option requires a value.");
                var value = args[index];
                switch (option)
                {
                    case "--output": output = Path.GetFullPath(value); break;
                    case "--phase": phase = value; break;
                    case "--scope":
                        scope = Path.GetFullPath(value);
                        if (!IndexingServiceProtocol.IsLocalPath(scope)) throw new ArgumentException("Scope must be a local drive path.");
                        break;
                    case "--duration-seconds": duration = ParseInt(value, 1, 7200); break;
                    case "--limit": limit = ParseInt(value, 1, 1000); break;
                    case "--interval-ms": interval = ParseInt(value, 100, 60_000); break;
                    case "--query":
                        if (value.Length > IndexingServiceProtocol.MaximumQueryLength) throw new ArgumentException("Query exceeds protocol length limit.");
                        queries.Add(value); break;
                    default: throw new ArgumentException("Unknown option (use --help).");
                }
            }
            if (output is null) throw new ArgumentException("--output is required.");
            if (phase.Length is < 1 or > 100) throw new ArgumentException("Phase must contain 1–100 characters.");
            if (queries.Count == 0) queries.AddRange(["*.txt", "*.cs", "报告", "folder:", "*"]);
            if (queries.Count > 100) throw new ArgumentException("At most 100 queries are allowed.");
            return new(output, phase, duration, limit, interval, queries, scope);
        }

        private static int ParseInt(string value, int min, int max) => int.TryParse(value, out var number) && number >= min && number <= max
            ? number : throw new ArgumentException($"Numeric argument must be in {min}–{max}.");
    }

    private sealed record ProcessUsage(uint Pid, double? TotalCpuMs, double? MachinePercent, double? CorePercent, double? WorkingSetMiB);

    private sealed class ProcessSampler
    {
        private uint lastPid;
        private double lastCpuMs;
        private long lastTime;
        public ProcessUsage? Read()
        {
            var pid = GetServicePid();
            if (pid == 0) return null;
            double? cpu = null, workingSet = null, core = null;
            try
            {
                using var process = Process.GetProcessById((int)pid);
                try { cpu = process.TotalProcessorTime.TotalMilliseconds; } catch (Exception error) when (error is System.ComponentModel.Win32Exception or InvalidOperationException) { }
                try { workingSet = process.WorkingSet64 / 1048576d; } catch (Exception error) when (error is System.ComponentModel.Win32Exception or InvalidOperationException) { }
            }
            catch (Exception error) when (error is ArgumentException or InvalidOperationException or System.ComponentModel.Win32Exception) { }
            var now = Stopwatch.GetTimestamp();
            if (cpu is not null && pid == lastPid && lastTime != 0)
            {
                var elapsed = Stopwatch.GetElapsedTime(lastTime, now).TotalMilliseconds;
                if (elapsed > 0) core = Math.Max(0, (cpu.Value - lastCpuMs) / elapsed * 100);
            }
            if (cpu is not null) { lastPid = pid; lastCpuMs = cpu.Value; lastTime = now; }
            return new(pid, cpu, core / Environment.ProcessorCount, core, workingSet);
        }

        private static uint GetServicePid()
        {
            var manager = OpenSCManagerW(null, null, 1);
            if (manager == IntPtr.Zero) return 0;
            try
            {
                var service = OpenServiceW(manager, IndexingServiceProtocol.ServiceName, 4);
                if (service == IntPtr.Zero) return 0;
                try
                {
                    return QueryServiceStatusEx(service, 0, out var status, Marshal.SizeOf<ServiceStatus>(), out _)
                        && status.State is 4 or 2 ? status.Pid : 0;
                }
                finally { CloseServiceHandle(service); }
            }
            finally { CloseServiceHandle(manager); }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct ServiceStatus
        {
            public uint Type, State, ControlsAccepted, Win32ExitCode, ServiceExitCode, Checkpoint, WaitHint, Pid, Flags;
        }
        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern IntPtr OpenSCManagerW(string? machine, string? database, uint access);
        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern IntPtr OpenServiceW(IntPtr manager, string name, uint access);
        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)] private static extern bool QueryServiceStatusEx(IntPtr service, int level, out ServiceStatus status, int length, out uint needed);
        [DllImport("advapi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)] private static extern bool CloseServiceHandle(IntPtr handle);
    }
}