Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] spread WS based timers over next 6 minutes to prevent heavy throttling #57745

Merged
merged 8 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
# Visual Studio Version 17
VisualStudioVersion = 17.0.31521.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{B615DEB1-354C-4357-987A-BBA921E5A712}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.WebSockets.Client", "ref\System.Net.WebSockets.Client.csproj", "{BEA5BC2C-12D1-4D01-8D2C-5029578BD066}"
Expand All @@ -19,16 +23,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{A0314AC5-E49
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{6F9A42A0-A04B-4CD0-B8C9-9A728274C851}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.WebSockets.Client.Wasm.Tests", "tests\wasm\System.Net.WebSockets.Client.Wasm.Tests.csproj", "{CA20532A-33B3-4DC0-92D2-EA6D7987D59F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Release|Any CPU.Build.0 = Release|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Release|Any CPU.Build.0 = Release|Any CPU
{BEA5BC2C-12D1-4D01-8D2C-5029578BD066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEA5BC2C-12D1-4D01-8D2C-5029578BD066}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEA5BC2C-12D1-4D01-8D2C-5029578BD066}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -37,34 +43,39 @@ Global
{0CD4C24D-7746-46F0-8D47-A396882B5468}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CD4C24D-7746-46F0-8D47-A396882B5468}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CD4C24D-7746-46F0-8D47-A396882B5468}.Release|Any CPU.Build.0 = Release|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B615DEB1-354C-4357-987A-BBA921E5A712}.Release|Any CPU.Build.0 = Release|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90E8DA45-66F3-491E-B408-82AB85EEAB76}.Release|Any CPU.Build.0 = Release|Any CPU
{8CD4D190-F656-4970-9AE9-4A9F8B30A2F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CD4D190-F656-4970-9AE9-4A9F8B30A2F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CD4D190-F656-4970-9AE9-4A9F8B30A2F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CD4D190-F656-4970-9AE9-4A9F8B30A2F8}.Release|Any CPU.Build.0 = Release|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Release|Any CPU.Build.0 = Release|Any CPU
{6B9721B2-00D0-41EC-96B9-6428CF1830CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B9721B2-00D0-41EC-96B9-6428CF1830CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B9721B2-00D0-41EC-96B9-6428CF1830CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B9721B2-00D0-41EC-96B9-6428CF1830CF}.Release|Any CPU.Build.0 = Release|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59A23CAB-D098-495F-A467-74C7553FF5BB}.Release|Any CPU.Build.0 = Release|Any CPU
{CA20532A-33B3-4DC0-92D2-EA6D7987D59F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA20532A-33B3-4DC0-92D2-EA6D7987D59F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA20532A-33B3-4DC0-92D2-EA6D7987D59F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA20532A-33B3-4DC0-92D2-EA6D7987D59F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{90E8DA45-66F3-491E-B408-82AB85EEAB76} = {BEE2F256-0489-4809-AB20-27ADB2D0E10C}
{B615DEB1-354C-4357-987A-BBA921E5A712} = {BEE2F256-0489-4809-AB20-27ADB2D0E10C}
{BEA5BC2C-12D1-4D01-8D2C-5029578BD066} = {A0314AC5-E490-4A6A-B946-8B9A21A2FA05}
{6B9721B2-00D0-41EC-96B9-6428CF1830CF} = {A0314AC5-E490-4A6A-B946-8B9A21A2FA05}
{0CD4C24D-7746-46F0-8D47-A396882B5468} = {6F9A42A0-A04B-4CD0-B8C9-9A728274C851}
{90E8DA45-66F3-491E-B408-82AB85EEAB76} = {BEE2F256-0489-4809-AB20-27ADB2D0E10C}
{8CD4D190-F656-4970-9AE9-4A9F8B30A2F8} = {6F9A42A0-A04B-4CD0-B8C9-9A728274C851}
{6B9721B2-00D0-41EC-96B9-6428CF1830CF} = {A0314AC5-E490-4A6A-B946-8B9A21A2FA05}
{59A23CAB-D098-495F-A467-74C7553FF5BB} = {6F9A42A0-A04B-4CD0-B8C9-9A728274C851}
{CA20532A-33B3-4DC0-92D2-EA6D7987D59F} = {BEE2F256-0489-4809-AB20-27ADB2D0E10C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D91D7DC5-24CC-4716-A357-8170C4EB1C32}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ await SendAsync(
"ReceiveAsync"),
ex.Message);

