DotTiled/DotTiled.Benchmark/Program.cs

95 lines
3.4 KiB
C#
Raw Normal View History

using System;
2024-08-03 21:07:31 +02:00
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
2024-07-28 23:50:16 +02:00
using System.Security.Cryptography;
2024-08-03 21:07:31 +02:00
using System.Text;
using System.Xml;
2024-07-28 23:50:16 +02:00
using BenchmarkDotNet.Attributes;
2024-08-03 21:07:31 +02:00
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Reports;
2024-07-28 23:50:16 +02:00
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
2024-08-03 21:07:31 +02:00
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
2024-08-11 12:10:56 +02:00
[HideColumns(["StdDev", "Error", "RatioSD"])]
2024-08-03 21:07:31 +02:00
public class MapLoading
2024-07-28 23:50:16 +02:00
{
private string _tmxPath = @"DotTiled.Tests/Serialization/TestData/Map/default-map/default-map.tmx";
2024-08-03 21:07:31 +02:00
private string _tmxContents = "";
private string _tmjPath = @"DotTiled.Tests/Serialization/TestData/Map/default-map/default-map.tmj";
2024-08-11 12:10:56 +02:00
private string _tmjContents = "";
2024-08-03 21:07:31 +02:00
public MapLoading()
{
var basePath = Path.GetDirectoryName(WhereAmI())!;
var tmxPath = Path.Combine(basePath, $"../{_tmxPath}");
var tmjPath = Path.Combine(basePath, $"../{_tmjPath}");
_tmxContents = System.IO.File.ReadAllText(tmxPath);
_tmjContents = System.IO.File.ReadAllText(tmjPath);
2024-08-03 21:07:31 +02:00
}
static string WhereAmI([CallerFilePath] string callerFilePath = "") => callerFilePath;
2024-08-03 21:07:31 +02:00
[BenchmarkCategory("MapFromInMemoryTmxString")]
[Benchmark(Baseline = true, Description = "DotTiled")]
2024-08-11 12:10:56 +02:00
public DotTiled.Map LoadWithDotTiledFromInMemoryTmxString()
2024-08-03 21:07:31 +02:00
{
using var stringReader = new StringReader(_tmxContents);
using var xmlReader = XmlReader.Create(stringReader);
2024-08-11 12:10:56 +02:00
using var mapReader = new DotTiled.TmxMapReader(xmlReader, _ => throw new Exception(), _ => throw new Exception(), []);
2024-08-03 21:07:31 +02:00
return mapReader.ReadMap();
}
2024-08-11 12:10:56 +02:00
[BenchmarkCategory("MapFromInMemoryTmjString")]
2024-08-03 21:07:31 +02:00
[Benchmark(Baseline = true, Description = "DotTiled")]
2024-08-11 12:10:56 +02:00
public DotTiled.Map LoadWithDotTiledFromInMemoryTmjString()
2024-08-03 21:07:31 +02:00
{
2024-08-11 12:10:56 +02:00
using var mapReader = new DotTiled.TmjMapReader(_tmjContents, _ => throw new Exception(), _ => throw new Exception(), []);
2024-08-03 21:07:31 +02:00
return mapReader.ReadMap();
}
[BenchmarkCategory("MapFromInMemoryTmxString")]
[Benchmark(Description = "TiledLib")]
2024-08-11 12:10:56 +02:00
public TiledLib.Map LoadWithTiledLibFromInMemoryTmxString()
2024-08-03 21:07:31 +02:00
{
using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(_tmxContents));
return TiledLib.Map.FromStream(memStream);
}
[BenchmarkCategory("MapFromInMemoryTmjString")]
[Benchmark(Description = "TiledLib")]
public TiledLib.Map LoadWithTiledLibFromInMemoryTmjString()
{
using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(_tmjContents));
return TiledLib.Map.FromStream(memStream);
}
2024-08-03 21:07:31 +02:00
[BenchmarkCategory("MapFromInMemoryTmxString")]
[Benchmark(Description = "TiledCSPlus")]
2024-08-11 12:10:56 +02:00
public TiledCSPlus.TiledMap LoadWithTiledCSPlusFromInMemoryTmxString()
2024-07-28 23:50:16 +02:00
{
2024-08-03 21:07:31 +02:00
using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(_tmxContents));
return new TiledCSPlus.TiledMap(memStream);
2024-07-28 23:50:16 +02:00
}
}
public class Program
{
public static void Main(string[] args)
{
2024-08-03 21:07:31 +02:00
var config = BenchmarkDotNet.Configs.DefaultConfig.Instance
2024-08-11 12:10:56 +02:00
.WithArtifactsPath(args[0])
2024-08-03 21:07:31 +02:00
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.AddDiagnoser(BenchmarkDotNet.Diagnosers.MemoryDiagnoser.Default);
var summary = BenchmarkRunner.Run<MapLoading>(config);
2024-07-28 23:50:16 +02:00
}
}
}