-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DevTools] Performance monitoring (#216) +semver: feature
- Loading branch information
Showing
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
Aquality.Selenium/src/Aquality.Selenium/Browsers/DevToolsPerformanceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Newtonsoft.Json.Linq; | ||
using OpenQA.Selenium.DevTools.V85.Performance; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Aquality.Selenium.Browsers | ||
{ | ||
/// <summary> | ||
/// Implementation of version-independent performance DevTools commands as extensions for <see cref="DevToolsHandling"/>. | ||
/// For more information, see <see href="https://chromedevtools.github.io/devtools-protocol/tot/Performance/"/>. | ||
/// </summary> | ||
public static class DevToolsPerformanceExtensions | ||
{ | ||
/// <summary> | ||
/// Disable collecting and reporting metrics. | ||
/// </summary> | ||
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param> | ||
/// <returns>A task for asynchronous command.</returns> | ||
public static async Task DisablePerfomanceMonitoring(this DevToolsHandling devTools) | ||
{ | ||
await devTools.SendCommand(new DisableCommandSettings()); | ||
} | ||
|
||
/// <summary> | ||
/// Enable collecting and reporting metrics. | ||
/// </summary> | ||
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param> | ||
/// <param name="timeDomain">Time domain to use for collecting and reporting duration metrics. | ||
/// Allowed Values: timeTicks, threadTicks. </param> | ||
/// <returns>A task for asynchronous command.</returns> | ||
public static async Task EnablePerfomanceMonitoring(this DevToolsHandling devTools, string timeDomain = null) | ||
{ | ||
await devTools.SendCommand(new EnableCommandSettings { TimeDomain = timeDomain }); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve current values of run-time metrics. | ||
/// </summary> | ||
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param> | ||
/// <returns>A task for asynchronous command with current values for run-time metrics as result.</returns> | ||
public static async Task<IDictionary<string, double>> GetPerformanceMetrics(this DevToolsHandling devTools) | ||
{ | ||
JToken result = await devTools.SendCommand(new GetMetricsCommandSettings()); | ||
return (result["metrics"] as JArray) | ||
.ToDictionary(item => item["name"].ToString(), item => double.Parse(item["value"].ToString(), CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/DevToolsPerformanceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Aquality.Selenium.Browsers; | ||
using NUnit.Framework; | ||
using OpenQA.Selenium.DevTools.V96.Performance; | ||
using System.Collections.Generic; | ||
|
||
namespace Aquality.Selenium.Tests.Integration | ||
{ | ||
internal class DevToolsPerformanceTests : UITest | ||
{ | ||
private static DevToolsHandling DevTools => AqualityServices.Browser.DevTools; | ||
|
||
[Test] | ||
public void Should_BePossibleTo_CollectPerformanceMetrics() | ||
{ | ||
Assert.DoesNotThrowAsync(() => DevTools.EnablePerfomanceMonitoring(), "Should be possible to enable performance monitoring"); | ||
|
||
AqualityServices.Browser.GoTo("http://www.google.com"); | ||
IDictionary<string, double> metrics = null; | ||
Assert.DoesNotThrowAsync(async () => metrics = await DevTools.GetPerformanceMetrics(), "Should be possible to get performance metrics"); | ||
CollectionAssert.IsNotEmpty(metrics, "Some metrics should be returned"); | ||
|
||
AqualityServices.Browser.Refresh(); | ||
IDictionary<string, double> otherMetrics = DevTools.GetPerformanceMetrics().GetAwaiter().GetResult(); | ||
CollectionAssert.AreNotEqual(otherMetrics, metrics, "Some additional metrics should have been collected"); | ||
|
||
Assert.DoesNotThrowAsync(() => DevTools.DisablePerfomanceMonitoring(), "Should be possible to disable performance monitoring"); | ||
AqualityServices.Browser.Refresh(); | ||
metrics = DevTools.GetPerformanceMetrics().GetAwaiter().GetResult(); | ||
CollectionAssert.IsEmpty(metrics, "Metrics should have not been collected after performance monitoring have been disabled"); | ||
} | ||
} | ||
} |