|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using NUnit.Framework; |
| 7 | +using UnityEngine; |
| 8 | + |
| 9 | +namespace Framework |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Utility class for analyzing Asset Import Worker log files to verify which workers |
| 13 | + /// are processing specific asset import requests. |
| 14 | + /// </summary> |
| 15 | + public static class WorkerLogAnalyzer |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Represents a snapshot of import counts across all Asset Import Workers for a specific asset. |
| 19 | + /// </summary> |
| 20 | + public class WorkerLogSnapshot |
| 21 | + { |
| 22 | + public Dictionary<int, int> WorkerImportCounts { get; private set; } = new Dictionary<int, int>(); |
| 23 | + |
| 24 | + public void AddWorkerImportCount(int workerId, int count) |
| 25 | + { |
| 26 | + WorkerImportCounts[workerId] = count; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Calculates the difference between this snapshot and a baseline snapshot, |
| 31 | + /// returning only the new imports that occurred since the baseline. |
| 32 | + /// </summary> |
| 33 | + /// <param name="baseline">The baseline snapshot to compare against</param> |
| 34 | + /// <returns>A new snapshot containing only the import differences</returns> |
| 35 | + public WorkerLogSnapshot GetDifference(WorkerLogSnapshot baseline) |
| 36 | + { |
| 37 | + var diff = new WorkerLogSnapshot(); |
| 38 | + foreach (var kvp in WorkerImportCounts) |
| 39 | + { |
| 40 | + int baselineCount = baseline.WorkerImportCounts.GetValueOrDefault(kvp.Key, 0); |
| 41 | + int newImports = kvp.Value - baselineCount; |
| 42 | + if (newImports > 0) |
| 43 | + { |
| 44 | + diff.AddWorkerImportCount(kvp.Key, newImports); |
| 45 | + } |
| 46 | + } |
| 47 | + return diff; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets a summary string of the worker import counts for debugging. |
| 52 | + /// </summary> |
| 53 | + public string GetSummary() |
| 54 | + { |
| 55 | + if (WorkerImportCounts.Count == 0) |
| 56 | + return "No workers found"; |
| 57 | + |
| 58 | + return string.Join(", ", WorkerImportCounts.Select(kvp => $"Worker{kvp.Key}:{kvp.Value}")); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Creates a snapshot of the current import counts for the specified asset across all Asset Import Workers. |
| 64 | + /// </summary> |
| 65 | + /// <param name="assetPath">The asset path to look for in the worker logs</param> |
| 66 | + /// <returns>A snapshot of current import counts</returns> |
| 67 | + public static WorkerLogSnapshot SnapshotWorkerLogs(string assetPath) |
| 68 | + { |
| 69 | + var snapshot = new WorkerLogSnapshot(); |
| 70 | + var logsDirectory = Path.Combine(Application.dataPath, "../Logs"); |
| 71 | + |
| 72 | + if (!Directory.Exists(logsDirectory)) |
| 73 | + { |
| 74 | + Debug.LogWarning($"Logs directory not found at: {logsDirectory}"); |
| 75 | + return snapshot; |
| 76 | + } |
| 77 | + |
| 78 | + var logFiles = Directory.GetFiles(logsDirectory, "AssetImportWorker*.log") |
| 79 | + .Where(f => !Path.GetFileName(f).Contains("-prev")) // Ignore previous run logs |
| 80 | + .ToArray(); |
| 81 | + |
| 82 | + Debug.Log($"Found {logFiles.Length} current worker log files in {logsDirectory}"); |
| 83 | + |
| 84 | + foreach (var logFile in logFiles) |
| 85 | + { |
| 86 | + var workerId = ExtractWorkerIdFromFilename(logFile); |
| 87 | + if (workerId >= 0) |
| 88 | + { |
| 89 | + int importCount = CountAssetImportsInLog(logFile, assetPath); |
| 90 | + snapshot.AddWorkerImportCount(workerId, importCount); |
| 91 | + Debug.Log($"Worker{workerId}: Found {importCount} imports for {assetPath}"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return snapshot; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Extracts the worker ID from an Asset Import Worker log filename. |
| 100 | + /// </summary> |
| 101 | + /// <param name="logFilePath">Path to the log file</param> |
| 102 | + /// <returns>Worker ID, or -1 if not found</returns> |
| 103 | + private static int ExtractWorkerIdFromFilename(string logFilePath) |
| 104 | + { |
| 105 | + var filename = Path.GetFileName(logFilePath); |
| 106 | + var match = Regex.Match(filename, @"AssetImportWorker(\d+)\.log"); |
| 107 | + return match.Success ? int.Parse(match.Groups[1].Value) : -1; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Counts the number of import requests for the specified asset in a worker log file. |
| 112 | + /// Looks for the pattern: |
| 113 | + /// ======================================================================== |
| 114 | + /// Received Import Request. |
| 115 | + /// Time since last request: XX.XXXXXX seconds. |
| 116 | + /// path: [assetPath] |
| 117 | + /// </summary> |
| 118 | + /// <param name="logFilePath">Path to the worker log file</param> |
| 119 | + /// <param name="assetPath">The asset path to search for</param> |
| 120 | + /// <returns>Number of import requests found</returns> |
| 121 | + private static int CountAssetImportsInLog(string logFilePath, string assetPath) |
| 122 | + { |
| 123 | + if (!File.Exists(logFilePath)) |
| 124 | + return 0; |
| 125 | + |
| 126 | + int count = 0; |
| 127 | + try |
| 128 | + { |
| 129 | + // Use FileStream with FileShare.ReadWrite to allow reading while the worker has the file open for writing |
| 130 | + using (var fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 131 | + using (var reader = new StreamReader(fileStream)) |
| 132 | + { |
| 133 | + var lines = new List<string>(); |
| 134 | + string line; |
| 135 | + while ((line = reader.ReadLine()) != null) |
| 136 | + { |
| 137 | + lines.Add(line); |
| 138 | + } |
| 139 | + |
| 140 | + for (int i = 0; i < lines.Count; i++) |
| 141 | + { |
| 142 | + // Look for the "Received Import Request." line |
| 143 | + if (lines[i].Contains("Received Import Request.")) |
| 144 | + { |
| 145 | + // Check the next few lines for the path |
| 146 | + for (int j = i + 1; j < Math.Min(i + 5, lines.Count); j++) |
| 147 | + { |
| 148 | + if (lines[j].Contains($"path: {assetPath}")) |
| 149 | + { |
| 150 | + count++; |
| 151 | + break; // Found the path, move to next import request |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + catch (IOException ex) when (ex.Message.Contains("sharing violation") || ex.Message.Contains("being used by another process")) |
| 159 | + { |
| 160 | + Debug.LogWarning($"Worker log {logFilePath} is currently in use by another process. Retrying..."); |
| 161 | + |
| 162 | + // Retry after a short delay - the worker might have just finished writing |
| 163 | + System.Threading.Thread.Sleep(100); |
| 164 | + try |
| 165 | + { |
| 166 | + using (var fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 167 | + using (var reader = new StreamReader(fileStream)) |
| 168 | + { |
| 169 | + var lines = new List<string>(); |
| 170 | + string line; |
| 171 | + while ((line = reader.ReadLine()) != null) |
| 172 | + { |
| 173 | + lines.Add(line); |
| 174 | + } |
| 175 | + |
| 176 | + for (int i = 0; i < lines.Count; i++) |
| 177 | + { |
| 178 | + if (lines[i].Contains("Received Import Request.")) |
| 179 | + { |
| 180 | + for (int j = i + 1; j < Math.Min(i + 5, lines.Count); j++) |
| 181 | + { |
| 182 | + if (lines[j].Contains($"path: {assetPath}")) |
| 183 | + { |
| 184 | + count++; |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + catch (Exception retryEx) |
| 193 | + { |
| 194 | + Debug.LogWarning($"Failed to read worker log {logFilePath} after retry: {retryEx.Message}"); |
| 195 | + } |
| 196 | + } |
| 197 | + catch (Exception ex) |
| 198 | + { |
| 199 | + Debug.LogWarning($"Failed to read worker log {logFilePath}: {ex.Message}"); |
| 200 | + } |
| 201 | + |
| 202 | + return count; |
| 203 | + } |
| 204 | + |
| 205 | + /// <summary> |
| 206 | + /// Asserts that a single worker processed an import for the specified asset at least twice |
| 207 | + /// and that the expected number of imports were processed overall. |
| 208 | + /// </summary> |
| 209 | + /// <param name="importDifference">The difference snapshot showing new imports</param> |
| 210 | + /// <param name="assetPath">The asset path being imported</param> |
| 211 | + /// <param name="expectedImports">The expected number of imports</param> |
| 212 | + public static void AssertSingleWorkerUsedAtLeastTwice(WorkerLogSnapshot importDifference, string assetPath, int expectedImports) |
| 213 | + { |
| 214 | + var workersWithImports = importDifference.WorkerImportCounts.Where(kvp => kvp.Value > 0).ToList(); |
| 215 | + |
| 216 | + var maxImportsWorker = workersWithImports.OrderByDescending(kvp => kvp.Value).First(); |
| 217 | + Assert.IsTrue(maxImportsWorker.Value > 1, |
| 218 | + $"Expected at least 2 imports on a single Worker for {assetPath}"); |
| 219 | + |
| 220 | + var sumImports = workersWithImports.Sum(kvp => kvp.Value); |
| 221 | + Assert.AreEqual(expectedImports, sumImports, |
| 222 | + $"Expected a total of {expectedImports} imports for {assetPath}, but found {sumImports} imports across all workers"); |
| 223 | + } |
| 224 | + |
| 225 | + /// <summary> |
| 226 | + /// Asserts that all imports in the difference snapshot occurred on a single worker |
| 227 | + /// without checking the exact count (useful when the expected count is unknown). |
| 228 | + /// </summary> |
| 229 | + /// <param name="importDifference">The difference snapshot showing new imports</param> |
| 230 | + /// <param name="assetPath">The asset path being imported</param> |
| 231 | + /// <returns>The worker ID that performed all imports</returns> |
| 232 | + public static int AssertSingleWorkerUsed(WorkerLogSnapshot importDifference, string assetPath) |
| 233 | + { |
| 234 | + var workersWithImports = importDifference.WorkerImportCounts.Where(kvp => kvp.Value > 0).ToList(); |
| 235 | + |
| 236 | + Assert.IsTrue(workersWithImports.Count > 0, |
| 237 | + $"Expected at least one worker to have processed imports for {assetPath}, but found none"); |
| 238 | + |
| 239 | + Assert.AreEqual(1, workersWithImports.Count, |
| 240 | + $"Expected all imports for {assetPath} to occur on a single worker, but found imports on {workersWithImports.Count} workers: {string.Join(", ", workersWithImports.Select(kvp => $"Worker{kvp.Key}({kvp.Value} imports)"))}"); |
| 241 | + |
| 242 | + var singleWorker = workersWithImports.First(); |
| 243 | + Debug.Log($"✓ All {singleWorker.Value} imports for {assetPath} occurred on Worker{singleWorker.Key} as expected"); |
| 244 | + |
| 245 | + return singleWorker.Key; |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments