Use CodeBenchmark concurrent test logic code

We have been interested in comparing the performance of the test, so also wrote a lot of testing tools WebApiBenchmarkand TcpBenchmarkso on; but these are all ways to test tools and configurations targeted to limited functionality so difficult to apply more scenes, so developers alone a component CodeBenchmarkto address business performance tests under different codes; strictly speaking CodeBenchmarkis not a complete test tool that provides a test management and functional testing environment, you can test for a concurrent realization of a rule business code and provide a final test results. the following describes how to use CodeBenchmark.

Environmental requirements

CodeBenchmarkIt is based on the netstandard2.0development for support of this release .net coreand .net frameworkthe environment; run the system need to be deployed to look at the situation linuxor windowsthe development of language c#.

Build a test project

By vsor vscodeconstruct a console project and then reference the component (references, the latest version BeetleX.CodeBenchmark)

Install-Package BeetleX.CodeBenchmark -Version 0.6.2 

After the reference component can be prepared by specific test, must meet the test assembly preparation of the test requirements, it is necessary to implement an interface to write test code, the interface is described as follows:

    public interface IExample:IDisposable
    {
        void Initialize(Benchmark benchmark);
        Task Execute();
    }

Initialize

A method for performing initialization information described concurrent instances created

Execute

Code logic concurrent instances of each execution

Test code can be written for your business, the specific business logic can be http, database access and other operations.

websocket test

[System.ComponentModel.Category("TCP")]
public class WebsocketJson : IExample
{
    public async Task Execute()
    {
        var request = new { url = "/json" };
        var result = await jsonClient.ReceiveFrom(request);
    }

    private BeetleX.Http.WebSockets.JsonClient jsonClient;

    public void Initialize(Benchmark benchmark)
    {
        jsonClient = new BeetleX.Http.WebSockets.JsonClient("ws: //192.168.2.19: 8080 " ); 
    } 

    public  void Dispose () 
    { 
        jsonClient.Dispose (); 
    } 
}

TCP Test Case

[System.ComponentModel.Category("TCP")]
public class TcpTextLine : IExample
{
    public async Task Execute()
    {
        var data = $"henryfan@{DateTime.Now}";
        var stream = await mClient.ReceiveFrom(s => s.WriteLine(data));
        stream.ReadLine();

    }

    private BeetleX.Clients.AsyncTcpClient mClient;

    public void Initialize(Benchmark benchmark)
    {
        mClient = BeetleX.SocketFactory.CreateClient<BeetleX.Clients.AsyncTcpClient>("192.168.2.19", 9012);
    }

    public void Dispose()
    {
        mClient.Dispose();
    }
}

Http test

[System.ComponentModel.Category("TCP")]
class HttpGet : IExample
{
    public void Dispose()
    {

    }

    public async Task Execute()
    {
        var result = await _httpHandler.json();
    }

    public void Initialize(Benchmark benchmark)
    {
        if (_httpApi == null)
        {
            _httpApi = new BeetleX.Http.Clients.HttpClusterApi();
            _httpApi.DefaultNode.Add("http://192.168.2.19:8080");
            _httpHandler = _httpApi.Create<IHttpHandler>();
        }
    }

    static BeetleX.Http.Clients.HttpClusterApi _httpApi;

    static IHttpHandler _httpHandler;

    [BeetleX.Http.Clients.FormUrlFormater]
    public interface IHttpHandler
    {
        // http://host/json
        Task<string> json();
    }
}

Basis of the above test cases are available from https://github.com/IKende/CodeBenchmarkDoc  been acquired.

Run the test case

Component does not need to be configured at run time by the embodiment, only the need to build and test subject with a test assemblies into registration to complete.

     Benchmark benchmark = new Benchmark();
     benchmark.Register(typeof(Program).Assembly);
     benchmark.Start();

The above code is loaded this item assembly, test and launch a service management interface port is enabled by default. 9090, You can Startspecify the port service to start logging the following method:

Can be viewed by logging into the service starts, the run after success can be accessed via a browser and test

Can be selected according to the required test cases test cases, the number of concurrent and run time.

You can run multiple test cases, and to compare their performance. 

 

If you are interested in components or comments can focus  https://github.com/IKende/CodeBenchmarkDoc

Guess you like

Origin www.cnblogs.com/smark/p/11423215.html