Skip to content

Instantly share code, notes, and snippets.

@halter73
Created October 28, 2016 00:20
Show Gist options
  • Save halter73/3cd704aad57ece45ee516c0ac897aea5 to your computer and use it in GitHub Desktop.
Save halter73/3cd704aad57ece45ee516c0ac897aea5 to your computer and use it in GitHub Desktop.
PeekLong Benchmark
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment