Browse Source

Add benchmark and fix a chunkyimage bug

Equbuxu 3 years ago
parent
commit
b672502746

+ 1 - 1
src/ChunkyImageLib/ChunkyImage.cs

@@ -330,7 +330,7 @@ namespace ChunkyImageLib
 
         private void MaybeCreateAndProcessQueueForChunk(Vector2i chunkPos, ChunkResolution resolution)
         {
-            if (latestChunksData[resolution].TryGetValue(chunkPos, out LatestChunkData chunkData))
+            if (!latestChunksData[resolution].TryGetValue(chunkPos, out LatestChunkData chunkData))
                 chunkData = new() { QueueProgress = 0, IsDeleted = !committedChunks[ChunkResolution.Full].ContainsKey(chunkPos) };
             if (chunkData.QueueProgress == queuedOperations.Count)
                 return;

+ 14 - 0
src/ChunkyImageLibBenchmark/ChunkyImageLibBenchmark.csproj

@@ -0,0 +1,14 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\ChunkyImageLib\ChunkyImageLib.csproj" />
+  </ItemGroup>
+
+</Project>

+ 52 - 0
src/ChunkyImageLibBenchmark/Program.cs

@@ -0,0 +1,52 @@
+using ChunkyImageLib;
+using SkiaSharp;
+using System.Diagnostics;
+
+
+for (int i = 0; i < 10; i++)
+{
+    Benchmark();
+}
+
+int count = 10000;
+double totalFirst = 0;
+double totalSecond = 0;
+for (int i = 0; i < count; i++)
+{
+    (double first, double second) = Benchmark();
+    totalFirst += first;
+    totalSecond += second;
+}
+
+Console.WriteLine($"took {totalFirst / count} ms first, then {totalSecond / count} ms");
+Console.ReadKey();
+
+(double first, double second) Benchmark()
+{
+    using ChunkyImage image = new(new(1024, 1024));
+    image.DrawRectangle(new(new(0, 0), new(1024, 1024), 10, SKColors.Black, SKColors.Bisque));
+
+    Stopwatch sw = Stopwatch.StartNew();
+    for (int i = 0; i < 4; i++)
+    {
+        for (int j = 0; j < 4; j++)
+        {
+            image.GetLatestChunk(new(i, j), ChunkyImageLib.DataHolders.ChunkResolution.Full);
+        }
+    }
+    sw.Stop();
+    double first = sw.ElapsedTicks / (double)Stopwatch.Frequency * 1000;
+
+    sw = Stopwatch.StartNew();
+    for (int i = 0; i < 4; i++)
+    {
+        for (int j = 0; j < 4; j++)
+        {
+            image.GetLatestChunk(new(i, j), ChunkyImageLib.DataHolders.ChunkResolution.Full);
+        }
+    }
+    sw.Stop();
+    double second = sw.ElapsedTicks / (double)Stopwatch.Frequency * 1000;
+
+    return (first, second);
+}

+ 7 - 1
src/PixiEditorPrototype.sln

@@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChunkyImageLibTest", "Chunk
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChunkyImageLibVis", "ChunkyImageLibVis\ChunkyImageLibVis.csproj", "{3B0A0186-8AC0-4B1D-8587-4CE4E0E12567}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PixiEditor.Zoombox", "PixiEditor.Zoombox\PixiEditor.Zoombox.csproj", "{232E58B6-8080-4725-8541-98BFCFE23A1C}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditor.Zoombox", "PixiEditor.Zoombox\PixiEditor.Zoombox.csproj", "{232E58B6-8080-4725-8541-98BFCFE23A1C}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChunkyImageLibBenchmark", "ChunkyImageLibBenchmark\ChunkyImageLibBenchmark.csproj", "{3998B3EB-F11F-4637-A135-FBC3CCA24299}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -45,6 +47,10 @@ Global
 		{232E58B6-8080-4725-8541-98BFCFE23A1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{232E58B6-8080-4725-8541-98BFCFE23A1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{232E58B6-8080-4725-8541-98BFCFE23A1C}.Release|Any CPU.Build.0 = Release|Any CPU
+		{3998B3EB-F11F-4637-A135-FBC3CCA24299}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{3998B3EB-F11F-4637-A135-FBC3CCA24299}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{3998B3EB-F11F-4637-A135-FBC3CCA24299}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{3998B3EB-F11F-4637-A135-FBC3CCA24299}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE