Skip to content

Commit a821bd6

Browse files
authored
Merge pull request #40 from filzrev/chore-modify-benchmarks2
chore: Add benchmark workflow and refactor benchmark projects
2 parents c2ba6fe + 9ecf97f commit a821bd6

File tree

6 files changed

+219
-133
lines changed

6 files changed

+219
-133
lines changed

.github/workflows/benchmark.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Benchmark
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
benchmark:
8+
runs-on: ubuntu-latest
9+
timeout-minutes: 60 # Default: 360 minutes
10+
strategy:
11+
fail-fast: true
12+
steps:
13+
- uses: Cysharp/Actions/.github/actions/checkout@main
14+
- uses: Cysharp/Actions/.github/actions/setup-dotnet@main
15+
- run: dotnet build -c Release
16+
17+
- name: Run Benchmarks
18+
working-directory: sandbox/Benchmark
19+
run: |
20+
dotnet run -c Release --no-build --no-launch-profile -- --filter "*" --logBuildOutput BenchmarkDotNet.Artifacts --exporters GitHub
21+
22+
- name: Upload artifacts
23+
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #v4.6.1
24+
with:
25+
name: BenchmarkDotNet.Artifacts
26+
path: sandbox/Benchmark/BenchmarkDotNet.Artifacts/
27+
if-no-files-found: error
28+
29+
- name: Output results to JobSummary
30+
working-directory: sandbox/Benchmark
31+
shell: pwsh
32+
run: |
33+
$items = Get-ChildItem "BenchmarkDotNet.Artifacts/results/*.md"
34+
foreach($item in $items) {
35+
Write-Output ('## {0}' -f $item.Name) >> $env:GITHUB_STEP_SUMMARY
36+
Write-Output (Get-Content $item.FullName -Raw) >> $env:GITHUB_STEP_SUMMARY
37+
}

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,7 @@ src/ZLinq.Unity/obj/*
128128
src/ZLinq.Unity/Temp/*
129129
src/ZLinq.Unity/UserSettings/*
130130
src/ZLinq.Unity/*.sln
131-
src/ZLinq.Unity/*.csproj
131+
src/ZLinq.Unity/*.csproj
132+
133+
# BenchmarkDotNet
134+
BenchmarkDotNet.Artifacts/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Order;
3+
using Cathei.LinqGen;
4+
using SpanLinq;
5+
using StructLinq;
6+
using ZLinq;
7+
8+
namespace Benchmark;
9+
10+
[ShortRunJob]
11+
[MemoryDiagnoser]
12+
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
13+
public class IterateBenchmark
14+
{
15+
int[] array = Enumerable.Range(1, 10000).ToArray();
16+
17+
public IterateBenchmark()
18+
{
19+
20+
}
21+
22+
[Benchmark]
23+
public void SystemLinq()
24+
{
25+
var seq = array
26+
.Select(x => x * 3)
27+
.Where(x => x % 2 == 0);
28+
29+
foreach (var item in seq)
30+
{
31+
}
32+
}
33+
34+
[Benchmark]
35+
public void ZLinq()
36+
{
37+
var seq = array.AsValueEnumerable()
38+
.Select(x => x * 3)
39+
.Where(x => x % 2 == 0);
40+
41+
foreach (var item in seq) { }
42+
}
43+
44+
//[Benchmark]
45+
//public void ZLinqSpan()
46+
//{
47+
// var seq = array.AsSpan().AsValueEnumerable()
48+
// .Select<SpanValueEnumerable<int>, int, int>(x => x * 3)
49+
// .Where<SelectValueEnumerable<SpanValueEnumerable<int>, int, int>, int>(x => x % 2 == 0);
50+
51+
// foreach (var item in seq)
52+
// {
53+
54+
// }
55+
//}
56+
57+
[Benchmark]
58+
public void LinqGen()
59+
{
60+
var seq = array.Gen()
61+
.Select(x => x * 3)
62+
.Where(x => x % 2 == 0);
63+
64+
foreach (var item in seq)
65+
{
66+
67+
}
68+
}
69+
70+
[Benchmark]
71+
public void LinqAf()
72+
{
73+
var seq = LinqAF.ArrayExtensionMethods
74+
.Select(array, x => x * 3)
75+
.Where(x => x % 2 == 0);
76+
77+
foreach (var item in seq)
78+
{
79+
80+
}
81+
}
82+
83+
//[Benchmark]
84+
//public void StructLinq()
85+
//{
86+
// var seq = array.ToValueEnumerable()
87+
// .Select(x => x * 3, x => x)
88+
// .Where(x => x % 2 == 0, x => x);
89+
90+
// foreach (var item in seq)
91+
// {
92+
93+
// }
94+
//}
95+
96+
[Benchmark]
97+
public void SpanLinq()
98+
{
99+
var seq = array.AsSpan()
100+
.Select(x => x * 3)
101+
.Where(x => x % 2 == 0);
102+
103+
foreach (var item in seq)
104+
{
105+
106+
}
107+
}
108+
}

sandbox/Benchmark/Program.cs

+19-132
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,37 @@
1-
using BenchmarkDotNet.Attributes;
2-
using BenchmarkDotNet.Configs;
3-
using BenchmarkDotNet.Order;
1+
using BenchmarkDotNet.Configs;
42
using BenchmarkDotNet.Reports;
53
using BenchmarkDotNet.Running;
6-
using Perfolizer.Horology;
74
using Cathei.LinqGen;
5+
using Perfolizer.Horology;
86
using SpanLinq;
9-
using StructLinq;
107
using ZLinq;
11-
using ZLinq.Linq;
12-
using Benchmark;
13-
14-
#if !DEBUG
15-
16-
//BenchmarkRunner.Run<IterateBenchmark>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
17-
//BenchmarkRunner.Run<SimdRange>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
18-
//BenchmarkRunner.Run<SimdSelect>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
19-
//BenchmarkRunner.Run<LookupBattle>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
20-
// BenchmarkRunner.Run<CopyBattle>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
21-
22-
// BenchmarkRunner.Run<SimdSum>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default), args);
23-
//BenchmarkRunner.Run<SimdSumUnsigned>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default), args);
24-
25-
//BenchmarkRunner.Run<SimdAny>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default), args);
26-
27-
28-
//BenchmarkRunner.Run<ReadMeBenchmark>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default), args);
29-
30-
//BenchmarkRunner.Run<LinqPerfBenchmarks.AggregateBy00>(DefaultConfig.Instance, args);
31-
//BenchmarkRunner.Run<LinqPerfBenchmarks.Count00>(DefaultConfig.Instance, args);
32-
//BenchmarkRunner.Run<LinqPerfBenchmarks.CountBy00>(DefaultConfig.Instance, args);
33-
//BenchmarkRunner.Run<LinqPerfBenchmarks.GroupBy00>(DefaultConfig.Instance, args);
34-
//BenchmarkRunner.Run<LinqPerfBenchmarks.Order00>(DefaultConfig.Instance, args);
35-
BenchmarkRunner.Run<LinqPerfBenchmarks.Where00>(DefaultConfig.Instance, args);
36-
//BenchmarkRunner.Run<LinqPerfBenchmarks.Where01>(DefaultConfig.Instance, args);
37-
#else
38-
39-
BenchmarkRunner.Run<IterateBenchmark>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
40-
41-
var i = 0;
42-
foreach (var item in typeof(Enumerable).GetMethods().GroupBy(x => x.Name))
43-
{
44-
Console.WriteLine($"- [ ] {item.Key}");
45-
i++;
46-
}
47-
Console.WriteLine(i);
488

9+
namespace Benchmark;
4910

50-
#endif
5111

52-
[ShortRunJob]
53-
[MemoryDiagnoser]
54-
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
55-
public class IterateBenchmark
12+
internal static class Program
5613
{
57-
int[] array = Enumerable.Range(1, 10000).ToArray();
58-
59-
public IterateBenchmark()
60-
{
61-
62-
}
63-
64-
[Benchmark]
65-
public void SystemLinq()
66-
{
67-
var seq = array
68-
.Select(x => x * 3)
69-
.Where(x => x % 2 == 0);
70-
71-
foreach (var item in seq)
72-
{
73-
}
74-
}
75-
76-
[Benchmark]
77-
public void ZLinq()
78-
{
79-
var seq = array.AsValueEnumerable()
80-
.Select(x => x * 3)
81-
.Where(x => x % 2 == 0);
82-
83-
foreach (var item in seq) { }
84-
}
85-
86-
//[Benchmark]
87-
//public void ZLinqSpan()
88-
//{
89-
// var seq = array.AsSpan().AsValueEnumerable()
90-
// .Select<SpanValueEnumerable<int>, int, int>(x => x * 3)
91-
// .Where<SelectValueEnumerable<SpanValueEnumerable<int>, int, int>, int>(x => x % 2 == 0);
92-
93-
// foreach (var item in seq)
94-
// {
95-
96-
// }
97-
//}
98-
99-
[Benchmark]
100-
public void LinqGen()
101-
{
102-
var seq = array.Gen()
103-
.Select(x => x * 3)
104-
.Where(x => x % 2 == 0);
105-
106-
foreach (var item in seq)
107-
{
108-
109-
}
110-
}
111-
112-
[Benchmark]
113-
public void LinqAf()
14+
public static int Main(string[] args)
11415
{
115-
var seq = LinqAF.ArrayExtensionMethods
116-
.Select(array, x => x * 3)
117-
.Where(x => x % 2 == 0);
16+
#if DEBUG
17+
BenchmarkRunner.Run<IterateBenchmark>(DefaultConfig.Instance.WithSummaryStyle(SummaryStyle.Default.WithTimeUnit(TimeUnit.Millisecond)), args);
11818

119-
foreach (var item in seq)
19+
var i = 0;
20+
foreach (var item in typeof(Enumerable).GetMethods().GroupBy(x => x.Name))
12021
{
121-
22+
Console.WriteLine($"- [ ] {item.Key}");
23+
i++;
12224
}
123-
}
124-
125-
//[Benchmark]
126-
//public void StructLinq()
127-
//{
128-
// var seq = array.ToValueEnumerable()
129-
// .Select(x => x * 3, x => x)
130-
// .Where(x => x % 2 == 0, x => x);
131-
132-
// foreach (var item in seq)
133-
// {
134-
135-
// }
136-
//}
25+
Console.WriteLine(i);
26+
return 0;
27+
#endif
13728

138-
[Benchmark]
139-
public void SpanLinq()
140-
{
141-
var seq = array.AsSpan()
142-
.Select(x => x * 3)
143-
.Where(x => x % 2 == 0);
29+
if (args != null && args.Length != 0)
30+
Console.WriteLine($"Start ZLinq benchmarks with args: {string.Join(' ', args)}");
14431

145-
foreach (var item in seq)
146-
{
32+
var switcher = BenchmarkSwitcher.FromAssemblies([typeof(Program).Assembly]);
33+
switcher.Run(args).ToArray();
14734

148-
}
35+
return 0;
14936
}
15037
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"profiles": {
3+
"Default": {
4+
"commandName": "Project",
5+
"commandLineArgs": "--filter Benchmark.IterateBenchmark.*",
6+
7+
// Available benchmarks:
8+
// "commandLineArgs": "--filter Benchmark.IterateBenchmark.*",
9+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.AggregateBy00.*",
10+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Count00.*",
11+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.CountBy00.*",
12+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.GroupBy00.*",
13+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Order00.*",
14+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Where00.*",
15+
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Where01.*",
16+
// "commandLineArgs": "--filter Benchmark.SimdRange.*",
17+
// "commandLineArgs": "--filter Benchmark.SimdSelect.*",
18+
// "commandLineArgs": "--filter Benchmark.LookupBattle.*",
19+
// "commandLineArgs": "--filter Benchmark.LookupBattle.*",
20+
// "commandLineArgs": "--filter Benchmark.ReadMeBenchmark.*",
21+
// "commandLineArgs": "--filter Benchmark.ShuffleBench.*",
22+
// "commandLineArgs": "--filter Benchmark.SimdAny.*",
23+
// "commandLineArgs": "--filter Benchmark.SimdRange.*",
24+
// "commandLineArgs": "--filter Benchmark.SimdSelect.*",
25+
// "commandLineArgs": "--filter Benchmark.SimdSum.*",
26+
// "commandLineArgs": "--filter Benchmark.SimdSumUnsigned.*",
27+
28+
"workingDirectory": "."
29+
},
30+
"Select Benchmark": {
31+
"commandName": "Project",
32+
"commandLineArgs": ""
33+
},
34+
"Run All Benchmarks": {
35+
"commandName": "Project",
36+
"commandLineArgs": "--filter *",
37+
},
38+
"Run All Benchmarks with --join": {
39+
"commandName": "Project",
40+
"commandLineArgs": "--filter * --join",
41+
},
42+
"List Benchmark": {
43+
"commandName": "Project",
44+
"commandLineArgs": "--list tree",
45+
},
46+
"Help": {
47+
"commandName": "Project",
48+
"commandLineArgs": "--help",
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)