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

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Diagnostics;
using XFEToolBox.Client.Core.Search;

namespace XFEToolBox.Test;

public static class ColdFileIndexOptimizationTests
{
    [Test]
    public static void BroadQueriesReuseSnapshotStorageWithoutRetainingBorrowedResults()
    {
        const int count = 100_000;
        var index = new FileSearchIndex();
        index.AddOrUpdate(Enumerable.Range(0, count).Select(number => new FileSearchEntry($@"C:\pooled\file{number:D6}.txt", false)));
        var retained = index.Search("file000000").Items.Single();
        index.Search("*", limit: 0); // Warm the pool before measuring the steady-state path.
        var before = GC.GetAllocatedBytesForCurrentThread();
        for (var iteration = 0; iteration < 8; iteration++)
            Ensure(index.Search("*", limit: 0).TotalMatches == count, "池化快照返回了缓冲区尾部的旧条目。");
        var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
        Ensure(allocated < 2_000_000, $"连续宽查询仍在每次分配完整候选快照:{allocated:N0} 字节。");
        index.RemovePaths(index.GetEntriesSnapshot().Take(75_000).Select(entry => entry.FullPath));
        Ensure(index.Search("*", limit: 0).TotalMatches == 25_000, "清理后池化快照的实际存活数错误。");
        index.Clear();
        Ensure(index.Search("*", limit: 0).TotalMatches == 0 && retained.Entry.FullPath == @"C:\pooled\file000000.txt",
            "归还快照缓冲区改变了已返回结果的所有权。");
        Console.WriteLine($"池化宽查询:{count:N0} 项 × 8 次,累计分配 {allocated / 1000d:F1} KB。");
    }

    [Test]
    public static void DirectChildEntriesPreservePathsAndStableValueEquality()
    {
        foreach (var (parent, name) in new[]
                 {
                     (@"C:\", "root.txt"), (@"C:\Work", "中文.txt"),
                     (@"C:\Case Folder", "MiXeD.cs"), ("/tmp", "report.json"), ("/", "root.md")
                 })
        {
            var separator = parent.Contains('/') ? '/' : '\\';
            var fullPath = parent.EndsWith(separator) ? parent + name : parent + separator + name;
            var original = new FileSearchEntry(fullPath, false);
            var hashBeforeReading = original.GetHashCode();
            var direct = FileSearchEntry.FromDirectory(fullPath, false, parent, FileSearchEntry.NormalizePath(parent));
            Ensure(original == direct && hashBeforeReading == direct.GetHashCode(), "共享父路径改变了条目的值相等语义。");
            Ensure(original.Name == direct.Name && original.DirectoryPath == direct.DirectoryPath
                   && original.SearchDirectoryPath == direct.SearchDirectoryPath, "共享父路径改变了文件名或目录拼写。");
            Ensure(hashBeforeReading == original.GetHashCode(), "读取惰性目录属性改变了哈希值。");
            var index = new FileSearchIndex();
            index.AddOrUpdate([direct]);
            Ensure(index.Search(name).Items.Single().Entry == original, "直接子项加入索引后结果发生改变。");
            Ensure(ReferenceEquals(parent, index.GetEntriesSnapshot()[0].DirectoryPath), "索引丢弃了可信扫描器提供的父路径共享。");
        }
        foreach (var path in new[] { @"C:\", "/", @"C:\folder\\report.txt", "relative.txt", "C:/mixed/path/report.txt" })
        {
            var entry = new FileSearchEntry(path, false);
            Ensure(entry.SearchDirectoryPath == FileSearchEntry.NormalizePath(entry.DirectoryPath),
                "目录跨度优化改变了根路径或重复分隔符的规范化。");
        }
    }

    [Test]
    public static void RollingGramPostingsMatchRepeatedLettersAndSupplementaryUnicode()
    {
        var entries = new[]
        {
            new FileSearchEntry(@"C:\aaaAAA\aaaaaa000000.json", false),
            new FileSearchEntry(@"C:\aaaAAA\BAaaaAB.txt", false),
            new FileSearchEntry("C:\\Unicode\\\U00010428\U00010428.配置.json", false),
            new FileSearchEntry("C:\\Unicode\\\u0131\u017f\u212a.cs", false),
            new FileSearchEntry(@"C:\Greek\Δelta.txt", false)
        };
        var index = new FileSearchIndex();
        index.AddOrUpdate(entries);
        foreach (var query in new[] { "aa", "aaa", "AAAA", "000", "BAaaa", "\U00010400", "\U00010428", "配置", "δelta", "I", "K", "*", "***", "*.json", "!aaa | ext:cs" })
        {
            var parsed = FileSearchQuery.Parse(query);
            var expected = entries.Where(parsed.Matches).Select(entry => entry.FullPath).Order(StringComparer.OrdinalIgnoreCase).ToArray();
            var actual = index.Search(query, limit: entries.Length);
            Ensure(actual.TotalMatches == expected.Length
                && actual.Items.Select(match => match.Entry.FullPath).Order(StringComparer.OrdinalIgnoreCase).SequenceEqual(expected),
                $"滚动gram、重复ID去重或全匹配快速路径发生语义偏差:{query}。");
        }
    }

    [Test]
    public static void ColdBuildKeepsSelectiveAndCancelledBroadSearchResponsive()
    {
        const int total = 150_000;
        var index = new FileSearchIndex();
        index.AddOrUpdate([new(@"C:\priority\first_needle.txt", false)]);
        var writer = Task.Factory.StartNew(() => index.AddOrUpdate(Enumerable.Range(0, total).Select(number =>
            new FileSearchEntry($@"C:\cold\Group{number % 37}\repeated_aaaa_{number:D7}.json", false))),
            CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        var samples = new List<double>();
        var broadQueries = 0;
        while (!writer.IsCompleted)
        {
            var watch = Stopwatch.StartNew();
            Ensure(index.Search("first_needle").TotalMatches == 1, "后台构建期间已发布条目不可搜索。");
            samples.Add(watch.Elapsed.TotalMilliseconds);
            using var cancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
            try { index.Search("*", cancellationToken: cancellation.Token); }
            catch (OperationCanceledException) { }
            broadQueries++;
            Thread.Sleep(2);
        }
        writer.GetAwaiter().GetResult();
        Ensure(index.Count == total + 1 && samples.Count > 0 && broadQueries > 0, "并发搜索测试没有覆盖构建过程。");
        Ensure(samples.Max() < 2000, "选择性搜索长时间等待后台构建。");
        Ensure(index.Search("repeated_aaaa").TotalMatches == total, "重复gram导致索引条目丢失。");
        Console.WriteLine($"冷索引与搜索并发:{total:N0} 项,选择性查询最大 {samples.Max():F1} ms,宽查询取消 {broadQueries} 次。");
    }

    private static void Ensure(bool condition, string message)
    {
        if (!condition)
            throw new InvalidOperationException(message);
    }
}