-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (85 loc) · 3.45 KB
/
Copy pathProgram.cs
File metadata and controls
101 lines (85 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using SaleniumUI.Models;
using SaleniumUI.Services;
// Parse arguments
if (args.Length < 1)
{
//Console.WriteLine("Usage: dotnet run -- <url> [idle-threshold-seconds] [json-output-path]");
//Console.WriteLine();
//Console.WriteLine(" url URL to test (required)");
//Console.WriteLine(" idle-threshold-seconds Seconds of no network activity to consider idle (default: 2.0)");
//Console.WriteLine(" json-output-path Path to export JSON report (optional)");
//Console.WriteLine();
//Console.WriteLine("Examples:");
//Console.WriteLine(" dotnet run -- \"https://www.example.com\"");
//Console.WriteLine(" dotnet run -- \"https://www.example.com\" 2.0");
//Console.WriteLine(" dotnet run -- \"https://www.example.com\" 2.0 report.json");
//return;
}
//var url = args[0];
//var idleThreshold = args.Length >= 2 ? double.Parse(args[1]) : 2.0;
//var jsonOutputPath = args.Length >= 3 ? args[2] : null;
var url = "https://www.google.com";
var idleThreshold = 2.0;
var jsonOutputPath = "report.json";
Console.WriteLine($" Target URL: {url}");
Console.WriteLine($" Idle threshold: {idleThreshold}s");
Console.WriteLine();
// Configure ChromeDriver
var options = new ChromeOptions();
options.AddArgument("--disable-extensions");
options.AddArgument("--no-first-run");
options.AddArgument("--disable-default-apps");
Console.WriteLine(" Launching Chrome...");
using var driver = new ChromeDriver(options);
try
{
// Set up CDP network tracking
Console.WriteLine(" Setting up network monitoring via CDP...");
var devToolsSession = ((IDevTools)driver).GetDevToolsSession();
var networkTracker = new NetworkTracker(idleThreshold);
await networkTracker.StartAsync(devToolsSession);
// Navigate to URL
Console.WriteLine($" Navigating to {url}...");
driver.Navigate().GoToUrl(url);
// Wait for network idle
Console.WriteLine(" Waiting for network idle...");
await networkTracker.WaitForNetworkIdleAsync();
Console.WriteLine(" Network idle reached.");
// Collect browser metrics
Console.WriteLine(" Collecting performance metrics...");
var metricsCollector = new MetricsCollector();
var report = new PerformanceReport
{
Url = url,
Timestamp = DateTime.Now
};
metricsCollector.CollectNavigationTiming(driver, report);
metricsCollector.CollectPaintTiming(driver, report);
metricsCollector.CollectLargestContentfulPaint(driver, report);
// Build network summary
var allRequests = networkTracker.GetAllRequests();
report.AllRequests = allRequests;
report.TotalRequests = allRequests.Count;
report.FailedRequests = allRequests.Count(r => r.Failed);
report.TotalTransferSizeBytes = allRequests.Sum(r => r.TransferSize);
report.NetworkIdleTimeMs = networkTracker.GetNetworkIdleTimeMs();
// Aggregate transfer size by resource type
foreach (var group in allRequests.GroupBy(r => r.ResourceType))
{
report.TransferSizeByType[group.Key] = group.Sum(r => r.TransferSize);
}
// Output report
var reportGenerator = new ReportGenerator();
reportGenerator.PrintConsoleReport(report);
if (jsonOutputPath != null)
{
reportGenerator.ExportJson(report, jsonOutputPath);
}
}
finally
{
driver.Quit();
}