Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update benchmark #124

Merged
merged 6 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,57 @@

namespace Masa.Contrib.Data.IdGenerator.Snowflake.BenchmarkDotnet.Tests;

[MarkdownExporter, AsciiDocExporter, HtmlExporter]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 1000)]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 10000)]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100000)]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
public class Benchmarks
{
private IIdGenerator<System.Snowflake, long> _idGenerator;
private IIdGenerator<System.Snowflake, long> _idGeneratorBySecond;
private IIdGenerator<System.Snowflake, long> _idGeneratorByEnableMachineClock;
private IIdGenerator<System.Snowflake, long> _idGeneratorBySecondAndEnableMachineClock;

[GlobalSetup]
public void GlobalSetup()
{
IServiceCollection services = new ServiceCollection();
services.AddSnowflake();
var serviceProvider = services.BuildServiceProvider();
_idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>();
_idGenerator.NewId();
_idGenerator = InitializeIdGenerator(services => services.AddSnowflake());
_idGeneratorBySecond =
InitializeIdGenerator(services => services.AddSnowflake(options => options.TimestampType = TimestampType.Seconds));
_idGeneratorByEnableMachineClock =
InitializeIdGenerator(services => services.AddSnowflake(options => options.EnableMachineClock = true));
_idGeneratorBySecondAndEnableMachineClock =
InitializeIdGenerator(services => services.AddSnowflake(options =>
{
options.EnableMachineClock = true;
options.TimestampType = TimestampType.Seconds;
}));
}

[Benchmark]
public void SnowflakeByMillisecond()
{
_idGenerator.NewId();
}
}

[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 1000)]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 10000)]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100000)]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
public class SecondBenchmarks
{
private IIdGenerator<System.Snowflake, long> _idGenerator;

[GlobalSetup]
public void GlobalSetup()
private IIdGenerator<System.Snowflake, long> InitializeIdGenerator(Action<IServiceCollection> action)
{
IServiceCollection services = new ServiceCollection();
services.AddSnowflake(options => options.TimestampType = TimestampType.Seconds);
action.Invoke(services);
var serviceProvider = services.BuildServiceProvider();
_idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>();
_idGenerator.NewId();
var idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>();
idGenerator.NewId();
return idGenerator;
}

[Benchmark(Baseline = true)]
public void SnowflakeByMillisecond()
=> _idGenerator.NewId();

[Benchmark]
public void SnowflakeBySecond()
{
_idGenerator.NewId();
}
=> _idGeneratorBySecond.NewId();

[Benchmark]
public void SnowflakeByMillisecondAndEnableMachineClock()
=> _idGeneratorByEnableMachineClock.NewId();

[Benchmark]
public void SnowflakeBySecondAndEnableMachineClock()
=> _idGeneratorBySecondAndEnableMachineClock.NewId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,43 @@

namespace Masa.Contrib.Data.IdGenerator.Snowflake.BenchmarkDotnet.Tests;

/// <summary>
/// Only supports the use of Redis environment
/// </summary>
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 1000)]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 10000)]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100000)]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
public class DistributedBenchmarks
{
private IIdGenerator<System.Snowflake, long> _idGenerator;
// private IIdGenerator<System.Snowflake, long> _idGenerator;

[GlobalSetup]
public void GlobalSetup()
{
IServiceCollection services = new ServiceCollection();
services.AddMasaRedisCache(opt =>
{
opt.Password = "";
opt.DefaultDatabase = 2;
opt.Servers = new List<RedisServerOptions>()
{
new("127.0.0.1", 6379)
};
});
services.AddSnowflake(options =>
{
options.UseRedis();
options.EnableMachineClock = true;
});
var serviceProvider = services.BuildServiceProvider();
_idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>();
_idGenerator.NewId();
}

[Benchmark]
public void Distributed()
{
_idGenerator.NewId();
}
// [GlobalSetup]
// public void GlobalSetup()
// {
// IServiceCollection services = new ServiceCollection();
// services.AddMasaRedisCache(opt =>
// {
// opt.Password = "";
// opt.DefaultDatabase = 2;
// opt.Servers = new List<RedisServerOptions>()
// {
// new("127.0.0.1", 6379)
// };
// });
// services.AddSnowflake(options =>
// {
// options.UseRedis();
// options.EnableMachineClock = true;
// });
// var serviceProvider = services.BuildServiceProvider();
// _idGenerator = serviceProvider.GetRequiredService<IIdGenerator<System.Snowflake, long>>();
// _idGenerator.NewId();
// }
//
// [Benchmark]
// public void Distributed()
// {
// _idGenerator.NewId();
// }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ static void Main(string[] args)
var config = DefaultConfig.Instance
.AddValidator(ExecutionValidator.FailOnError)
.WithOptions(ConfigOptions.DisableOptimizationsValidator);
// BenchmarkRunner.Run<Benchmarks>(config);
// BenchmarkRunner.Run<SecondBenchmarks>(config);
BenchmarkRunner.Run<MachineClockBenchmarks>(config);
BenchmarkRunner.Run<Benchmarks>(config);
// BenchmarkRunner.Run<DistributedBenchmarks>(config);
Console.ReadLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Masa.Contrib.Dispatcher.Events.BenchmarkDotnet.Tests;

[MarkdownExporter, AsciiDocExporter, HtmlExporter]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100)]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
public class Benchmarks
Expand Down Expand Up @@ -35,7 +36,7 @@ public void GlobalSetup()
};
}

[Benchmark]
[Benchmark(Baseline = true)]
public async Task SendCouponByDirect()
{
var _couponHandler = new CouponHandler(_serviceProvider);
Expand Down