Skip to content
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<!-- Testing -->
<PackageVersion Include="MSTest" Version="4.5.3" />
<PackageVersion Include="Verify.MSTest" Version="33.4.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.16.0-preview.2" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions TagLibSharp2.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
</Folder>
<Project Path="src/TagLibSharp2/TagLibSharp2.csproj" />
<Project Path="tests/TagLibSharp2.Tests/TagLibSharp2.Tests.csproj" />
<Project Path="benchmarks/TagLibSharp2.Benchmarks/TagLibSharp2.Benchmarks.csproj" />
</Solution>
48 changes: 48 additions & 0 deletions benchmarks/TagLibSharp2.Benchmarks/MediaFileReadBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using TagLibSharp2.Core;

namespace TagLibSharp2.Benchmarks;

/// <summary>
/// One <see cref="MediaFile.Read(string)"/> of a file whose payload is 1 MiB.
/// The number to watch is Allocated: a windowed read stays far under that payload.
/// </summary>
[MemoryDiagnoser]
[SuppressMessage ("Performance", "CA1822", Justification = "BenchmarkDotNet requires instance methods")]
public class MediaFileReadBenchmark
{
const int PayloadBytes = 1024 * 1024;

[Params ("mp3", "wav", "flac")]
public string Format { get; set; } = "";

string _directory = "";
string _path = "";

[GlobalSetup]
public void Setup ()
{
_directory = Path.Combine (Path.GetTempPath (), "tls-bench-" + Guid.NewGuid ().ToString ("N"));
Directory.CreateDirectory (_directory);
_path = Path.Combine (_directory, "song." + Format);
File.WriteAllBytes (_path, SampleFiles.Create (Format, PayloadBytes));
}

[GlobalCleanup]
public void Cleanup ()
{
if (Directory.Exists (_directory))
Directory.Delete (_directory, true);
}

[Benchmark]
public int Read ()
{
var result = MediaFile.Read (_path);
var mark = result.IsSuccess ? (result.Tag?.Title?.Length ?? 0) + 1 : 0;
if (result.File is IDisposable disposable)
disposable.Dispose ();
return mark;
}
}
8 changes: 8 additions & 0 deletions benchmarks/TagLibSharp2.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BenchmarkDotNet.Running;
using TagLibSharp2.Benchmarks;

if (args.Length > 0 && args[0] == "--scan")
return ScanDriver.Run (args);

BenchmarkSwitcher.FromAssembly (typeof (Program).Assembly).Run (args);
return 0;
99 changes: 99 additions & 0 deletions benchmarks/TagLibSharp2.Benchmarks/SampleFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Buffers.Binary;
using TagLibSharp2.Id3;
using TagLibSharp2.Id3.Id3v2;

namespace TagLibSharp2.Benchmarks;

static class SampleFiles
{
public static readonly string[] Formats = ["mp3", "wav", "flac"];

public static byte[] Create (string format, int payloadBytes) => format switch {
"mp3" => Mp3 (payloadBytes),
"wav" => Wav (payloadBytes),
"flac" => Flac (payloadBytes),
_ => throw new ArgumentOutOfRangeException (nameof (format), format, "Unknown sample format")
};

static byte[] Mp3 (int payloadBytes)
{
var id3v2 = new Id3v2Tag { Title = "Window" }.Render ().ToArray ();
var header = new byte[] { 0xFF, 0xFB, 0x90, 0x00 };
const int FrameSize = 417;
var audio = new byte[FrameSize + 4];
header.CopyTo (audio, 0);
header.CopyTo (audio, FrameSize);
var id3v1 = new Id3v1Tag { Title = "Tail" }.Render ().ToArray ();
var file = new byte[id3v2.Length + audio.Length + payloadBytes + id3v1.Length];
var offset = 0;
id3v2.CopyTo (file, offset);
offset += id3v2.Length;
audio.CopyTo (file, offset);
offset += audio.Length + payloadBytes;
id3v1.CopyTo (file, offset);
return file;
}

static byte[] Wav (int payloadBytes)
{
var title = "Window\0"u8;
var listBody = 4 + 4 + 4 + title.Length;
var form = 4 + (8 + 16) + (8 + payloadBytes) + (8 + listBody);
var file = new byte[8 + form];
var span = file.AsSpan ();
"RIFF"u8.CopyTo (span);
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (4), (uint)form);
var o = 8;
"WAVE"u8.CopyTo (span.Slice (o));
o += 4;
"fmt "u8.CopyTo (span.Slice (o));
o += 4;
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), 16);
o += 4;
BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 1);
o += 2;
BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 1);
o += 2;
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), 44100);
o += 4;
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), 44100);
o += 4;
BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 1);
o += 2;
BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 8);
o += 2;
"data"u8.CopyTo (span.Slice (o));
o += 4;
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), (uint)payloadBytes);
o += 4 + payloadBytes;
"LIST"u8.CopyTo (span.Slice (o));
o += 4;
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), (uint)listBody);
o += 4;
"INFO"u8.CopyTo (span.Slice (o));
o += 4;
"INAM"u8.CopyTo (span.Slice (o));
o += 4;
BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), (uint)title.Length);
o += 4;
title.CopyTo (span.Slice (o));
return file;
}

