diff --git a/samples/ComputeSharp.Benchmark/Dispatching/DispatchingBenchmark.cs b/samples/ComputeSharp.Benchmark/Dispatching/DispatchingBenchmark.cs new file mode 100644 index 00000000..dc85822e --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Dispatching/DispatchingBenchmark.cs @@ -0,0 +1,64 @@ +using System; +using BenchmarkDotNet.Attributes; + +namespace ComputeSharp.Benchmark.Blas; + +/// +/// A that benchmarks the shader dispatch times. +/// +public partial class DispatchingBenchmark : IDisposable +{ + /// + /// The test buffer. + /// + private ReadWriteBuffer? Buffer; + + /// + /// Initial setup for a benchmarking session. + /// + [GlobalSetup] + public void Setup() + { + Buffer = GraphicsDevice.Default.AllocateReadWriteBuffer(128); + } + + /// + [GlobalCleanup] + public void Dispose() + { + Buffer!.Dispose(); + } + + /// + /// Invokes two empty compute shaders one by one. + /// + [Benchmark(Baseline = true)] + public void Compute_Single() + { + GraphicsDevice.Default.For(64, new TestShader(Buffer!)); + GraphicsDevice.Default.For(64, new TestShader(Buffer!)); + } + + /// + /// Invokes two empty compute shaders in a batch. + /// + [Benchmark] + public void Compute_Batched() + { + using var context = GraphicsDevice.Default.CreateComputeContext(); + + context.For(64, new TestShader(Buffer!)); + context.For(64, new TestShader(Buffer!)); + } + + [AutoConstructor] + internal readonly partial struct TestShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + } + } +}