首页 > 其他分享 >NetBenchmarkDotNet性能测试

NetBenchmarkDotNet性能测试

时间:2023-09-14 11:22:57浏览次数:34  
标签:StudyClassId StudentId stringslist 性能 NetBenchmarkDotNet Score 测试 Scorerate new

案例

using BenchmarkDotNet.Attributes;
using BenchmarkDotNetDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace BenchmarkDotNetDemo
{
    [MemoryDiagnoser, RankColumn]
    public class Test
    {


        IList<HTEAndStudent> stringslist = new List<HTEAndStudent>();
     /*   List<HTEAndStudent> stringslist2 = new List<HTEAndStudent>();

        List<HTEAndStudentDto> hTEAndStudentDtosOut = new();*/
        public Test()
        {
            Random random = new Random();
            for (int i = 0; i < 10000000; i++)
            {
                stringslist.Add(new HTEAndStudent
                {
                    IsSubmit = true,
                    StudentId = i,
                    Score = i + random.Next(1, 99),
                    Scorerate = 666,
                    StudyClassId = i + random.Next(1, 11),
                });

              /*  stringslist2.Add(new HTEAndStudent
                {
                    IsSubmit = true,
                    StudentId = i.ToString(),
                    Score = i + random.Next(1, 99),
                    Scorerate = 666,
                    StudyClassId = i + random.Next(1, 11),
                });*/
            }

           /* for (int i = 0; i < 1000; i++)
            {
                hTEAndStudentDtosOut.Add(new()
                {
                    IsSubmit = true,
                    StudentId = i,
                    Score = i + random.Next(1, 99),
                    Scorerate = 666,
                    StudyClassId = i + random.Next(1, 81),
                });
            }*/
        }

        /* [Benchmark]
         public void KK()
         {
             var aaaa = stringslist2.Where(r => r.StudentId < 1800).ToList();
             foreach (var item in aaaa)
             {
                 var bbbb = hTEAndStudentDtosOut.Where(r => r.StudyClassId == item.StudyClassId && r.Score > 50).ToList();
                 var aadd = bbbb.Count;
             }
         }
         [Benchmark]
         public void KK1()
         {
             var aaaa = stringslist2.Where(r => r.StudentId < 1800).ToList();
             var bbbbdic = hTEAndStudentDtosOut.GroupBy(r=>r.StudyClassId).ToDictionary(r => r.Key,v=>v.Where(r=>r.Score > 50).ToList());
             foreach (var item in aaaa)
             {
                 if(bbbbdic.ContainsKey(item.StudyClassId))
                 {
                     var bbbb = bbbbdic[item.StudyClassId];
                     var aadd = bbbb.Count;
                 }
             }
         }*/








/*

        [Benchmark]
        public void Count_Method()
        {
            if (stringslist.Count() == 0)
            {
                Console.WriteLine("5555");
            }
        }



        [Benchmark]
        public void Count_Property()
        {
            if (stringslist.Count == 0)
            {
                Console.WriteLine("5555");
            }
        }

        [Benchmark]
        public void Any()
        {
            if (!stringslist.Any())
            {
                Console.WriteLine("5555");
            }
        }



        [Benchmark]
        public void Count_Method_Where()
        {
            if (stringslist.Count(r => r.StudentId == 5) == 0)
            {
                Console.WriteLine("5555");
            }
        }


        [Benchmark]
        public void Any_Method_Where()
        {
            if (!stringslist.Any(r => r.StudentId == 5))
            {
                Console.WriteLine("5555");
            }
        }



        [Benchmark]
        public void IListForeEachList()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            stringslist.ToList().ForEach(r =>
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = r.StudyClassId,
                    Scorerate = r.Scorerate,
                    IsSubmit = r.IsSubmit,
                    Score = r.Score,
                    StudentId = r.StudentId,
                });
            });

        }*/

        [Benchmark]
        public void ForeEachList()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            stringslist.ToList().ForEach(r =>
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = r.StudyClassId,
                    Scorerate = r.Scorerate,
                    IsSubmit = r.IsSubmit,
                    Score = r.Score,
                    StudentId = r.StudentId,
                });
            });

        }

        [Benchmark]
        public void Forech()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            foreach (var r in stringslist)
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = r.StudyClassId,
                    Scorerate = r.Scorerate,
                    IsSubmit = r.IsSubmit,
                    Score = r.Score,
                    StudentId = r.StudentId,
                });
            }
            
        }

        [Benchmark]
        public void For()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            for (int i = 0; i < stringslist.Count; i++)
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = stringslist[i].StudyClassId,
                    Scorerate = stringslist[i].Scorerate,
                    IsSubmit = stringslist[i].IsSubmit,
                    Score = stringslist[i].Score,
                    StudentId = stringslist[i].StudentId,
                });
            }
        }









       /* [Benchmark]
        public void ForechIntoDto()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            foreach (var r in stringslist)
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = r.StudyClassId,
                    Scorerate = r.Scorerate,
                    IsSubmit = r.IsSubmit,
                    Score = r.Score,
                    StudentId = r.StudentId,
                });
            }
        }

        [Benchmark]
        public void LinqSelectIntoDto()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = stringslist.Select(r => new HTEAndStudentDto()
            {
                StudyClassId = r.StudyClassId,
                Scorerate = r.Scorerate,
                IsSubmit = r.IsSubmit,
                Score = r.Score,
                StudentId = r.StudentId,
            }
            ).ToList();
        }


        [Benchmark]
        public void foreachHasWhereOuter()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            var data = stringslist.Where(r => r.Score > 80);
            foreach (var r in data)
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = r.StudyClassId,
                    Scorerate = r.Scorerate,
                    IsSubmit = r.IsSubmit,
                    Score = r.Score,
                    StudentId = r.StudentId,
                });
            }
        }

        [Benchmark]
        public void foreachHasWhereInner()
        {
            List<HTEAndStudentDto> hTEAndStudentDtos = new();
            foreach (var r in stringslist.Where(r => r.Score > 80))
            {
                hTEAndStudentDtos.Add(new()
                {
                    StudyClassId = r.StudyClassId,
                    Scorerate = r.Scorerate,
                    IsSubmit = r.IsSubmit,
                    Score = r.Score,
                    StudentId = r.StudentId,
                });
            }
        }*/
    }
}

