|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Reflection; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Sample |
| 12 | +{ |
| 13 | + public class AppStartTask : BenchTask |
| 14 | + { |
| 15 | + public override string Name => "AppStart"; |
| 16 | + public override bool BrowserOnly => true; |
| 17 | + |
| 18 | + [DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, "System.Runtime.InteropServices.JavaScript.Runtime", "System.Private.Runtime.InteropServices.JavaScript")] |
| 19 | + static Type jsRuntimeType = System.Type.GetType("System.Runtime.InteropServices.JavaScript.Runtime, System.Private.Runtime.InteropServices.JavaScript", true); |
| 20 | + static Type jsFunctionType = System.Type.GetType("System.Runtime.InteropServices.JavaScript.Function, System.Private.Runtime.InteropServices.JavaScript", true); |
| 21 | + [DynamicDependency("InvokeJS(System.String)", "System.Runtime.InteropServices.JavaScript.Runtime", "System.Private.Runtime.InteropServices.JavaScript")] |
| 22 | + static MethodInfo invokeJSMethod = jsRuntimeType.GetMethod("InvokeJS", new Type[] { typeof(string) }); |
| 23 | + [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, "System.Runtime.InteropServices.JavaScript.Function", "System.Private.Runtime.InteropServices.JavaScript")] |
| 24 | + static ConstructorInfo functionConstructor = jsRuntimeType.GetConstructor(new Type[] { typeof(object[]) }); |
| 25 | + [DynamicDependency("Call()", "System.Runtime.InteropServices.JavaScript.Function", "System.Private.Runtime.InteropServices.JavaScript")] |
| 26 | + static MethodInfo functionCall = jsFunctionType.GetMethod("Call", BindingFlags.Instance | BindingFlags.Public, new Type[] { }); |
| 27 | + |
| 28 | + public AppStartTask() |
| 29 | + { |
| 30 | + measurements = new Measurement[] { |
| 31 | + new PageShow(), |
| 32 | + new ReachManaged(), |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + Measurement[] measurements; |
| 37 | + public override Measurement[] Measurements => measurements; |
| 38 | + |
| 39 | + static string InvokeJS(string js) |
| 40 | + { |
| 41 | + return (string)invokeJSMethod.Invoke(null, new object[] { js }); |
| 42 | + } |
| 43 | + |
| 44 | + class PageShow : BenchTask.Measurement |
| 45 | + { |
| 46 | + public override string Name => "Page show"; |
| 47 | + |
| 48 | + public override int InitialSamples => 3; |
| 49 | + |
| 50 | + async Task RunAsyncStep() |
| 51 | + { |
| 52 | + var function = Activator.CreateInstance(jsFunctionType, new object[] { new object[] { @"return App.StartAppUI();" } }); |
| 53 | + var task = (Task<object>)functionCall.Invoke(function, new object[] { }); |
| 54 | + |
| 55 | + await task; |
| 56 | + } |
| 57 | + |
| 58 | + public override bool HasRunStepAsync => true; |
| 59 | + |
| 60 | + public override async Task RunStepAsync() |
| 61 | + { |
| 62 | + var function = Activator.CreateInstance(jsFunctionType, new object[] { new object[] { @"return App.PageShow();" } }); |
| 63 | + await (Task<object>)functionCall.Invoke(function, null); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + class ReachManaged : BenchTask.Measurement |
| 68 | + { |
| 69 | + public override string Name => "Reach managed"; |
| 70 | + public override int InitialSamples => 3; |
| 71 | + public override bool HasRunStepAsync => true; |
| 72 | + |
| 73 | + static object jsUIReachedManagedFunction = Activator.CreateInstance(jsFunctionType, new object[] { new object[] { @"return App.ReachedManaged();" } }); |
| 74 | + static object jsReached = Activator.CreateInstance(jsFunctionType, new object[] { new object[] { @"return App.reached();" } }); |
| 75 | + |
| 76 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 77 | + public static void Reached() |
| 78 | + { |
| 79 | + functionCall.Invoke(jsReached, null); |
| 80 | + } |
| 81 | + |
| 82 | + public override async Task RunStepAsync() |
| 83 | + { |
| 84 | + await (Task<object>)functionCall.Invoke(jsUIReachedManagedFunction, null); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments