restsharp / restsharp/RestSharp

RestClient.Execute() is extremely slow compared to RestClient.ExecuteAsync().Result

Open
#2,375 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
9.8k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
RestClient.Execute() is extremely slow compared to RestClient.ExecuteAsync().Result, when called in a parallel setting, even though both are synchronous.

To Reproduce

private class CustomInterceptor : Interceptor
{
    public override ValueTask BeforeDeserialization ( RestResponse response, CancellationToken cancellationToken )
    {
        response.ContentType = ContentTypes.application_json;
        return ValueTask.CompletedTask;
    }
}

static void Test02 ()
{
    //
    const int total = 100;
    int width = ( int ) Math.Ceiling( Math.Log10( total ) );
    ( int, TimeSpan )[] elapsed = new ( int, TimeSpan )[total];
    //

    RestResponse    Execute    ( RestClient c, string resource, Method method, Action<RestRequest> callback )
    {
        //
        RestRequest     i;
        RestResponse    o;
        //

        if ( c == null )
            throw new ArgumentNullException( nameof( c ) );

        i = new RestRequest( resource, method );
        callback?.Invoke( i );
        i.Interceptors = [];
        i.Interceptors.Add( new CustomInterceptor() );
        o = c.Execute( i );
        return o;
    }

    void Test ( int i )
    {
        //
        Stopwatch watch;
        //

        static void Add ( RestRequest t )
        {
            t.AddHeader( HTTPHeaders.Content_Type, ContentTypes.application_json );
        }

        watch = Stopwatch.StartNew();
        using ( var c = new RestClient( some server ) )
            Execute( c, "", Method.Post, Add );

        watch.Stop();
        elapsed[i] = ( i, watch.Elapsed );
    }

    Parallel.For( 0, total, Test );

    ////

    Array.Sort( elapsed, ( x, y ) => y.Item2.CompareTo( x.Item2 ) );
    for ( int i = 0; i < total; i++ )
        Console.WriteLine( $@"{elapsed[i].Item1.ToString().PadLeft( width )} => {elapsed[i].Item2:mm\:ss\.fff}" );
}

If you use the above code, which uses c.Execute( i ), to communicate to a server, there will be very long delays (up to roughly 10 s). If you replace c.Execute( i ) with c.ExecuteAsync( i ).Result, the time is reduced to just roughly 1 s.

Expected behavior
Since both RestClient.Execute() and RestClient.ExecuteAsync().Result are synchronous, both should have the exact same performance.

Desktop

  • OS: Windows 11
  • .NET 8.0
  • Version 114.0.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the RestClient.Execute() and RestClient.ExecuteAsync() entry points and reproduce the timing difference with the provided Parallel.For example on .NET 8. Compare their synchronous execution paths under parallel use, then verify that equivalent calls have comparable performance and that the reported delay is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.