Program

using BenchmarkDotNet.Running;

namespace BenchmarkDotNetDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {

            var summary = BenchmarkRunner.Run<Test>();
            Console.WriteLine("Hello, World!");
        }
    }
}

标签:StudyClassId,StudentId,stringslist,性能,NetBenchmarkDotNet,Score,测试,Scorerate,new
From: https://www.cnblogs.com/kkbk/p/17702057.html

相关文章

  • RunnerGo:提供更好的性能测试解决方案
    你是否曾经为了寻找一个可靠的性能测试工具而苦恼?传统的性能测试工具往往价格高昂,而且复杂难用,让企业难以承受。现在,我们为您推荐一款全新的性能测试工具——RunnerGo,它将为您带来前所未有的测试体验。RunnerGo是一款由国内开发者自主研发的企业级性能测试工具,它采用了轻量级、高性......
  • 软件项目结题测试报告怎么做?有哪些要求?
    ​ (公众号:软件测评闲聊站)验收测试报告当软件项目立项后,执行期到需要做项目结题,软件验收测试报告是证明项目成果是否达到预期技术考核指标的强有力证明材料。所以在项目执行期满前都需要找具备第三方软件测评资质如CMA或CNAS资质的机构出具第三方软件验收测试报告。验收测试......
  • 升讯威在线客服系统的并发高性能数据处理技术:PLINQ并行查询技术
    我在业余时间开发维护了一款免费开源的升讯威在线客服系统,也收获了许多用户。对我来说,只要能获得用户的认可,就是我最大的动力。最近客服系统成功经受住了客户现场组织的压力测试,获得了客户的认可。客户组织多名客服上线后,所有员工同一时间打开访客页面疯狂不停的给在线客服发消......
  • testing-code-测试代码unittest如何编写?
    1---name_function.py函数name_function.py用来获取人名,入参时选择姓、名、中间字三项(middle为可选入参项)1#!usr/bin/env/python23defget_formatted_name(first,last,middle=""):4ifmiddle:5full_name=f"{first}{middle}{last}"6returnf......
  • Apache Iceberg 表有哪些性能优化方式
    ApacheIceberg是一种开源的分布式数据表格格式,旨在提供可扩展性、性能和数据一致性。它建立在ApacheHadoop的基础上,并支持多种数据湖存储(如HadoopHDFS、AmazonS3等)。为了优化ApacheIceberg表的性能,可以采取多种策略和技术,以下是一些重要的性能优化方式和详细示例:Partition......
  • Node.js vs. Spring Boot:Hello World 性能对决,谁更快一点?
    前言:SpringBoot在Java生态中备受欢迎,它是一款基于Java构建的轻量级服务端框架,主要用于Web服务。SpringBoot的应用使得创建各类基于Spring的企业级应用变得异常简单。Node.js作为一种基于ChromeV8引擎的JavaScript运行时环境,在服务端上运行JavaScript代码。它以其独......
  • 14 性能对比分析
    packageannotate;importjava.lang.reflect.Constructor;importjava.lang.reflect.Field;importjava.lang.reflect.Method;//性能检测publicclassTest12{//1.普通调用publicstaticvoidtest01(){longstartTime=System.currentTimeMillis(......
  • 通信系统的性能指标
    主要性能指标:有效性(速度)、可靠性(质量)有效性模拟通信系统中:带宽数字通信系统中:码元传输速率(码元速率、传码率)单位时间(每秒)内系统传输的码元符号的数目,单位为波特(Baud),用\(......
  • MySQL+MHA搭建&&性能优化
    MHA基础概念MHAMHA(MasterHighAvailability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。MHA的出现就是解决MySQL单点的问题。MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上......
  • 云主机测试Flink磁盘满问题解决
    问题描述:使用云主机测试Flink时,根目录满了。经排查发现运行Flink任务后根目录空间一直在减少,最后定位持续增加的目录是/tmp目录解决方法:修改Flink配置使用一个相对较大的磁盘目录做为Flink运行时目录#Overridethedirectoriesfortemporaryfiles.Ifnotspecified,the#sy......