Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using BenchmarkDotNet.Running;
- using BenchmarkDotNet.Attributes;
- using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
- namespace ConsoleApp2
- {
- public class Program
- {
- private static MemoryPool _pool;
- private static MemoryPoolIterator _iter;
- private static ulong _expectedResult;
- private static long _oldExpectedResult;
- static Program()
- {
- var blockBytes = 0;
- _pool = new MemoryPool();
- // Arrange
- _expectedResult = 0x0102030405060708UL;
- _oldExpectedResult = (long)_expectedResult;
- //var nonZeroData = 0xFF00FFFF0000FFFFUL;
- var nextBlockBytes = 8 - blockBytes;
- var block = _pool.Lease();
- block.Start += 8;
- block.End = block.Start + blockBytes;
- var nextBlock = _pool.Lease();
- nextBlock.Start += 8;
- nextBlock.End = nextBlock.Start + nextBlockBytes;
- block.Next = nextBlock;
- var bytes = BitConverter.GetBytes(_expectedResult);
- Buffer.BlockCopy(bytes, 0, block.Array, block.Start, blockBytes);
- Buffer.BlockCopy(bytes, blockBytes, nextBlock.Array, nextBlock.Start, nextBlockBytes);
- // Fill in surrounding bytes with non-zero data
- //var nonZeroBytes = BitConverter.GetBytes(nonZeroData);
- //Buffer.BlockCopy(nonZeroBytes, 0, block.Array, block.Start - 8, 8);
- //Buffer.BlockCopy(nonZeroBytes, 0, block.Array, block.End, 8);
- //Buffer.BlockCopy(nonZeroBytes, 0, nextBlock.Array, nextBlock.Start - 8, 8);
- //Buffer.BlockCopy(nonZeroBytes, 0, nextBlock.Array, nextBlock.End, 8);
- _iter = block.GetIterator();
- }
- public static void Main(string[] args)
- {
- new Program().TestPeekLong();
- new Program().TestOldPeekLong();
- var summary = BenchmarkRunner.Run<Program>();
- Console.WriteLine(summary);
- }
- [Benchmark]
- public void TestPeekLong()
- {
- ulong? result = _iter.PeekLong();
- if (result != _expectedResult)
- {
- throw new Exception();
- }
- }
- [Benchmark]
- public void TestOldPeekLong()
- {
- long result = _iter.OldPeekLong();
- if (result != _oldExpectedResult)
- {
- throw new Exception();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement