Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EgorBot for antonfirsov in #112417 #293

Open
EgorBot opened this issue Feb 11, 2025 · 1 comment
Open

EgorBot for antonfirsov in #112417 #293

EgorBot opened this issue Feb 11, 2025 · 1 comment

Comments

@EgorBot
Copy link
Owner

EgorBot commented Feb 11, 2025

Processing dotnet/runtime#112417 (comment) command:

Command

-intel

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Net;
using System.Net.Sockets;

BenchmarkSwitcher.FromAssembly(typeof(SocketBench).Assembly).Run(args);

[MemoryDiagnoser]
public class SocketBench
{
    private Socket? _listener;
    private Socket? _receiver;
    private Socket? _sender;
    private CancellationTokenSource? _cts;
    private byte[] _sendBuffer = new byte[16];

    [Benchmark]
    public int Send() => _sender!.Send(_sendBuffer);

    [Benchmark]
    public async Task<int> SendAsync() => await _sender!.SendAsync(_sendBuffer, SocketFlags.None, default);

    [Benchmark]
    public void CreateClose()
    {
        Socket sock = new Socket(SocketType.Stream, ProtocolType.Tcp);
        sock.Dispose();
    }

    [GlobalSetup(Targets = [nameof(Send), nameof(SendAsync)])]
    public async Task Setup()
    {
        _listener = new Socket(SocketType.Stream, ProtocolType.Tcp);
        _listener.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
        IPEndPoint serverEp = (IPEndPoint)_listener.LocalEndPoint!;
        _listener.Listen();

        Task<Socket> acceptTask = _listener.AcceptAsync();

        _sender = new Socket(SocketType.Stream, ProtocolType.Tcp);
        await _sender.ConnectAsync(serverEp);

        _receiver = await acceptTask;
        _cts = new CancellationTokenSource();

        _ = RunServer();

        async Task RunServer()
        {
            byte[] rcvBuffer = new byte[16];
            try
            {
                while (!_cts.Token.IsCancellationRequested)
                {
                    await _receiver.ReceiveAsync(rcvBuffer, _cts.Token);
                }
            }
            catch (OperationCanceledException) { }
        }
    }

    [GlobalCleanup(Targets = [nameof(Send), nameof(SendAsync)])]
    public void Cleanup()
    {
        _cts?.Cancel();
        _receiver?.Dispose();
        _listener?.Dispose();
        _sender?.Dispose();
    }
}

(EgorBot will reply in this issue)

@EgorBot
Copy link
Owner Author

EgorBot commented Feb 11, 2025

Benchmark results on linux_azure_cascadelake

BenchmarkDotNet v0.14.0, Ubuntu 24.04.1 LTS (Noble Numbat)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 8 logical and 4 physical cores
  Job-BZNWLL : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  Job-PIBQFT : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
Method Toolchain Mean Error Ratio Gen0 Allocated Alloc Ratio
Send Main 426.8 ns 4.52 ns 1.00 - - NA
Send PR 433.5 ns 4.94 ns 1.02 - - NA
SendAsync Main 524.5 ns 3.78 ns 1.00 0.0029 72 B 1.00
SendAsync PR 523.5 ns 3.62 ns 1.00 0.0029 72 B 1.00
CreateClose Main 3,165.8 ns 34.96 ns 1.00 0.0038 176 B 1.00
CreateClose PR 3,202.9 ns 35.18 ns 1.01 0.0038 168 B 0.95

BDN_Artifacts.zip

cc @antonfirsov (agent_logs.txt). EgorBot manual: link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant