|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the MIT License. See License.txt in the project root for license information.
|
3 | 3 |
|
4 |
| -using System; |
5 |
| -using System.IO; |
6 |
| -using System.Runtime.CompilerServices; |
7 |
| -using System.Threading.Tasks; |
8 | 4 | using BenchmarkDotNet.Attributes;
|
9 | 5 | using Microsoft.Azure.WebJobs.Script.Description;
|
10 | 6 | using Microsoft.Azure.WebJobs.Script.Extensibility;
|
|
13 | 9 | using Microsoft.CodeAnalysis.Scripting;
|
14 | 10 | using Microsoft.CodeAnalysis.Scripting.Hosting;
|
15 | 11 | using Microsoft.Extensions.Logging.Abstractions;
|
| 12 | +using System; |
| 13 | +using System.IO; |
| 14 | +using System.Runtime.CompilerServices; |
| 15 | +using System.Threading.Tasks; |
16 | 16 |
|
17 | 17 | namespace Microsoft.Azure.WebJobs.Script.Benchmarks
|
18 | 18 | {
|
19 | 19 | public class CSharpCompilationBenchmarks
|
20 | 20 | {
|
21 |
| - // Dyanmic Compilation |
22 |
| - private readonly InteractiveAssemblyLoader _assemblyLoader = new InteractiveAssemblyLoader(); |
| 21 | + // Set of samples to benchmark |
| 22 | + // TODOO: BlobTrigger, needs assembly refs working |
| 23 | + [Params("DocumentDB", "HttpTrigger", "HttpTrigger-Cancellation", "HttpTrigger-CustomRoute", "NotificationHub")] |
| 24 | + public string BenchmarkTrigger; |
23 | 25 |
|
24 | 26 | // Script source
|
25 |
| - private string _scriptPath; |
26 |
| - private string _scriptSource; |
27 |
| - private FunctionMetadata _functionMetadata; |
28 |
| - private IFunctionMetadataResolver _resolver; |
29 |
| - private CSharpCompilationService _compilationService; |
| 27 | + private string ScriptPath; |
| 28 | + private static string GetCSharpSamplePath([CallerFilePath] string thisFilePath = null) => |
| 29 | + Path.Combine(thisFilePath, "..", "..", "..", "sample", "CSharp"); |
| 30 | + private string ScriptSource; |
| 31 | + private FunctionMetadata FunctionMetadata; |
30 | 32 |
|
31 |
| - private IDotNetCompilation _scriptCompilation; |
32 |
| - private DotNetCompilationResult _scriptAssembly; |
| 33 | + // Dyanmic Compilation |
| 34 | + private readonly InteractiveAssemblyLoader AssemblyLoader = new InteractiveAssemblyLoader(); |
| 35 | + private IFunctionMetadataResolver Resolver; |
| 36 | + private CSharpCompilationService CompilationService; |
33 | 37 |
|
34 |
| - // Set of samples to benchmark |
35 |
| - // TODO: BlobTrigger, needs assembly refs working |
36 |
| - [Params("DocumentDB", "HttpTrigger", "HttpTrigger-Cancellation", "HttpTrigger-CustomRoute", "NotificationHub")] |
37 |
| - public string BenchmarkTrigger { get; set; } |
| 38 | + private IDotNetCompilation ScriptCompilation; |
| 39 | + private DotNetCompilationResult ScriptAssembly; |
38 | 40 |
|
39 | 41 | [GlobalSetup]
|
40 | 42 | public async Task SetupAsync()
|
41 | 43 | {
|
42 |
| - _scriptPath = Path.Combine(GetCSharpSamplePath(), BenchmarkTrigger, "run.csx"); |
43 |
| - _scriptSource = File.ReadAllText(_scriptPath); |
44 |
| - _functionMetadata = new FunctionMetadata() |
| 44 | + ScriptPath = Path.Combine(GetCSharpSamplePath(), BenchmarkTrigger, "run.csx"); |
| 45 | + ScriptSource = File.ReadAllText(ScriptPath); |
| 46 | + FunctionMetadata = new FunctionMetadata() |
45 | 47 | {
|
46 |
| - FunctionDirectory = Path.GetDirectoryName(_scriptPath), |
47 |
| - ScriptFile = _scriptPath, |
| 48 | + FunctionDirectory = Path.GetDirectoryName(ScriptPath), |
| 49 | + ScriptFile = ScriptPath, |
48 | 50 | Name = BenchmarkTrigger,
|
49 | 51 | Language = DotNetScriptTypes.CSharp
|
50 | 52 | };
|
51 | 53 |
|
52 |
| - _resolver = new ScriptFunctionMetadataResolver(_scriptPath, Array.Empty<IScriptBindingProvider>(), NullLogger.Instance); |
53 |
| - _compilationService = new CSharpCompilationService(_resolver, OptimizationLevel.Release); |
| 54 | + Resolver = new ScriptFunctionMetadataResolver(ScriptPath, Array.Empty<IScriptBindingProvider>(), NullLogger.Instance); |
| 55 | + CompilationService = new CSharpCompilationService(Resolver, OptimizationLevel.Release); |
54 | 56 |
|
55 |
| - _scriptCompilation = await _compilationService.GetFunctionCompilationAsync(_functionMetadata); |
56 |
| - _scriptAssembly = await _scriptCompilation.EmitAsync(default); |
| 57 | + ScriptCompilation = await CompilationService.GetFunctionCompilationAsync(FunctionMetadata); |
| 58 | + ScriptAssembly = await ScriptCompilation.EmitAsync(default); |
57 | 59 | }
|
58 | 60 |
|
59 | 61 | [Benchmark(Description = nameof(CSharpScript) + "." + nameof(CSharpScript.Create))]
|
60 |
| - public Script<object> ScriptCreation() => |
61 |
| - CSharpScript.Create(_scriptSource, options: _resolver.CreateScriptOptions(), assemblyLoader: _assemblyLoader); |
| 62 | + public Script<object> ScriptCreation() => |
| 63 | + CSharpScript.Create(ScriptSource, options: Resolver.CreateScriptOptions(), assemblyLoader: AssemblyLoader); |
62 | 64 |
|
63 | 65 | [Benchmark(Description = nameof(CSharpCompilationService) + "." + nameof(CSharpCompilationService.GetFunctionCompilationAsync))]
|
64 |
| - public Task<IDotNetCompilation> GetFunctionCompilationAsync() => _compilationService.GetFunctionCompilationAsync(_functionMetadata); |
| 66 | + public Task<IDotNetCompilation> GetFunctionCompilationAsync() => CompilationService.GetFunctionCompilationAsync(FunctionMetadata); |
65 | 67 |
|
66 | 68 | [Benchmark(Description = nameof(CSharpCompilationBenchmarks) + "." + nameof(CSharpCompilationBenchmarks.EmitAsync))]
|
67 |
| - public Task<DotNetCompilationResult> EmitAsync() => _scriptCompilation.EmitAsync(default); |
| 69 | + public Task<DotNetCompilationResult> EmitAsync() => ScriptCompilation.EmitAsync(default); |
68 | 70 |
|
69 | 71 | [Benchmark(Description = nameof(DotNetCompilationResult) + "." + nameof(DotNetCompilationResult.Load))]
|
70 |
| - public void Load() => _scriptAssembly.Load(_functionMetadata, _resolver, NullLogger.Instance); |
71 |
| - |
72 |
| - private static string GetCSharpSamplePath([CallerFilePath] string thisFilePath = null) => |
73 |
| - Path.Combine(thisFilePath, "..", "..", "..", "sample", "CSharp"); |
| 72 | + public void Load() => ScriptAssembly.Load(FunctionMetadata,Resolver, NullLogger.Instance); |
74 | 73 | }
|
75 | 74 | }
|
0 commit comments