|
3 | 3 |
|
4 | 4 | namespace Masa.Contrib.Data.IdGenerator.Snowflake.BenchmarkDotnet.Tests;
|
5 | 5 |
|
| 6 | +[MarkdownExporter, AsciiDocExporter, HtmlExporter] |
6 | 7 | [SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 1000)]
|
7 | 8 | [SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 10000)]
|
8 | 9 | [SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100000)]
|
9 | 10 | [MinColumn, MaxColumn, MeanColumn, MedianColumn]
|
10 | 11 | public class Benchmarks
|
11 | 12 | {
|
12 | 13 | private IIdGenerator<System.Snowflake, long> _idGenerator;
|
| 14 | + private IIdGenerator<System.Snowflake, long> _idGeneratorBySecond; |
| 15 | + private IIdGenerator<System.Snowflake, long> _idGeneratorByEnableMachineClock; |
| 16 | + private IIdGenerator<System.Snowflake, long> _idGeneratorBySecondAndEnableMachineClock; |
13 | 17 |
|
14 | 18 | [GlobalSetup]
|
15 | 19 | public void GlobalSetup()
|
16 | 20 | {
|
17 |
| - IServiceCollection services = new ServiceCollection(); |
18 |
| - services.AddSnowflake(); |
19 |
| - var serviceProvider = services.BuildServiceProvider(); |
20 |
| - _idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>(); |
21 |
| - _idGenerator.NewId(); |
| 21 | + _idGenerator = InitializeIdGenerator(services => services.AddSnowflake()); |
| 22 | + _idGeneratorBySecond = |
| 23 | + InitializeIdGenerator(services => services.AddSnowflake(options => options.TimestampType = TimestampType.Seconds)); |
| 24 | + _idGeneratorByEnableMachineClock = |
| 25 | + InitializeIdGenerator(services => services.AddSnowflake(options => options.EnableMachineClock = true)); |
| 26 | + _idGeneratorBySecondAndEnableMachineClock = |
| 27 | + InitializeIdGenerator(services => services.AddSnowflake(options => |
| 28 | + { |
| 29 | + options.EnableMachineClock = true; |
| 30 | + options.TimestampType = TimestampType.Seconds; |
| 31 | + })); |
22 | 32 | }
|
23 | 33 |
|
24 |
| - [Benchmark] |
25 |
| - public void SnowflakeByMillisecond() |
26 |
| - { |
27 |
| - _idGenerator.NewId(); |
28 |
| - } |
29 |
| -} |
30 |
| - |
31 |
| -[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 1000)] |
32 |
| -[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 10000)] |
33 |
| -[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100000)] |
34 |
| -[MinColumn, MaxColumn, MeanColumn, MedianColumn] |
35 |
| -public class SecondBenchmarks |
36 |
| -{ |
37 |
| - private IIdGenerator<System.Snowflake, long> _idGenerator; |
38 |
| - |
39 |
| - [GlobalSetup] |
40 |
| - public void GlobalSetup() |
| 34 | + private IIdGenerator<System.Snowflake, long> InitializeIdGenerator(Action<IServiceCollection> action) |
41 | 35 | {
|
42 | 36 | IServiceCollection services = new ServiceCollection();
|
43 |
| - services.AddSnowflake(options => options.TimestampType = TimestampType.Seconds); |
| 37 | + action.Invoke(services); |
44 | 38 | var serviceProvider = services.BuildServiceProvider();
|
45 |
| - _idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>(); |
46 |
| - _idGenerator.NewId(); |
| 39 | + var idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>(); |
| 40 | + idGenerator.NewId(); |
| 41 | + return idGenerator; |
47 | 42 | }
|
48 | 43 |
|
| 44 | + [Benchmark(Baseline = true)] |
| 45 | + public void SnowflakeByMillisecond() |
| 46 | + => _idGenerator.NewId(); |
| 47 | + |
49 | 48 | [Benchmark]
|
50 | 49 | public void SnowflakeBySecond()
|
51 |
| - { |
52 |
| - _idGenerator.NewId(); |
53 |
| - } |
| 50 | + => _idGeneratorBySecond.NewId(); |
| 51 | + |
| 52 | + [Benchmark] |
| 53 | + public void SnowflakeByMillisecondAndEnableMachineClock() |
| 54 | + => _idGeneratorByEnableMachineClock.NewId(); |
| 55 | + |
| 56 | + [Benchmark] |
| 57 | + public void SnowflakeBySecondAndEnableMachineClock() |
| 58 | + => _idGeneratorBySecondAndEnableMachineClock.NewId(); |
54 | 59 | }
|
0 commit comments