本文主要介绍.NET Core中,使用BeetleX.ConcurrentTest来进行Web API压力测试的方法,以及相关的示例代码。

1、安装引用BeetleX.ConcurrentTest

相关文档及源码https://github.com/IKende/ConcurrentTest

注意:本文中示例代码是在.NET Core 3.1中测试。

由于使用Install-Package BeetleX.ConcurrentTest -Version 0.2.8通过Nuget安装引用没找到,本文中是直接下载源代码,添加引用BeetleX.ConcurrentTest这个项目。另外还需要通过Nuget安装的引用如下:

1)使用Nuget界面管理器

搜索"BeetleX.FastHttpApi",在列表中找到它,点击"安装"

相关文档VS(Visual Studio)中Nuget的使用

2)使用Package Manager命令安装

PM> Install-Package BeetleX.FastHttpApi

3)使用.NET CLI命令安装

> dotnet add TodoApi.csproj package BeetleX.FastHttpApi

2、自定义的FastHttpClientTest测试类代码

using BeetleX.ConcurrentTest;
using BeetleX.FastHttpApi;
using BeetleX.FastHttpApi.Clients;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
    public class obj
    {
        public int id { get; set; }
        public int lid { get; set; }
        public int awardId { get; set; }
        public int balance { get; set; }
    }
    public class FastHttpClientTest
    {
        public FastHttpClientTest()
        {
            httpApiClient = new HttpApiClient(Host);
            clientApi = httpApiClient.Create<IHttpClientApi>();
        }
        public static ConcurrentDictionary<int, List<obj>> dics = new ConcurrentDictionary<int, List<obj>>();
        private string Host = "http://localhost:5000";
        private HttpApiClient httpApiClient;
        private IHttpClientApi clientApi;
        [CTestCase]
        public void GetWinAward()
        {
            var result = clientApi.GetWinAward(1, "1");
            obj o = null;
            if (result.Data!=null)
             o = Newtonsoft.Json.JsonConvert.DeserializeObject<obj>(result.Data?.ToString());
            if (o!=null&&o.awardId > 0)
            {
                lock (dics)
                {
                    if (dics.ContainsKey(o.awardId))
                        dics[o.awardId].Add(o);
                    else
                    {
                        dics[o.awardId] = new List<obj>();
                        dics[o.awardId].Add(o);
                    }
                }
                Console.WriteLine(result.Data.ToString());
            }

        }
        // [CTestCase]
        // public void ListEmployees () {
        //     clientApi.ListEmployees (2);
        // }
        [JsonFormater]
        public interface IHttpClientApi
        {
            [BeetleX.FastHttpApi.Get(Route = "api/service/GetWinAward")]
            JsonResult GetWinAward(int lid, string openid, string type = "release", string code = "", int latitude = -1, int longitude = -1);
            // [Post (Route = "api/employee")]
            // Employee AddEmployee (Employee item);
        }
    }
}

3、执行测试的代码

using BeetleX.ConcurrentTest;
using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //配置线程和请求次数
            CTester.RunTest<FastHttpClientTest>(3, 2000);
            foreach (var item in FastHttpClientTest.dics.Keys)
            {
                Console.WriteLine("awardId = " + item + " count = " + FastHttpClientTest.dics[item].Count);
            }
            Console.WriteLine("Hello World!");
        }
    }
}

测试项目源码https://www.cjavapy.com/download/5ebd3facdc72d90263e632d1/