|
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; |
4 | 8 | using BenchmarkDotNet.Attributes; |
5 | 9 | using Microsoft.Azure.WebJobs.Script.Description; |
6 | 10 | using Microsoft.Azure.WebJobs.Script.Extensibility; |
|
9 | 13 | using Microsoft.CodeAnalysis.Scripting; |
10 | 14 | using Microsoft.CodeAnalysis.Scripting.Hosting; |
11 | 15 | 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 | | - // 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; |
| 21 | + // Dyanmic Compilation |
| 22 | + private readonly InteractiveAssemblyLoader _assemblyLoader = new InteractiveAssemblyLoader(); |
25 | 23 |
|
26 | 24 | // Script source |
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; |
| 25 | + private string _scriptPath; |
| 26 | + private string _scriptSource; |
| 27 | + private FunctionMetadata _functionMetadata; |
| 28 | + private IFunctionMetadataResolver _resolver; |
| 29 | + private CSharpCompilationService _compilationService; |
32 | 30 |
|
33 | | - // Dyanmic Compilation |
34 | | - private readonly InteractiveAssemblyLoader AssemblyLoader = new InteractiveAssemblyLoader(); |
35 | | - private IFunctionMetadataResolver Resolver; |
36 | | - private CSharpCompilationService CompilationService; |
| 31 | + private IDotNetCompilation _scriptCompilation; |
| 32 | + private DotNetCompilationResult _scriptAssembly; |
37 | 33 |
|
38 | | - private IDotNetCompilation ScriptCompilation; |
39 | | - private DotNetCompilationResult ScriptAssembly; |
| 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; } |
40 | 38 |
|
41 | 39 | [GlobalSetup] |
42 | 40 | public async Task SetupAsync() |
43 | 41 | { |
44 | | - ScriptPath = Path.Combine(GetCSharpSamplePath(), BenchmarkTrigger, "run.csx"); |
45 | | - ScriptSource = File.ReadAllText(ScriptPath); |
46 | | - FunctionMetadata = new FunctionMetadata() |
| 42 | + _scriptPath = Path.Combine(GetCSharpSamplePath(), BenchmarkTrigger, "run.csx"); |
| 43 | + _scriptSource = File.ReadAllText(_scriptPath); |
| 44 | + _functionMetadata = new FunctionMetadata() |
47 | 45 | { |
48 | | - FunctionDirectory = Path.GetDirectoryName(ScriptPath), |
49 | | - ScriptFile = ScriptPath, |
| 46 | + FunctionDirectory = Path.GetDirectoryName(_scriptPath), |
| 47 | + ScriptFile = _scriptPath, |
50 | 48 | Name = BenchmarkTrigger, |
51 | 49 | Language = DotNetScriptTypes.CSharp |
52 | 50 | }; |
53 | 51 |
|
54 | | - Resolver = new ScriptFunctionMetadataResolver(ScriptPath, Array.Empty<IScriptBindingProvider>(), NullLogger.Instance); |
55 | | - CompilationService = new CSharpCompilationService(Resolver, OptimizationLevel.Release); |
| 52 | + _resolver = new ScriptFunctionMetadataResolver(_scriptPath, Array.Empty<IScriptBindingProvider>(), NullLogger.Instance); |
| 53 | + _compilationService = new CSharpCompilationService(_resolver, OptimizationLevel.Release); |
56 | 54 |
|
57 | | - ScriptCompilation = await CompilationService.GetFunctionCompilationAsync(FunctionMetadata); |
58 | | - ScriptAssembly = await ScriptCompilation.EmitAsync(default); |
| 55 | + _scriptCompilation = await _compilationService.GetFunctionCompilationAsync(_functionMetadata); |
| 56 | + _scriptAssembly = await _scriptCompilation.EmitAsync(default); |
59 | 57 | } |
60 | 58 |
|
61 | 59 | [Benchmark(Description = nameof(CSharpScript) + "." + nameof(CSharpScript.Create))] |
62 | | - public Script<object> ScriptCreation() => |
63 | | - CSharpScript.Create(ScriptSource, options: Resolver.CreateScriptOptions(), assemblyLoader: AssemblyLoader); |
| 60 | + public Script<object> ScriptCreation() => |
| 61 | + CSharpScript.Create(_scriptSource, options: _resolver.CreateScriptOptions(), assemblyLoader: _assemblyLoader); |
64 | 62 |
|
65 | 63 | [Benchmark(Description = nameof(CSharpCompilationService) + "." + nameof(CSharpCompilationService.GetFunctionCompilationAsync))] |
66 | | - public Task<IDotNetCompilation> GetFunctionCompilationAsync() => CompilationService.GetFunctionCompilationAsync(FunctionMetadata); |
| 64 | + public Task<IDotNetCompilation> GetFunctionCompilationAsync() => _compilationService.GetFunctionCompilationAsync(_functionMetadata); |
67 | 65 |
|
68 | 66 | [Benchmark(Description = nameof(CSharpCompilationBenchmarks) + "." + nameof(CSharpCompilationBenchmarks.EmitAsync))] |
69 | | - public Task<DotNetCompilationResult> EmitAsync() => ScriptCompilation.EmitAsync(default); |
| 67 | + public Task<DotNetCompilationResult> EmitAsync() => _scriptCompilation.EmitAsync(default); |
70 | 68 |
|
71 | 69 | [Benchmark(Description = nameof(DotNetCompilationResult) + "." + nameof(DotNetCompilationResult.Load))] |
72 | | - public void Load() => ScriptAssembly.Load(FunctionMetadata,Resolver, NullLogger.Instance); |
| 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"); |
73 | 74 | } |
74 | 75 | } |
0 commit comments