Assert.Equal(WebSocketState.Aborted, cws.State);
Assert.True(WebSocketState.Aborted == cws.State, cws.State+" state when InvalidOperationException");
}
else if (ex is WebSocketException)
{
// Multiple cases.
Assert.Equal(WebSocketState.Aborted, cws.State);
Assert.True(WebSocketState.Aborted == cws.State, cws.State + " state when WebSocketException");

WebSocketError errCode = (ex as WebSocketException).WebSocketErrorCode;
Assert.True(
Expand All @@ -269,7 +269,7 @@ await SendAsync(
}
else if (ex is OperationCanceledException)
{
Assert.Equal(WebSocketState.Aborted, cws.State);
Assert.True(WebSocketState.Aborted == cws.State, cws.State + " state when OperationCanceledException");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets.Client.Tests;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace System.Net.WebSockets.Client.Wasm.Tests
{
// https://developer.chrome.com/blog/timer-throttling-in-chrome-88/
// https://docs.google.com/document/d/11FhKHRcABGS4SWPFGwoL6g0ALMqrFKapCk5ZTKKupEk/view
// requires chromium based browser
// requires minimized browser or browser tab out of focus, browser can't be headless
// requires --enable-features=IntensiveWakeUpThrottling:grace_period_seconds/1 chromeDriver flags
// doesn't work with --disable-background-timer-throttling
[TestCaseOrderer("System.Net.WebSockets.Client.Wasm.Tests.AlphabeticalOrderer", "System.Net.WebSockets.Client.Wasm.Tests")]
public class BrowserTimerThrottlingTest : ClientWebSocketTestBase
{
public static bool IsBrowser => RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
const double moreThanLightThrottlingThreshold = 1900;
const double detectLightThrottlingThreshold = 950;
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved

public BrowserTimerThrottlingTest(ITestOutputHelper output) : base(output) { }

[ConditionalFact(nameof(PlatformDetection.IsBrowser))]
[OuterLoop] // involves long delay
// this test is influenced by usage of WS on the same browser tab in previous unit tests. we may need to wait long time for it to fizzle down
public async Task DotnetTimersAreHeavilyThrottledWithoutWebSocket()
{
double maxDelayMs = 0;
double maxLightDelayMs = 0;
DateTime start = DateTime.Now;
CancellationTokenSource cts = new CancellationTokenSource();

using (var timer = new Timers.Timer(100))
{
DateTime last = DateTime.Now;
DateTime lastSent = DateTime.MinValue;
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += (object? source, Timers.ElapsedEventArgs? e) =>
{
var ms = (e.SignalTime - last).TotalMilliseconds;
var msSent = (e.SignalTime - lastSent).TotalMilliseconds;
if (maxDelayMs < ms)
{
maxDelayMs = ms;
}
if (ms > moreThanLightThrottlingThreshold)
{
#if DEBUG
Console.WriteLine("Too slow tick " + ms);
#endif
// stop, we are throttled heavily, this is what we are looking for
cts.Cancel();
}
else if (ms > detectLightThrottlingThreshold)
{
maxLightDelayMs = ms;
// we are lightly throttled
#if DEBUG
Console.WriteLine("Slow tick NO-WS " + ms);
#endif
}
if (msSent > 45000)
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
{
lastSent = DateTime.Now;
}
last = e.SignalTime;
};

// test it for 10 minutes
try { await Task.Delay(10 * 60 * 1000, cts.Token); } catch (Exception) { }
timer.Close();
}
Assert.True(maxDelayMs > detectLightThrottlingThreshold, "Expect that it throttled lightly " + maxDelayMs);
Assert.True(maxDelayMs > moreThanLightThrottlingThreshold, "Expect that it was heavily throttled " + maxDelayMs);
}

[ConditionalFact(nameof(WebSocketsSupported), nameof(PlatformDetection.IsBrowser))]
[OuterLoop] // involves long delay
public async Task WebSocketKeepsDotnetTimersOnlyLightlyThrottled()
{
double maxDelayMs = 0;
double maxLightDelayMs = 0;
DateTime start = DateTime.Now;
CancellationTokenSource cts = new CancellationTokenSource();

using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(Test.Common.Configuration.WebSockets.RemoteEchoServer, TimeOutMilliseconds, _output))
{
await SendAndReceive(cws, "test");
using (var timer = new Timers.Timer(100))
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
{
DateTime last = DateTime.Now;
DateTime lastSent = DateTime.MinValue;
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += async (object? source, Timers.ElapsedEventArgs? e) =>
{
var ms = (e.SignalTime - last).TotalMilliseconds;
var msSent = (e.SignalTime - lastSent).TotalMilliseconds;
if (maxDelayMs < ms)
{
maxDelayMs = ms;
}
if (ms > moreThanLightThrottlingThreshold)
{
// fail fast, we are throttled heavily
Console.WriteLine("Too slow tick " + ms);
cts.Cancel();
}
else if (ms > detectLightThrottlingThreshold)
{
maxLightDelayMs = ms;
// we are lightly throttled
#if DEBUG
Console.WriteLine("Slow tick WS " + ms);
#endif
}
if (msSent > 45000)
{
await SendAndReceive(cws, "test");
lastSent = DateTime.Now;
}
last = e.SignalTime;
};

// test it for 10 minutes
try { await Task.Delay(10 * 60 * 1000, cts.Token); } catch (Exception) { }
timer.Close();
}
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "WebSocketKeepsDotnetTimersOnlyLightlyThrottled", CancellationToken.None);
}
Assert.True(maxDelayMs > detectLightThrottlingThreshold, "Expect that it throttled lightly " + maxDelayMs);
Assert.True(maxDelayMs < moreThanLightThrottlingThreshold, "Expect that it wasn't heavily throttled " + maxDelayMs);
}

private async static Task SendAndReceive(ClientWebSocket cws, string message)
{
try
{
byte[] buffer = Encoding.UTF8.GetBytes(message);
await cws.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);

var receiveBuffer = new byte[100];
var receiveSegment = new ArraySegment<byte>(receiveBuffer);
WebSocketReceiveResult recvRet = await cws.ReceiveAsync(receiveSegment, CancellationToken.None);
#if DEBUG
Console.WriteLine("SendAndReceive");
#endif
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Console.WriteLine("SendAndReceive fail:" + ex);
}
}
}

public class AlphabeticalOrderer : ITestCaseOrderer
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
{
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
where TTestCase : ITestCase
{
List<TTestCase> result = testCases.ToList();
result.Sort((x, y) => StringComparer.Ordinal.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<TargetFrameworks>$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
<WasmXHarnessArgs>--background-throttling</WasmXHarnessArgs>
<Scenario>WasmTestOnBrowser</Scenario>
<TestArchiveTestsRoot>$(TestArchiveRoot)browseronly/</TestArchiveTestsRoot>
<TestArchiveTestsDir>$(TestArchiveTestsRoot)$(OSPlatformConfig)/</TestArchiveTestsDir>
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>

<Import Condition="'$(TargetOS)' == 'Browser'" Project="$(CommonTestPath)System/Net/Prerequisites/LocalEchoServer.props" />

<!-- Browser specific files -->
<ItemGroup Condition="'$(TargetOS)' == 'Browser'">
<ProjectReference Include="$(CommonTestPath)System/Net/Prerequisites/NetCoreServer/NetCoreServer.csproj" ReferenceOutputAssembly="false"/>
<ProjectReference Include="$(CommonTestPath)System/Net/Prerequisites/RemoteLoopServer/RemoteLoopServer.csproj" ReferenceOutputAssembly="false"/>
<Compile Include="$(CommonTestPath)System\Net\WebSockets\WebSocketStream.cs"
Link="Common\System\Net\WebSockets\WebSocketStream.cs" />
</ItemGroup>

<!-- Do not reference these assemblies from the TargetingPack since we are building part of the source code for tests. -->
<ItemGroup>
<DefaultReferenceExclusion Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\Net\Capability.Security.cs"
Link="Common\System\Net\Capability.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs"
Link="Common\System\Net\Configuration.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Certificates.cs"
Link="Common\System\Net\Configuration.Certificates.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Http.cs"
Link="Common\System\Net\Configuration.Http.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Security.cs"
Link="Common\System\Net\Configuration.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.WebSockets.cs"
Link="Common\System\Net\Configuration.WebSockets.cs" />
<Compile Include="$(CommonTestPath)System\Net\EventSourceTestLogging.cs"
Link="Common\System\Net\EventSourceTestLogging.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\LoopbackProxyServer.cs"
Link="Common\System\Net\Http\LoopbackProxyServer.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\LoopbackServer.cs"
Link="Common\System\Net\Http\LoopbackServer.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\GenericLoopbackServer.cs"
Link="Common\System\Net\Http\GenericLoopbackServer.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\PlatformSupport.cs"
Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs"
Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
<Compile Include="..\ClientWebSocketTestBase.cs" />
<Compile Include="..\WebSocketHelper.cs" />
<Compile Include="..\LoopbackHelper.cs" />
<Compile Include="..\ResourceHelper.cs" />
<Compile Include="..\WebSocketData.cs" />
<Compile Include="BrowserTimerThrottlingTest.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Net.TestData" Version="$(SystemNetTestDataVersion)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31521.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.WebSockets.Client.Wasm.Tests", "System.Net.WebSockets.Client.Wasm.Tests.csproj", "{DA6DF153-169E-485D-90A4-EDBDA54B32A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DA6DF153-169E-485D-90A4-EDBDA54B32A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA6DF153-169E-485D-90A4-EDBDA54B32A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA6DF153-169E-485D-90A4-EDBDA54B32A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA6DF153-169E-485D-90A4-EDBDA54B32A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {627DB5DF-DE31-44B4-B5FD-E5666467145E}
EndGlobalSection
EndGlobal
Loading