using System.Diagnostics; using System.Text.Json; using JobTrackerApi.Controllers; using JobTrackerApi.Services; if (args.Length != 3 || args[1] is not ("pdf" or "docx")) throw new ArgumentException("Usage: CvImportBenchmark "); var options = new JsonSerializerOptions(JsonSerializerDefaults.Web) { WriteIndented = true }; var outputs = new Dictionary(); foreach (var file in Directory.GetFiles(args[0], "*." + args[1] + ".extracted.json").Order()) { using var json = JsonDocument.Parse(await File.ReadAllTextAsync(file)); var root = json.RootElement; var id = Path.GetFileName(file)[..^(args[1].Length + ".extracted.json".Length + 1)]; if (!root.GetProperty("ok").GetBoolean()) { outputs.Add(id, new { error = root.GetProperty("code").GetString() }); continue; } var text = root.GetProperty("text").GetString()!; var blocks = root.GetProperty("blocks").EnumerateArray().Select(b => new CvImportBlock(b.GetProperty("id").GetString()!, b.GetProperty("text").GetString()!, b.GetProperty("kind").GetString()!, b.GetProperty("page").ValueKind == JsonValueKind.Number ? b.GetProperty("page").GetInt32() : null)).ToArray(); var document = new CvImportDocument(text, false, null, root.GetProperty("page_count").ValueKind == JsonValueKind.Number ? root.GetProperty("page_count").GetInt32() : null, text.Length, id + "." + args[1], blocks, root.GetProperty("warnings").EnumerateArray().Select(w => w.GetString()!).ToArray(), root.GetProperty("method").GetString()!); var clock = Stopwatch.StartNew(); var profile = ProfileCvController.ParseImportDocument(document); clock.Stop(); outputs.Add(id, new { profile, extractionMethod = document.Method, reviewRequired = CvDocumentReview.Build(new(), profile).Sum(g => g.Fields.Count), timing = new { interpretationMs = clock.Elapsed.TotalMilliseconds } }); } using var destination = new FileStream(args[2], FileMode.CreateNew); await JsonSerializer.SerializeAsync(destination, outputs, options); Console.WriteLine($"Recorded {outputs.Count} {args[1]} cases; no generative model was invoked.");