|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.VisualStudio.Composition.Benchmarks; |
| 5 | + |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Reflection; |
| 8 | +using BenchmarkDotNet.Attributes; |
| 9 | +using Microsoft.VSDiagnostics; |
| 10 | + |
| 11 | +[MemoryDiagnoser] |
| 12 | +[CPUUsageDiagnoser] |
| 13 | +public class CompositionBenchmarks |
| 14 | +{ |
| 15 | + private Resolver resolver = null!; |
| 16 | + private Assembly[] assemblies = null!; |
| 17 | + private ComposableCatalog catalog = null!; |
| 18 | + private RuntimeComposition runtime = null!; |
| 19 | + private byte[] serialized = null!; |
| 20 | + private IExportProviderFactory exportProviderFactory = null!; |
| 21 | + private ImportDefinition unconstrainedImport = null!; |
| 22 | + private ImportDefinition constrainedImport = null!; |
| 23 | + |
| 24 | + [GlobalSetup] |
| 25 | + public void Setup() |
| 26 | + { |
| 27 | + this.resolver = Resolver.DefaultInstance; |
| 28 | + this.assemblies = new[] { typeof(CompositionBenchmarks).Assembly }; |
| 29 | + |
| 30 | + var discovered = this.NewDiscovery().CreatePartsAsync(this.assemblies).GetAwaiter().GetResult(); |
| 31 | + this.catalog = ComposableCatalog.Create(this.resolver).AddParts(discovered); |
| 32 | + var config = CompositionConfiguration.Create(this.catalog); |
| 33 | + this.runtime = RuntimeComposition.CreateRuntimeComposition(config); |
| 34 | + |
| 35 | + using var ms = new MemoryStream(); |
| 36 | + new CachedComposition().SaveAsync(this.runtime, ms).GetAwaiter().GetResult(); |
| 37 | + this.serialized = ms.ToArray(); |
| 38 | + |
| 39 | + this.exportProviderFactory = this.runtime.CreateExportProviderFactory(); |
| 40 | + |
| 41 | + // Two imports for the catalog's most-exported contract: one with no export constraints and |
| 42 | + // one with a type-identity constraint. Both resolve the same exports, so the delta between |
| 43 | + // the GetExports* benchmarks isolates (and guards) the no-constraint fast path. |
| 44 | + string contractName = this.catalog.Parts |
| 45 | + .SelectMany(p => p.ExportedTypes) |
| 46 | + .GroupBy(e => e.ContractName) |
| 47 | + .OrderByDescending(g => g.Count()) |
| 48 | + .First().Key; |
| 49 | + var emptyMetadata = ImmutableDictionary<string, object?>.Empty; |
| 50 | + this.unconstrainedImport = new ImportDefinition( |
| 51 | + contractName, |
| 52 | + ImportCardinality.ZeroOrMore, |
| 53 | + emptyMetadata, |
| 54 | + ImmutableList<IImportSatisfiabilityConstraint>.Empty); |
| 55 | + this.constrainedImport = new ImportDefinition( |
| 56 | + contractName, |
| 57 | + ImportCardinality.ZeroOrMore, |
| 58 | + emptyMetadata, |
| 59 | + new IImportSatisfiabilityConstraint[] { new ExportTypeIdentityConstraint(typeof(IService)) }); |
| 60 | + } |
| 61 | + |
| 62 | + [Benchmark] |
| 63 | + public int Discovery() |
| 64 | + { |
| 65 | + var discovered = this.NewDiscovery().CreatePartsAsync(this.assemblies).GetAwaiter().GetResult(); |
| 66 | + return discovered.Parts.Count; |
| 67 | + } |
| 68 | + |
| 69 | + [Benchmark] |
| 70 | + public int Composition() |
| 71 | + { |
| 72 | + var configuration = CompositionConfiguration.Create(this.catalog); |
| 73 | + return configuration.Parts.Count; |
| 74 | + } |
| 75 | + |
| 76 | + [Benchmark] |
| 77 | + public long Serialize() |
| 78 | + { |
| 79 | + using var ms = new MemoryStream(this.serialized.Length); |
| 80 | + new CachedComposition().SaveAsync(this.runtime, ms).GetAwaiter().GetResult(); |
| 81 | + return ms.Length; |
| 82 | + } |
| 83 | + |
| 84 | + [Benchmark] |
| 85 | + public int Deserialize() |
| 86 | + { |
| 87 | + using var ms = new MemoryStream(this.serialized, writable: false); |
| 88 | + var loaded = new CachedComposition().LoadRuntimeCompositionAsync(ms, this.resolver).GetAwaiter().GetResult(); |
| 89 | + return loaded.Parts.Count; |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public int Runtime() |
| 94 | + { |
| 95 | + int count = 0; |
| 96 | + using var exportProvider = this.exportProviderFactory.CreateExportProvider(); |
| 97 | + foreach (var processor in exportProvider.GetExportedValues<IProcessor>()) |
| 98 | + { |
| 99 | + count++; |
| 100 | + } |
| 101 | + |
| 102 | + foreach (var service in exportProvider.GetExportedValues<IService>()) |
| 103 | + { |
| 104 | + count++; |
| 105 | + } |
| 106 | + |
| 107 | + return count; |
| 108 | + } |
| 109 | + |
| 110 | + [Benchmark] |
| 111 | + public int GetExportsUnconstrained() => this.catalog.GetExports(this.unconstrainedImport).Count; |
| 112 | + |
| 113 | + [Benchmark] |
| 114 | + public int GetExportsConstrained() => this.catalog.GetExports(this.constrainedImport).Count; |
| 115 | + |
| 116 | + private PartDiscovery NewDiscovery() => PartDiscovery.Combine( |
| 117 | + this.resolver, |
| 118 | + new AttributedPartDiscovery(this.resolver, isNonPublicSupported: true), |
| 119 | + new AttributedPartDiscoveryV1(this.resolver)); |
| 120 | +} |
0 commit comments