C
C#2y ago
Senpawaii

✅ Weird values on accessing Database using EF DbContext under throughput load test

Hello! I'm testing an ASP.NET application that uses an SQL Database. To access this database I define a dependency service, that I add to my set of services in the following manner:
services.AddEntityFrameworkSqlServer()
.AddDbContext<DiscountContext>(options => {
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions => {
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
});
services.AddEntityFrameworkSqlServer()
.AddDbContext<DiscountContext>(options => {
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions => {
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
});
From my understanding a Db Connection is opened and closed in the scope of each request to my application, that is, each request uses its own connection to the Db. Upon testing my application, by varying the throughput of requests to the application, I notice that for lower throughput values the average access to the database is around 20ms. Under load, this value drops to 6ms/req. The executed queries are the same. I would assume that since each request uses its own connection (from the connection pool), the average latency would increase as I increase the throughput. Am I missing any "special" feature of EF that optimizes the accesses to the Db transparently in the background as the load increases? If this question isn't clear, please feel free to note it, I will try to explain it better 😄 thanks
4 Replies
Senpawaii
SenpawaiiOP2y ago
I should add that all the queries return the expected values (both under load and not under load).
JakenVeina
JakenVeina2y ago
there's a loooooooooooooooot of things that could be responsible it could be EF caching query compilations it could be the .NET runtime itself applying more aggressive JIT optimizations it could be the database doing some amount of compilation or results caching you are correct in your description of how connections are managed each request gets its own DbConnection object, which pulls actual connections from an underlying connection pool, which grows and shrinks as needed so, under heavy load, you are skipping most or all of the overhead of establishing database connections that could also account for reduced latency
Senpawaii
SenpawaiiOP2y ago
Ok, I think I get it! Thanks 😄
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server