DotTiled/DotTiled.Benchmark/Program.cs

93 lines
3.1 KiB
C#
Raw Normal View History

2024-07-28 23:50:16 +02:00
using System;
2024-08-03 21:07:31 +02:00
using System.Collections.Immutable;
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)]
public class MapLoading
2024-07-28 23:50:16 +02:00
{
2024-08-03 21:07:31 +02:00
private string _tmxPath = @"C:\Users\Daniel\winrepos\DotTiled\DotTiled.Tests\Serialization\Tmx\TestData\Map\empty-map-csv.tmx";
private string _tmxContents = "";
public MapLoading()
{
_tmxContents = System.IO.File.ReadAllText(_tmxPath);
}
[BenchmarkCategory("MapFromInMemoryTmxString")]
[Benchmark(Baseline = true, Description = "DotTiled")]
public DotTiled.Map LoadWithDotTiledFromInMemoryString()
{
using var stringReader = new StringReader(_tmxContents);
using var xmlReader = XmlReader.Create(stringReader);
using var mapReader = new DotTiled.TmxMapReader(xmlReader, _ => throw new Exception(), _ => throw new Exception());
return mapReader.ReadMap();
}
[BenchmarkCategory("MapFromTmxFile")]
[Benchmark(Baseline = true, Description = "DotTiled")]
public DotTiled.Map LoadWithDotTiledFromFile()
{
using var fileStream = System.IO.File.OpenRead(_tmxPath);
using var xmlReader = XmlReader.Create(fileStream);
using var mapReader = new DotTiled.TmxMapReader(xmlReader, _ => throw new Exception(), _ => throw new Exception());
return mapReader.ReadMap();
}
[BenchmarkCategory("MapFromInMemoryTmxString")]
[Benchmark(Description = "TiledLib")]
public TiledLib.Map LoadWithTiledLibFromInMemoryString()
{
using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(_tmxContents));
return TiledLib.Map.FromStream(memStream);
}
[BenchmarkCategory("MapFromTmxFile")]
[Benchmark(Description = "TiledLib")]
public TiledLib.Map LoadWithTiledLibFromFile()
2024-07-28 23:50:16 +02:00
{
2024-08-03 21:07:31 +02:00
using var fileStream = System.IO.File.OpenRead(_tmxPath);
var map = TiledLib.Map.FromStream(fileStream);
return map;
2024-07-28 23:50:16 +02:00
}
2024-08-03 21:07:31 +02:00
[BenchmarkCategory("MapFromInMemoryTmxString")]
[Benchmark(Description = "TiledCSPlus")]
public TiledCSPlus.TiledMap LoadWithTiledCSPlusFromInMemoryString()
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
}
2024-08-03 21:07:31 +02:00
[BenchmarkCategory("MapFromTmxFile")]
[Benchmark(Description = "TiledCSPlus")]
public TiledCSPlus.TiledMap LoadWithTiledCSPlusFromFile()
2024-07-28 23:50:16 +02:00
{
2024-08-03 21:07:31 +02:00
using var fileStream = System.IO.File.OpenRead(_tmxPath);
return new TiledCSPlus.TiledMap(fileStream);
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
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.AddDiagnoser(BenchmarkDotNet.Diagnosers.MemoryDiagnoser.Default);
var summary = BenchmarkRunner.Run<MapLoading>(config);
2024-07-28 23:50:16 +02:00
}
}
}