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

Add a benchmark for SKData #2129

Closed
wants to merge 7 commits into from
Closed
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
106 changes: 106 additions & 0 deletions benchmarks/SkiaSharp.Benchmarks/Benchmarks/SKData/CreateFromStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Buffers;
using System.IO;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnostics.Windows.Configs;
using BenchmarkDotNet.Jobs;

namespace SkiaSharp.Benchmarks;

[EtwProfiler]
// [NativeMemoryProfiler]
[MemoryDiagnoser]
// [SimpleJob(RuntimeMoniker.Mono)]
// [SimpleJob(RuntimeMoniker.Net472)]
// [SimpleJob(RuntimeMoniker.NetCoreApp31)]
[SimpleJob(RuntimeMoniker.Net60)]
public class SKDataCreateFromStream
{
private int iterations;

private byte[] data;

private Stream seekable;
private Stream nonseekable;
private byte[] buffer;

[Params(1, 100, 1_000, 10_000)]
public int SizeKB;

[GlobalSetup]
public void GlobalSetup()
{
data = new byte[SizeKB * 1024];
var rnd = new Random();
rnd.NextBytes(data);

seekable = new MemoryStream(data);
nonseekable = new NonSeekableReadOnlyStream(seekable);

buffer = ArrayPool<byte>.Shared.Rent(SKData.CopyBufferSize);

iterations = 100_000 / SizeKB;

// init skiasharp static things
SKObject.GetInstance<SKObject>(IntPtr.Zero, out _);
}

[GlobalCleanup]
public void GlobalCleanup()
{
nonseekable.Dispose();
seekable.Dispose();

data = null;

ArrayPool<byte>.Shared.Return(buffer);
}

[Benchmark]
public void Seekable()
{
for (var i = 0; i < iterations; i++)
{
seekable.Position = 0;
var skdata = SKData.Create(seekable);
skdata.Dispose();
}
}

[Benchmark]
public void NonSeekable()
{
for (var i = 0; i < iterations; i++)
{
seekable.Position = 0;
var skdata = SKData.Create(nonseekable);
skdata.Dispose();
}
}

[Benchmark]
public unsafe void PInvoke()
{
for (var i = 0; i < iterations; i++)
{
seekable.Position = 0;
fixed (byte* ptr = buffer)
{
var memory = SkiaApi.sk_dynamicmemorywstream_new();

int len;
while ((len = nonseekable.Read(buffer, 0, buffer.Length)) > 0)
{
SkiaApi.sk_wstream_write(memory, ptr, (IntPtr)len);
}
SkiaApi.sk_wstream_flush(memory);

var skdata = SkiaApi.sk_dynamicmemorywstream_detach_as_data(memory);

SkiaApi.sk_dynamicmemorywstream_destroy(memory);

SkiaApi.sk_data_unref(skdata);
}
}
}
}
2 changes: 1 addition & 1 deletion benchmarks/SkiaSharp.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Benchmark>();
var summary = BenchmarkRunner.Run<SKDataCreateFromStream>();
}
}