C#C
C#β€’4y ago
Kosta

❔ Why is my grpc so much slower then my minimal api? Need The Csharp Gods

Hey there a bit of an odd question
I've downloaded the code for this :
https://medium.com/geekculture/build-high-performant-microservices-using-grpc-and-net-6-adde158c5ac
basically is streams 1 line at a time to a client~---takes about 1 min to stream 1.5 mil lines,
Tried replicating the same behavior with Minimal Api and not sure how,
But it takes 13 seconds to read and stream all 5 million records,
Shouldnt it be slower?
I think my code is badly messed up and probably rotter logic all over the place πŸ˜„
Any Help would be swell
My Ui Code:

using HttpClient client = new();

using HttpResponseMessage response = await client.GetAsync(
    "http://localhost:5247/test",
    HttpCompletionOption.ResponseHeadersRead
).ConfigureAwait(false);
IAsyncEnumerable<Sales> Sales= await response.Content.ReadFromJsonAsync<IAsyncEnumerable<Sales>>().ConfigureAwait(false);
var count = 0;
var watch = System.Diagnostics.Stopwatch.StartNew();

await foreach (var each in Sales)
{
    Console.WriteLine(String.Format("New Order Receieved from {0}-{1},Order ID = {2}, Unit Price ={3}, Ship Date={4}", each.Country, each.Region, each.OrderID, each.UnitPrice, each.TotalRevenue));

}



The Backend:
app.MapGet("/test", async () =>
{
return MakeHttpCall();


});
``` async IAsyncEnumerable<Sales> MakeHttpCall()
{
    var watch = System.Diagnostics.Stopwatch.StartNew();
    int Count = 0;

    using (var reader = new StreamReader("path"))
    {
        while (watch.Elapsed < TimeSpan.FromSeconds(60) && !reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var pieces = line.Split(',');
            var _model = new Sales();
          
            _model.Region = pieces[0];
            _model.Country = pieces[1];

            yield return _model;

        }

    }


}


Also the ui seems to start prining the text only when its all done, meaning not rly streaming..
(removed all the code regarding watch and count)
Save me C# Gods
Medium
Learn how to leverage server streaming in gRPC to deliver 5Million records in a breeze
Was this page helpful?