static byte[] Flac (int payloadBytes)
{
var file = new byte[4 + 4 + 34 + payloadBytes];
"fLaC"u8.CopyTo (file);
file[4] = 0x80;
file[7] = 34;
var info = file.AsSpan (8, 34);
const int SampleRate = 44100;
info[10] = (byte)((SampleRate >> 12) & 0xFF);
info[11] = (byte)((SampleRate >> 4) & 0xFF);
info[12] = (byte)((SampleRate & 0x0F) << 4);
info[12] |= 0x02;
info[13] = 0xF0;
BinaryPrimitives.WriteUInt32BigEndian (info.Slice (14), 44100);
return file;
}
}
58 changes: 58 additions & 0 deletions benchmarks/TagLibSharp2.Benchmarks/ScanDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Diagnostics;
using TagLibSharp2.Core;

namespace TagLibSharp2.Benchmarks;

static class ScanDriver
{
public static int Run (string[] args)
{
var retain = args.Contains ("--retain");
var directory = Path.Combine (Path.GetTempPath (), "tls-scan-" + Guid.NewGuid ().ToString ("N"));
Directory.CreateDirectory (directory);
var paths = new string[SampleFiles.Formats.Length];
for (var i = 0; i < paths.Length; i++) {
paths[i] = Path.Combine (directory, "song." + SampleFiles.Formats[i]);
File.WriteAllBytes (paths[i], SampleFiles.Create (SampleFiles.Formats[i], 1024 * 1024));
var check = MediaFile.Read (paths[i]);
Console.WriteLine ($"{SampleFiles.Formats[i]} success={check.IsSuccess} title={check.Tag?.Title} error={check.Error}");
if (!check.IsSuccess)
return 1;
if (check.File is IDisposable disposable)
disposable.Dispose ();
}

var rounds = retain ? 40 : 300;
foreach (var arg in args) {
if (int.TryParse (arg, out var parsed) && parsed > 0)
rounds = parsed;
}
var held = new List<MediaFileResult> (retain ? rounds * paths.Length : 0);
var clock = Stopwatch.StartNew ();
var reads = 0;
for (var round = 0; round < rounds; round++) {
foreach (var path in paths) {
var result = MediaFile.Read (path);
reads++;
if (!result.IsSuccess) {
Console.Error.WriteLine (result.Error);
return 1;
}

if (retain)
held.Add (result);
else if (result.File is IDisposable disposable)
disposable.Dispose ();
}
}

clock.Stop ();
Console.WriteLine ($"reads={reads} ms={clock.ElapsedMilliseconds} retained={held.Count}");
if (!retain)
return 0;

Console.WriteLine ("READY");
Thread.Sleep (TimeSpan.FromSeconds (20));
return held.Count > 0 ? 0 : 1;
}
}
22 changes: 22 additions & 0 deletions benchmarks/TagLibSharp2.Benchmarks/TagLibSharp2.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<!-- net11 is local-only. CI installs the 8.0 and 10.0 SDKs. -->
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '11.0'))">$(TargetFrameworks);net11.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
<NoWarn>$(NoWarn);CA1515;CA1303</NoWarn>
<RootNamespace>TagLibSharp2.Benchmarks</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\TagLibSharp2\TagLibSharp2.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/TagLibSharp2/Aiff/AiffChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public class AiffChunk
Data = data;
}

/// <summary>
/// A chunk whose declared size is kept when the bytes were left in the file.
/// </summary>
internal static AiffChunk WithDeclaredSize (string fourCC, uint size, BinaryData data) =>
new (fourCC, size, data);

/// <summary>
/// Creates a new AiffChunk with the specified FourCC and data.
/// </summary>
Expand Down
Loading
Loading