forked from microsoft/node-api-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmarks.cs
86 lines (70 loc) · 2.54 KB
/
Benchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Microsoft.JavaScript.NodeApi.Runtimes;
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
using static Microsoft.JavaScript.NodeApi.Test.TestUtils;
namespace Microsoft.JavaScript.NodeApi.Bench;
public abstract class Benchmarks
{
public static void Main(string[] args)
{
// Example: dotnet run -c Release --filter aot
// If no filter is specified, the switcher will prompt.
BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args,
ManualConfig.Create(DefaultConfig.Instance).WithOptions(ConfigOptions.JoinSummary));
}
public class NonAot : Benchmarks
{
// Non-AOT-only benchmarks may go here
}
[SimpleJob(RuntimeMoniker.NativeAot70)]
public class Aot : Benchmarks
{
// AOT-only benchmarks may go here
}
private static string LibnodePath { get; } = Path.Combine(
GetRepoRootDirectory(),
"bin",
"win-x64", // TODO
"libnode" + GetSharedLibraryExtension());
private napi_env _env;
private JSValue _function;
private JSValue _callback;
private JSReference _reference = null!;
[GlobalSetup]
public void Setup()
{
NodejsPlatform platform = new(LibnodePath);
// This setup avoids using NodejsEnvironment so benchmarks can run on the same thread.
// NodejsEnvironment creates a separate thread that would slow down the micro-benchmarks.
_env = JSNativeApi.CreateEnvironment(
(napi_platform)platform, (error) => Console.WriteLine(error), null);
// The new scope instance saves itself as the thread-local JSValueScope.Current.
JSValueScope scope = new(JSValueScopeType.Root, _env);
// Create some JS values that will be used by the benchmarks.
_function = JSNativeApi.RunScript("function callMeBack(cb) { cb(); }; callMeBack");
_callback = JSValue.CreateFunction("callback", (args) => JSValue.Undefined);
_reference = new JSReference(_function);
}
[Benchmark]
public void CallJS()
{
_function.Call(thisArg: default, _callback);
}
[Benchmark]
public void GetReference()
{
_ = _reference.GetValue()!.Value;
}
[Benchmark]
public void CreateAndDiposeReference()
{
using JSReference reference = new(_function);
}
}