-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[wasm] Reduce number of calls to setTimer #62433
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
68168ba
wip
pavelsavara 927a027
no id
pavelsavara 9bb2585
feedback
pavelsavara f693c9a
cleanup
pavelsavara ac3fb95
cleanup
pavelsavara cc45045
typo
pavelsavara 29b37a2
switch to new xunit/xharness
pavelsavara 26e2b49
improve test
pavelsavara b71b244
only on browser, make less noise
pavelsavara df27bdc
feedback
pavelsavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
116 changes: 116 additions & 0 deletions
116
....InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/TimerTests.cs
This file contains hidden or 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,116 @@ | ||
| // 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.Threading; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace System.Runtime.InteropServices.JavaScript.Tests | ||
| { | ||
| // V8's implementation of setTimer ignores delay parameter and always run immediately. So it could not be used to test this. | ||
| [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsBrowserDomSupported))] | ||
| public class TimerTests : IAsyncLifetime | ||
| { | ||
| static JSObject _timersHelper; | ||
| static Function _installWrapper; | ||
| static Function _getRegisterCount; | ||
| static Function _getHitCount; | ||
| static Function _cleanupWrapper; | ||
| // static Function _log; | ||
|
|
||
| public static IEnumerable<object[]> TestCases() | ||
| { | ||
| yield return new object[] { new int[0], 0, null, null }; | ||
| yield return new object[] { new[] { 10 }, 1 , null, null }; | ||
| yield return new object[] { new[] { 10, 5 }, 2 , null, null }; | ||
| yield return new object[] { new[] { 10, 20 }, 1 , null, null }; | ||
| yield return new object[] { new[] { 800, 600, 400, 200, 050 }, 5, 13, 9 }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(TestCases))] | ||
| public async Task TestTimers(int[] timeouts, int? expectedSetCounter, int? expectedSetCounterAfterCleanUp, int? expectedHitCount) | ||
| { | ||
| int wasCalled = 0; | ||
| Timer[] timers = new Timer[timeouts.Length]; | ||
| try | ||
| { | ||
| // _log.Call(null, $"Waiting for runtime to settle"); | ||
| await Task.Delay(2000); | ||
| _installWrapper.Call(); | ||
| // _log.Call(null, $"Ready!"); | ||
|
|
||
| for (int i = 0; i < timeouts.Length; i++) | ||
| { | ||
| int index = i; | ||
| // _log.Call(null, $"Registering {index} delay {timeouts[i]}"); | ||
| timers[i] = new Timer((_) => | ||
| { | ||
| // _log.Call(null, $"In timer{index}"); | ||
| wasCalled++; | ||
| }, null, timeouts[i], 0); | ||
| } | ||
|
|
||
| var setCounter = (int)_getRegisterCount.Call(); | ||
| Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}"); | ||
| Assert.True((expectedSetCounter ?? timeouts.Length) == setCounter, $"setCounter: actual {setCounter} expected {expectedSetCounter}"); | ||
|
|
||
| } | ||
| finally | ||
| { | ||
| await WaitForCleanup(); | ||
| Assert.True(timeouts.Length == wasCalled, $"wasCalled: actual {wasCalled} expected {timeouts.Length}"); | ||
|
|
||
| if (expectedSetCounterAfterCleanUp != null) | ||
| { | ||
| var setCounter = (int)_getRegisterCount.Call(); | ||
| Assert.True(expectedSetCounterAfterCleanUp.Value == setCounter, $"setCounter: actual {setCounter} expected {expectedSetCounterAfterCleanUp.Value}"); | ||
| } | ||
|
|
||
| if (expectedHitCount != null) | ||
| { | ||
| var hitCounter = (int)_getHitCount.Call(); | ||
| Assert.True(expectedHitCount == hitCounter, $"hitCounter: actual {hitCounter} expected {expectedHitCount}"); | ||
| } | ||
|
|
||
| for (int i = 0; i < timeouts.Length; i++) | ||
| { | ||
| timers[i].Dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task WaitForCleanup() | ||
| { | ||
| // _log.Call(null, "wait for cleanup begin"); | ||
| await Task.Delay(1200); | ||
pavelsavara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _cleanupWrapper.Call(); | ||
| // _log.Call(null, "wait for cleanup end"); | ||
| } | ||
|
|
||
| public async Task InitializeAsync() | ||
| { | ||
| if (_timersHelper == null) | ||
| { | ||
| Function helper = new Function(@" | ||
| const loadTimersJs = async () => { | ||
| await import('./timers.js'); | ||
| }; | ||
| return loadTimersJs(); | ||
| "); | ||
| await (Task)helper.Call(); | ||
|
|
||
| _timersHelper = (JSObject)Runtime.GetGlobalObject("timersHelper"); | ||
| _installWrapper = (Function)_timersHelper.GetObjectProperty("install"); | ||
| _getRegisterCount = (Function)_timersHelper.GetObjectProperty("getRegisterCount"); | ||
| _getHitCount = (Function)_timersHelper.GetObjectProperty("getHitCount"); | ||
| _cleanupWrapper = (Function)_timersHelper.GetObjectProperty("cleanup"); | ||
| // var console = (JSObject)Runtime.GetGlobalObject("console"); | ||
| // _log = (Function)console.GetObjectProperty("log"); | ||
| } | ||
| } | ||
|
|
||
| public Task DisposeAsync() => Task.CompletedTask; | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/timers.js
This file contains hidden or 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,44 @@ | ||
| class TimersHelper { | ||
| install() { | ||
| const measuredCallbackName = "mono_wasm_set_timeout_exec"; | ||
| globalThis.registerCount = 0; | ||
| globalThis.hitCount = 0; | ||
| // console.log("install") | ||
| if (!globalThis.originalSetTimeout) { | ||
| globalThis.originalSetTimeout = globalThis.setTimeout; | ||
| } | ||
| globalThis.setTimeout = (cb, time) => { | ||
| var start = Date.now().valueOf(); | ||
| if (cb.name === measuredCallbackName) { | ||
| globalThis.registerCount++; | ||
| // console.log(`registerCount: ${globalThis.registerCount} now:${start} delay:${time}`) | ||
| } | ||
| return globalThis.originalSetTimeout(() => { | ||
| if (cb.name === measuredCallbackName) { | ||
| var hit = Date.now().valueOf(); | ||
| globalThis.hitCount++; | ||
| var delta = hit - start; | ||
| // console.log(`hitCount: ${globalThis.hitCount} now:${hit} delay:${time} delta:${delta}`) | ||
| } | ||
| cb(); | ||
| }, time); | ||
| }; | ||
| } | ||
|
|
||
| getRegisterCount() { | ||
| // console.log(`registerCount: ${globalThis.registerCount} `) | ||
| return globalThis.registerCount; | ||
| } | ||
|
|
||
| getHitCount() { | ||
| // console.log(`hitCount: ${globalThis.hitCount} `) | ||
| return globalThis.hitCount; | ||
| } | ||
|
|
||
| cleanup() { | ||
| // console.log(`cleanup registerCount: ${globalThis.registerCount} hitCount: ${globalThis.hitCount} `) | ||
| globalThis.setTimeout = globalThis.originalSetTimeout; | ||
| } | ||
| } | ||
|
|
||
| globalThis.timersHelper = new TimersHelper(); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.