|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using Microsoft.DurableTask.Worker; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | + |
| 8 | +namespace Microsoft.DurableTask.Benchmarks; |
| 9 | + |
| 10 | +[MemoryDiagnoser] |
| 11 | +public class DurableTaskRegistryBenchmarks |
| 12 | +{ |
| 13 | + readonly IServiceProvider services; |
| 14 | + |
| 15 | + public DurableTaskRegistryBenchmarks() |
| 16 | + { |
| 17 | + ServiceCollection services = new(); |
| 18 | + services.AddTransient<FromServicesActivity>(); |
| 19 | + this.services = services.BuildServiceProvider(); |
| 20 | + } |
| 21 | + |
| 22 | + [Benchmark] |
| 23 | + public void CreateActivity_FromServices() |
| 24 | + { |
| 25 | + DurableTaskRegistry registry = new(); |
| 26 | + TaskName name = nameof(FromServicesActivity); |
| 27 | + registry.AddActivity<FromServicesActivity>(name); |
| 28 | + IDurableTaskFactory factory = registry.BuildFactory(); |
| 29 | + |
| 30 | + for (int i = 0; i < 100; i++) |
| 31 | + { |
| 32 | + factory.TryCreateActivity(name, this.services, out _); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [Benchmark] |
| 37 | + public void CreateActivity_FromActivator() |
| 38 | + { |
| 39 | + DurableTaskRegistry registry = new(); |
| 40 | + TaskName name = nameof(FromActivatorActivity); |
| 41 | + registry.AddActivity<FromActivatorActivity>(name); |
| 42 | + IDurableTaskFactory factory = registry.BuildFactory(); |
| 43 | + |
| 44 | + for (int i = 0; i < 100; i++) |
| 45 | + { |
| 46 | + factory.TryCreateActivity(name, this.services, out _); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + class FromServicesActivity : TaskActivity<string, string> |
| 51 | + { |
| 52 | + public override Task<string> RunAsync(TaskActivityContext context, string input) |
| 53 | + { |
| 54 | + throw new NotImplementedException(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + class FromActivatorActivity : TaskActivity<string, string> |
| 59 | + { |
| 60 | + public override Task<string> RunAsync(TaskActivityContext context, string input) |
| 61 | + { |
| 62 | + throw new NotImplementedException(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments