diff --git a/src/Tests/CompileTests/NativeAot/Program.cs b/src/Tests/CompileTests/NativeAot/Program.cs index c4f65158..a098acbf 100644 --- a/src/Tests/CompileTests/NativeAot/Program.cs +++ b/src/Tests/CompileTests/NativeAot/Program.cs @@ -1,4 +1,6 @@ using FlatSharp; +using System.Diagnostics; +using System.Text; namespace NativeAot { @@ -6,14 +8,15 @@ internal class Program { static void Main(string[] args) { + IndexedVector indexedVector = new(); + for (int i = 0; i < 1000; ++i) + { + indexedVector.Add(new KeyValuePair { Key = Guid.NewGuid().ToString(), Value = i }); + } + Root root = new() { - IndexedVector = new IndexedVector - { - new KeyValuePair { Key = "a", Value = 0 }, - new KeyValuePair { Key = "b", Value = 1 }, - new KeyValuePair { Key = "c", Value = 2 }, - }, + IndexedVector = indexedVector, IntVector = new int[] { 1, 2, 3 }, StructVector = new List { @@ -27,9 +30,22 @@ static void Main(string[] args) Console.WriteLine("Max size: " + maxSize); byte[] buffer = new byte[maxSize]; - int bytesWritten = Root.Serializer.Write(buffer, root); + int bytesWritten = 0; + + for (int i = 0; i < 10; ++i) + { + bytesWritten = Root.Serializer.Write(buffer, root); + } + + Stopwatch sw = Stopwatch.StartNew(); + for (int i = 0; i < 1000; ++i) + { + bytesWritten = Root.Serializer.Write(buffer, root); + } + sw.Stop(); - Console.WriteLine("Serialization complete. Bytes written = " + bytesWritten); + Console.WriteLine($"Serialization complete. Bytes written = {bytesWritten}. TotalTime = {sw.Elapsed.TotalMicroseconds:N0}us"); + Console.WriteLine(); foreach (var option in Enum.GetValues()) { @@ -37,44 +53,152 @@ static void Main(string[] args) Traverse(root, new(buffer), option); Traverse(root, new(buffer), option); Traverse(root, new(buffer), option); + Traverse(root, new(new ArrayInputBuffer(buffer)), option); + Console.WriteLine(); } } public static void Traverse(Root original, TInputBuffer buffer, FlatBufferDeserializationOption option) where TInputBuffer : IInputBuffer { - Console.WriteLine($"Parsing [ {option} ][ {typeof(TInputBuffer).Name} ]"); + for (int i = 0; i < 10; ++i) + { + ParseAndTraverse(original, buffer, option); + } + + Stopwatch sw = Stopwatch.StartNew(); + for (int i = 0; i < 1000; ++i) + { + ParseAndTraverse(original, buffer, option); + } + sw.Stop(); - Root parsed = Root.Serializer.Parse(buffer, option); + Console.WriteLine($"Parsing [ {option} ][ {typeof(TInputBuffer).Name} ]. TotalTime = {sw.Elapsed.TotalMicroseconds:N0}us"); - for (int i = 0; i < original.IntVector.Count; ++i) + static void ParseAndTraverse(Root original, TInputBuffer buffer, FlatBufferDeserializationOption option) { - if (original.IntVector[i] != parsed.IntVector[i]) + Root parsed = Root.Serializer.Parse(buffer, option); + + for (int i = 0; i < original.IntVector.Count; ++i) { - throw new Exception(); + if (original.IntVector[i] != parsed.IntVector[i]) + { + throw new Exception(); + } } - } - foreach (var kvp in original.IndexedVector) - { - string key = kvp.Key; - int value = kvp.Value.Value; + foreach (var kvp in original.IndexedVector) + { + string key = kvp.Key; + int value = kvp.Value.Value; - if (!parsed.IndexedVector.TryGetValue(key, out var parsedValue) || parsedValue.Value != value) + if (!parsed.IndexedVector.TryGetValue(key, out var parsedValue) || parsedValue.Value != value) + { + throw new Exception(); + } + } + + for (int i = 0; i < original.StructVector.Count; ++i) { - throw new Exception(); + Vec3 originalItem = original.StructVector[i]; + Vec3 parsedItem = parsed.StructVector[i]; + + if (originalItem.X != parsedItem.X || originalItem.Y != parsedItem.Y || originalItem.Z != parsedItem.Z) + { + throw new Exception(); + } } } + } - for (int i = 0; i < original.StructVector.Count; ++i) + private class CustomInputBuffer : IInputBuffer + { + private readonly IInputBuffer inner; + + public CustomInputBuffer(IInputBuffer inner) { - Vec3 originalItem = original.StructVector[i]; - Vec3 parsedItem = parsed.StructVector[i]; + this.inner = inner; + } - if (originalItem.X != parsedItem.X || originalItem.Y != parsedItem.Y || originalItem.Z != parsedItem.Z) - { - throw new Exception(); - } + public bool IsReadOnly => inner.IsReadOnly; + + public bool IsPinned => inner.IsPinned; + + public int Length => inner.Length; + + public Memory GetMemory() + { + return inner.GetMemory(); + } + + public ReadOnlyMemory GetReadOnlyMemory() + { + return inner.GetReadOnlyMemory(); + } + + public ReadOnlySpan GetReadOnlySpan() + { + return inner.GetReadOnlySpan(); + } + + public Span GetSpan() + { + return inner.GetSpan(); + } + + public byte ReadByte(int offset) + { + return inner.ReadByte(offset); + } + + public double ReadDouble(int offset) + { + return inner.ReadDouble(offset); + } + + public float ReadFloat(int offset) + { + return inner.ReadFloat(offset); + } + + public int ReadInt(int offset) + { + return inner.ReadInt(offset); + } + + public long ReadLong(int offset) + { + return inner.ReadLong(offset); + } + + public sbyte ReadSByte(int offset) + { + return inner.ReadSByte(offset); + } + + public short ReadShort(int offset) + { + return inner.ReadShort(offset); + } + + public string ReadString(int offset, int byteLength, Encoding encoding) + { + return inner.ReadString(offset, byteLength, encoding); + } + + public uint ReadUInt(int offset) + { + return inner.ReadUInt(offset); + } + + public ulong ReadULong(int offset) + { + return inner.ReadULong(offset); + } + + public ushort ReadUShort(int offset) + { + return inner.ReadUShort(offset); } } }