Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#2,375 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
csharp

Direzione di ricerca

Inizia tracciando i punti di ingresso RestClient.Execute() e RestClient.ExecuteAsync() e riproduci la differenza nei tempi con l'esempio Parallel.For fornito su .NET 8. Confronta i relativi percorsi di esecuzione sincrona in uso parallelo, quindi verifica che le chiamate equivalenti abbiano prestazioni comparabili e che il ritardo segnalato sia stato risolto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

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
Lingua principale
C#
Stelle
9.8k
Fork
2.3k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di restsharp/RestSharp

Tutte le issue di restsharp/RestSharp

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.