|
| 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.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Net.WebSockets; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Runtime.InteropServices.JavaScript; |
| 10 | +using System.Text; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace Sample |
| 15 | +{ |
| 16 | + // http://localhost:8000/?task=JSInterop |
| 17 | + class JSInteropTask : BenchTask |
| 18 | + { |
| 19 | + public override string Name => "JSInterop"; |
| 20 | + public override Measurement[] Measurements => measurements; |
| 21 | + public override bool BrowserOnly => false; |
| 22 | + |
| 23 | + Measurement[] measurements; |
| 24 | + public JSInteropTask() |
| 25 | + { |
| 26 | + measurements = new Measurement[] { |
| 27 | + new LegacyExportIntMeasurement(), |
| 28 | + new JSExportIntMeasurement(), |
| 29 | + new LegacyExportStringMeasurement(), |
| 30 | + new JSExportStringMeasurement(), |
| 31 | + new JSImportIntMeasurement(), |
| 32 | + new JSImportStringMeasurement(), |
| 33 | + new JSImportTaskMeasurement(), |
| 34 | + new JSImportTaskFailMeasurement(), |
| 35 | + new JSImportFailMeasurement(), |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + public class LegacyExportIntMeasurement : BenchTask.Measurement |
| 40 | + { |
| 41 | + public override int InitialSamples => 10; |
| 42 | + public override string Name => "LegacyExportInt"; |
| 43 | + public override void RunStep() |
| 44 | + { |
| 45 | + ImportsExportsHelper.RunLegacyExportInt(10000); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public class JSExportIntMeasurement : BenchTask.Measurement |
| 50 | + { |
| 51 | + public override int InitialSamples => 10; |
| 52 | + public override string Name => "JSExportInt"; |
| 53 | + public override void RunStep() |
| 54 | + { |
| 55 | + ImportsExportsHelper.RunJSExportInt(10000); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public class LegacyExportStringMeasurement : BenchTask.Measurement |
| 60 | + { |
| 61 | + public override int InitialSamples => 10; |
| 62 | + public override string Name => "LegacyExportString"; |
| 63 | + public override void RunStep() |
| 64 | + { |
| 65 | + ImportsExportsHelper.RunLegacyExportString(10000); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public class JSExportStringMeasurement : BenchTask.Measurement |
| 70 | + { |
| 71 | + public override int InitialSamples => 10; |
| 72 | + public override string Name => "JSExportString"; |
| 73 | + public override void RunStep() |
| 74 | + { |
| 75 | + ImportsExportsHelper.RunJSExportString(10000); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public class JSImportIntMeasurement : BenchTask.Measurement |
| 80 | + { |
| 81 | + public override int InitialSamples => 10; |
| 82 | + public override string Name => "JSImportInt"; |
| 83 | + public override void RunStep() |
| 84 | + { |
| 85 | + ImportsExportsHelper.ImportTargetInt(10000); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public class JSImportStringMeasurement : BenchTask.Measurement |
| 90 | + { |
| 91 | + public override int InitialSamples => 10; |
| 92 | + public override string Name => "JSImportString"; |
| 93 | + public override void RunStep() |
| 94 | + { |
| 95 | + ImportsExportsHelper.ImportTargetString("A" + currentStep); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public class JSImportTaskMeasurement : BenchTask.Measurement |
| 100 | + { |
| 101 | + public override int InitialSamples => 10; |
| 102 | + public override string Name => "JSImportTask"; |
| 103 | + public override async Task RunStepAsync() |
| 104 | + { |
| 105 | + TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(); |
| 106 | + var promise = ImportsExportsHelper.ImportTargetTask(tcs.Task); |
| 107 | + tcs.SetResult(currentStep); |
| 108 | + await promise; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public class JSImportTaskFailMeasurement : BenchTask.Measurement |
| 113 | + { |
| 114 | + public override int InitialSamples => 10; |
| 115 | + public override string Name => "JSImportTaskFail"; |
| 116 | + public override async Task RunStepAsync() |
| 117 | + { |
| 118 | + TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(); |
| 119 | + var promise = ImportsExportsHelper.ImportTargetTask(tcs.Task); |
| 120 | + tcs.SetException(new Exception("test")); |
| 121 | + try |
| 122 | + { |
| 123 | + await promise; |
| 124 | + } |
| 125 | + catch (Exception) |
| 126 | + { |
| 127 | + // no action |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public class JSImportFailMeasurement : BenchTask.Measurement |
| 133 | + { |
| 134 | + public override int InitialSamples => 10; |
| 135 | + public override string Name => "JSImportFail"; |
| 136 | + public override void RunStep() |
| 137 | + { |
| 138 | + try |
| 139 | + { |
| 140 | + ImportsExportsHelper.ImportTargetThrows(currentStep); |
| 141 | + } |
| 142 | + catch (Exception) |
| 143 | + { |
| 144 | + // no action |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + partial class ImportsExportsHelper |
| 151 | + { |
| 152 | + [JSImport("Sample.Test.runLegacyExportInt", "main.js")] |
| 153 | + public static partial void RunLegacyExportInt(int count); |
| 154 | + |
| 155 | + [JSImport("Sample.Test.runJSExportInt", "main.js")] |
| 156 | + public static partial void RunJSExportInt(int count); |
| 157 | + |
| 158 | + [JSImport("Sample.Test.runLegacyExportString", "main.js")] |
| 159 | + public static partial void RunLegacyExportString(int count); |
| 160 | + |
| 161 | + [JSImport("Sample.Test.runJSExportString", "main.js")] |
| 162 | + public static partial void RunJSExportString(int count); |
| 163 | + |
| 164 | + [JSImport("Sample.Test.importTargetInt", "main.js")] |
| 165 | + public static partial int ImportTargetInt(int value); |
| 166 | + |
| 167 | + [JSImport("Sample.Test.importTargetString", "main.js")] |
| 168 | + public static partial string ImportTargetString(string value); |
| 169 | + |
| 170 | + [JSImport("Sample.Test.importTargetTask", "main.js")] |
| 171 | + public static partial Task<int> ImportTargetTask(Task<int> value); |
| 172 | + |
| 173 | + [JSImport("Sample.Test.importTargetThrows", "main.js")] |
| 174 | + public static partial void ImportTargetThrows(int value); |
| 175 | + |
| 176 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 177 | + public static int LegacyExportTargetInt(int value) |
| 178 | + { |
| 179 | + return value + 1; |
| 180 | + } |
| 181 | + |
| 182 | + [JSExport] |
| 183 | + public static int JSExportTargetInt(int value) |
| 184 | + { |
| 185 | + return value + 1; |
| 186 | + } |
| 187 | + |
| 188 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 189 | + public static string LegacyExportTargetString(string value) |
| 190 | + { |
| 191 | + return value + "A"; |
| 192 | + } |
| 193 | + |
| 194 | + [JSExport] |
| 195 | + public static string JSExportTargetString(string value) |
| 196 | + { |
| 197 | + return value + "A"; |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments