public void MimallocAllocFreePerf() { var h = Mem.HeapNew(); Mem.OptionSetEnabled(Mem.Option.EagerCommit, true); Mem.OptionSetEnabled(Mem.Option.LargeOsPages, true); Mem.OptionSetEnabled(Mem.Option.ResetDecommits, true); Mem.OptionSetEnabled(Mem.Option.PageReset, true); Mem.OptionSetEnabled(Mem.Option.SegmentReset, true); Mem.OptionSetEnabled(Mem.Option.AbandonedPageReset, true); Mem.OptionSet(Mem.Option.ResetDelay, 0); Mem.OptionSetEnabled(Mem.Option.EagerRegionCommit, true); Mem.RegisterOutput((str, arg) => { Console.Write(str); }, null); Assert.IsTrue(Mem.OptionIsEnabled(Mem.Option.PageReset)); var count = 100_000L; var size = 32 * 4096; IntPtr[] ptrs = new IntPtr[count]; for (int r = 0; r < 4; r++) { Task.Factory.StartNew(() => { using (Benchmark.Run("Alloc" + r, (long)(count * size / 1000.0))) { for (int i = 0; i < count; i++) { ptrs[i] = (IntPtr)Mem.HeapMalloc(h, (UIntPtr)size); if ((long)(ptrs[i]) % 4096 != 0) { Assert.Fail(((long)(ptrs[i]) % 4096).ToString()); // Console.WriteLine((long)(ptrs[i]) % 4096); } for (int j = 0; j < size; j += 4096) { ((byte *)ptrs[i])[j] = 0; } ((byte *)ptrs[i])[size - 1] = 0; } } } , TaskCreationOptions.LongRunning).Wait(); Task.Factory.StartNew(() => { using (Benchmark.Run("Free" + r, (long)(count * size / 1000.0))) { for (long i = count - 1; i >= 0; i--) { Mem.Free((byte *)ptrs[i]); } // Mem.HeapCollect(h, true); } } , TaskCreationOptions.LongRunning).Wait(); } Mem.StatsPrint(); // long x = 0; // while (true) // { // x++; // // ptrs[0] = (IntPtr) Spreads.Native.Mem.Malloc((UIntPtr) size); // Mem.Free((byte*) ptrs[0]); // if (x == long.MaxValue) // break; // // Thread.Sleep(1); // } // // // Spreads.Native.Mem.Collect(false); // // // Thread.Sleep(10000000); }
public void MimallocAllocFreeCallLatency() { Mem.OptionSetEnabled(Mem.Option.EagerCommit, true); Mem.OptionSetEnabled(Mem.Option.LargeOsPages, true); Mem.OptionSetEnabled(Mem.Option.ResetDecommits, true); Mem.OptionSetEnabled(Mem.Option.PageReset, true); Mem.OptionSetEnabled(Mem.Option.SegmentReset, true); Mem.OptionSetEnabled(Mem.Option.AbandonedPageReset, true); // Mem.OptionSet(Mem.Option.ResetDelay, 0); Mem.OptionSetEnabled(Mem.Option.EagerRegionCommit, true); Mem.RegisterOutput((str, arg) => { Console.Write(str); }, null); var rng = new Random(); var allocated = 0L; var h = Mem.HeapNew(); var count = 100_000L; var size = 32 * 4096; IntPtr[] ptrs = new IntPtr[count]; for (int r = 0; r < 4; r++) { var histogram = new HdrHistogram.LongHistogram(1, 1000000, 1); using (Benchmark.Run("AllocFree" + r, count)) { for (int i = 0; i < count; i++) { var x = rng.NextDouble(); var start = Stopwatch.GetTimestamp(); if (allocated < 1000) { Allocate(); } else if (allocated > 2000) { Free(); } else if (((2000 - allocated) / 1000.0 * x) > 0.5) { Allocate(); } else { Free(); } var time = Stopwatch.GetTimestamp() - start; histogram.RecordValue(time); void Allocate() { ptrs[allocated] = (IntPtr)Mem.HeapMalloc(h, (UIntPtr)size); for (int j = 0; j < size; j += 4096) { ((byte *)ptrs[allocated])[j] = 0; } ((byte *)ptrs[allocated])[size - 1] = 0; allocated++; } void Free() { Mem.Free((byte *)ptrs[allocated - 1]); allocated--; } } } histogram.OutputPercentileDistribution(Console.Out, outputValueUnitScalingRatio: OutputScalingFactor.TimeStampToMicroseconds); } Mem.StatsPrint(); }
public void Lz4Benchmark() { const string name = "LZ4"; var count = ItemCount; var bufferLen = count * Unsafe.SizeOf <TestValue>(); var originalPtr = (byte *)Marshal.AllocHGlobal(bufferLen); var compressedPtr = (byte *)Marshal.AllocHGlobal(bufferLen * 2); var decompressedPtr = (byte *)Marshal.AllocHGlobal(bufferLen); for (int i = 0; i < count; i++) { ((TestValue *)originalPtr)[i] = new TestValue() { //Dbl = (double)i + 1 / (double)(i + 1), //Dbl1 = (double)i + 1 / (double)(i + 1), Num = i, Num1 = i, }; } for (int level = 1; level < 10; level++) { var compressedLen = Compression.compress_lz4(originalPtr, (IntPtr)bufferLen, compressedPtr, (IntPtr)(bufferLen * 2), level); var decompressedLen = Compression.decompress_lz4(compressedPtr, (IntPtr)compressedLen, decompressedPtr, (IntPtr)bufferLen); Console.WriteLine($"{name} Level: {level}, ratio: {1.0 * decompressedLen / compressedLen}"); Assert.AreEqual(bufferLen, decompressedLen); Assert.IsTrue(new Span <byte>(originalPtr, bufferLen).SequenceEqual(new Span <byte>(decompressedPtr, bufferLen))); } Console.WriteLine("-------------------------------"); var rounds = 3; var iterations = Iterations / 10; for (int r = 0; r < rounds; r++) { for (int level = 1; level < 10; level++) { int compressedLen = 0; using (Benchmark.Run($"{name} W{level}", bufferLen * iterations, true)) { for (int i = 0; i < iterations; i++) { compressedLen = Compression.compress_lz4(originalPtr, (IntPtr)bufferLen, compressedPtr, (IntPtr)bufferLen, level); } } using (Benchmark.Run($"{name} R{level}", bufferLen * iterations, true)) { for (int i = 0; i < iterations; i++) { Compression.decompress_lz4(compressedPtr, (IntPtr)compressedLen, decompressedPtr, (IntPtr)bufferLen); } } } } Benchmark.Dump(); }