Files
DotNetty/test/DotNetty.Microbench/Allocators/AbstractByteBufferAllocatorBenchmark.cs
Andrey Ilnitsky 1cdaadbfaa - target net472 and netstandard2.0
- microsoft.extensions.logging 5.0.0
- microsoft.extensions.configuration 5.0.0
2021-07-16 18:04:31 -07:00

62 lines
1.8 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Microbench.Allocators
{
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
// using BenchmarkDotNet.Attributes.Jobs;
using DotNetty.Buffers;
using DotNetty.Common;
// [CoreJob]
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
[BenchmarkCategory("ByteBufferAllocator")]
[MemoryDiagnoser]
public abstract class AbstractByteBufferAllocatorBenchmark
{
const int MaxLiveBuffers = 8192;
public const int IterationCount = 3;
readonly IByteBufferAllocator allocator;
readonly Random rand = new Random();
readonly IByteBuffer[] buffers = new IByteBuffer[MaxLiveBuffers];
static AbstractByteBufferAllocatorBenchmark()
{
ResourceLeakDetector.Level = ResourceLeakDetector.DetectionLevel.Disabled;
}
protected AbstractByteBufferAllocatorBenchmark(IByteBufferAllocator allocator)
{
this.allocator = allocator;
}
[Params(0, 256, 1024, 4096, 16384, 65536)]
public int Size { get; set; }
[Benchmark]
public void Allocate() => this.AllocateAndFree(this.Size);
void AllocateAndFree(int size)
{
int idx = this.rand.Next(this.buffers.Length);
IByteBuffer oldBuf = this.buffers[idx];
oldBuf?.Release();
this.buffers[idx] = this.allocator.Buffer(size);
}
[GlobalCleanup]
public void GlobalCleanup()
{
for (int i = 0; i < this.buffers.Length; i++)
{
this.buffers[i]?.Release();
this.buffers[i] = null;
}
}
}
}