benchmarkdotnet docker 运行

使用docker 运行基准测试是一个不错的选择,可以减少我们环境搭建的时间,同时也可以加速ci/cd

环境准备

  • docker-compose 文件
version: "3"
services: 
  app-benchmark:
    build: 
      context: ./
      dockerfile: Dockerfile-benchmark
    image: dalongrong/dotnetcorebenchmark-demo:alpine
  • dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
COPY benchmark/*.csproj ./
RUN dotnet restore
COPY benchmark/. /app/
ENTRYPOINT [ "dotnet","run","-c","Release" ]

代码准备

注意target版本dotnetcore2.0

  • 创建demo项目
mkdir benchmark
cd benchmark
dotnet new console
  • 添加依赖库
dotnet add package BenchmarkDotNet
  • 简单demo
using System;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
    public class Md5VsSha256
    {
        private const int N = 10000;
        private readonly byte[] data;
        private readonly SHA256 sha256 = SHA256.Create();
        private readonly MD5 md5 = MD5.Create();
        public Md5VsSha256()
        {
            data = new byte[N];
            new Random(42).NextBytes(data);
        }
        [Benchmark]
        public byte[] Sha256() => sha256.ComputeHash(data);
        [Benchmark]
        public byte[] Md5() => md5.ComputeHash(data);
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<Md5VsSha256>();
        }
    }
}
   
  • demo 的benchmark.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
  </ItemGroup>
</Project>
 

构建&&运行

  • 构建
docker-compose up 
  • 效果

benchmarkdotnet docker 运行

 

参考资料

https://github.com/rongfengliang/aspnetcore-webapi-docker-compose-demo
https://benchmarkdotnet.org/articles/guides/how-to-run.html
https://github.com/dotnet/BenchmarkDotNet

上一篇:Maskrcnn_Benchmark Ubuntu18.04 环境配置踩坑流程


下一篇:Benchmark PostgreSQL With Linux HugePages