public void CouldShuffleUnshuffle()
        {
            byte itemSize = 16;
            var  srcLen   = ItemCount * itemSize;

            var originalPtr     = (byte *)Marshal.AllocHGlobal(srcLen);
            var compressedPtr   = (byte *)Marshal.AllocHGlobal(srcLen);
            var decompressedPtr = (byte *)Marshal.AllocHGlobal(srcLen);

            for (int i = 0; i < ItemCount; i++)
            {
                for (int j = 0; j < itemSize; j++)
                {
                    originalPtr[i * itemSize + j] = (byte)(j + 1);
                }
            }

            var iterations = Iterations;

            var rounds = 3;

            for (int r = 0; r < rounds; r++)
            {
                using (Benchmark.Run("Shuffle", iterations * srcLen))
                {
                    for (int i = 0; i < iterations; i++)
                    {
                        Compression.shuffle((IntPtr)itemSize, (IntPtr)srcLen, originalPtr, compressedPtr);
                    }
                }
                using (Benchmark.Run("Unshuffle", iterations * srcLen))
                {
                    for (int i = 0; i < iterations; i++)
                    {
                        Compression.unshuffle((IntPtr)itemSize, (IntPtr)srcLen, compressedPtr, decompressedPtr);
                    }
                }
            }
            Benchmark.Dump();

            Assert.IsTrue(new Span <byte>(originalPtr, srcLen).SequenceEqual(new Span <byte>(decompressedPtr, srcLen)));
        }
예제 #2
0
        public unsafe void VirtualAllocFreePerf()
        {
            var count = 100_000L;
            var size  = 16 * 4096;

            IntPtr[] ptrs = new IntPtr[count];

            for (int r = 0; r < 3; r++)
            {
                using (Benchmark.Run("Alloc" + r, (long)(count * size / 1000.0)))
                {
                    for (int i = 0; i < count; i++)
                    {
                        ptrs[i] = Kernel32.VirtualAlloc(IntPtr.Zero, (uint)size,
                                                        Kernel32.Consts.MEM_COMMIT | Kernel32.Consts.MEM_RESERVE, Kernel32.Consts.PAGE_READWRITE);

                        if ((long)(ptrs[i]) % 4096 != 0)
                        {
                            Assert.Fail(((long)(ptrs[i]) % 4096).ToString());
                        }

                        for (int j = 0; j < size; j += 4096)
                        {
                            ((byte *)ptrs[i])[j] = 0;
                        }
                        ((byte *)ptrs[i])[size - 1] = 0;
                    }
                }

                using (Benchmark.Run("Free" + r, (long)(count * size / 1000.0)))
                {
                    for (int i = 0; i < count; i++)
                    {
                        Kernel32.VirtualFree(ptrs[i], 0, Kernel32.Consts.MEM_RELEASE);
                    }
                }
            }
        }
예제 #3
0
        public void MimallocAllocFreeCallPerf()
        {
            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);

            var h = Mem.HeapNew();

            var count = 100_000L;
            var size  = 32 * 4096;

            IntPtr[] ptrs = new IntPtr[count];

            for (int r = 0; r < 4; r++)
            {
                using (Benchmark.Run("AllocFree" + r, count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        ptrs[i] = (IntPtr)Mem.HeapMalloc(h, (UIntPtr)size);
                        for (int j = 0; j < size; j += 4096)
                        {
                            ((byte *)ptrs[i])[j] = 0;
                        }
                        ((byte *)ptrs[i])[size - 1] = 0;
                        Mem.Free((byte *)ptrs[i]);
                    }
                }
            }

            Mem.StatsPrint();
        }
예제 #4
0
        public void CpuNumberPerformance()
        {
            var count = 10_000_000;

            for (int r = 0; r < 10; r++)
            {
                using (Benchmark.Run(".NET Core", count, true))
                {
                    GetCupuNumberDotnet(count);
                }

                using (Benchmark.Run("Spreads.Native", count, true))
                {
                    GetCupuNumberSpreads(count);
                }

                using (Benchmark.Run("Spreads.Pal", count, true))
                {
                    GetCupuNumberSpreadsPal(count);
                }
            }

            Benchmark.Dump();
        }
예제 #5
0
        public unsafe void MarshalAllocFreePerf()
        {
            var count = 100_000L;
            var size  = 32 * 4096;

            IntPtr[] ptrs = new IntPtr[count];

            for (int r = 0; r < 4; r++)
            {
                using (Benchmark.Run("Alloc" + r, (long)(count * size / 1000.0)))
                {
                    for (int i = 0; i < count; i++)
                    {
                        ptrs[i] = Marshal.AllocHGlobal(size);

                        // It's unaligned
                        // if ((long)(ptrs[i]) % 4096 != 0)
                        //     Assert.Fail(((long) (ptrs[i]) % 4096).ToString());

                        for (int j = 0; j < size; j += 4096)
                        {
                            ((byte *)ptrs[i])[j] = 0;
                        }
                        ((byte *)ptrs[i])[size - 1] = 0;
                    }
                }

                using (Benchmark.Run("Free" + r, (long)(count * size / 1000.0)))
                {
                    for (int i = 0; i < count; i++)
                    {
                        Marshal.FreeHGlobal(ptrs[i]);
                    }
                }
            }
        }
        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();
        }
예제 #7
0
        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();
        }
예제 #8
0
        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);
        }