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

EnsureCreatedAsync() performance downgrade after upgrading Microsoft.Data.SqlClient from version 5.1.1 to 5.1.4(5.1.1-->5.1.4) #2542

Closed
akhiltyagi98 opened this issue May 29, 2024 · 13 comments

Comments

@akhiltyagi98
Copy link

akhiltyagi98 commented May 29, 2024

Describe the bug

I am using Microsoft.EntityFrameworkCore localDB SQLServer to run test cases, I use below commands in each test cases

await context.Database.EnsureDeletedAsync().ConfigureAwait(false);
await context.Database.EnsureCreatedAsync().ConfigureAwait(false);

But after upgrading Microsoft.Data.SqlClient package version from 5.1.1 to 5.1.4(5.1.1-->5.1.4), EnsureCreatedAsync() performance downgraded, can you please help me understand this ?

To reproduce

Please use this code to check

using Microsoft.EntityFrameworkCore;
using Moq;
using Xunit;

namespace POCSQLClientPerformence.Tests
{
    public class SQLClientPerformenceDB
    {
        private IDbContextFactory<TestDBContext> factory;

        public SQLClientPerformenceDB()
        {
            CreateDbAsync().GetAwaiter().GetResult();
        }

        async Task CreateDbAsync()
        {
            Func<TestDBContext> CreateContext = () =>
            {
                var optionsBuilder = new DbContextOptionsBuilder<TestDBContext>()
                    .UseSqlServer(@$"Server=(localdb)\MSSQLLocalDb;Database=ABCTestDb;Trusted_Connection=True;MultipleActiveResultSets=true");

                return new TestDBContext(optionsBuilder.Options);
            };

            var factoryMock = new Mock<IDbContextFactory<TestDBContext>>();
            factoryMock.Setup(it => it.CreateDbContextAsync(It.IsAny<CancellationToken>())).ReturnsAsync(CreateContext);

            this.factory = factoryMock.Object;
            using var context = await this.factory.CreateDbContextAsync(CancellationToken.None).ConfigureAwait(false);

            await context.Database.EnsureDeletedAsync().ConfigureAwait(false);
            await context.Database.EnsureCreatedAsync().ConfigureAwait(false);
        }

        [Fact]
        public async Task FirstTest()
        {
            // Arrange
            using var context = await this.factory.CreateDbContextAsync(CancellationToken.None).ConfigureAwait(false);

            // Assert - Validate the results or behavior
            Assert.NotNull(context); // Just a dummy assertion for demonstration
        }

        [Fact]
        public async Task SecoundTest()
        {
            // Arrange
            using var context = await this.factory.CreateDbContextAsync(CancellationToken.None).ConfigureAwait(false);

            // Assert - Validate the results or behavior
            Assert.NotNull(context); // Just a dummy assertion for demonstration
        }

        [Fact]
        public async Task ThirdTest()
        {
            // Arrange
            using var context = await this.factory.CreateDbContextAsync(CancellationToken.None).ConfigureAwait(false);

            // Assert - Validate the results or behavior
            Assert.NotNull(context); // Just a dummy assertion for demonstration
        }
    }
 @}

Microsoft.Data.SQLClient: 5.2.0(Latest) test case execution time:
image

Microsoft.Data.SQLClient version 5.1.1 or later package test case execution time:
image

@ErikEJ
Copy link
Contributor

ErikEJ commented May 29, 2024

Fixed in 5.2.1 and 5.1.6

@arellegue arellegue added the 🆕 Triage Needed For new issues, not triaged yet. label May 29, 2024
@arellegue
Copy link
Contributor

Please try again after the 5.2.1 Hotfix is published and please let us know if you are still having an issue.

@akhiltyagi98
Copy link
Author

akhiltyagi98 commented May 29, 2024

I did not find Microsoft.Data.SqlClient version 5.2.1 and 5.1.6 please check published version. https://www.nuget.org/packages/Microsoft.Data.SqlClient/5.2.0?_src=template#versions-body-tab

Could you please explain why this is happening? mainly await context.Database.EnsureCreatedAsync().ConfigureAwait(false); this line taking time.

@arellegue
Copy link
Contributor

That feature you requested to be investigated is in EntityFramework package. Could you open an issue with EntityFramework instead, please?

https://github.com/dotnet/efcore/issues

@akhiltyagi98
Copy link
Author

akhiltyagi98 commented May 29, 2024

But it's due to Microsoft.Data.SqlClient version only you can check upgrading Microsoft.Data.SqlClient version from 5.1.1 to 5.1.4. you can observe time difference

@arellegue
Copy link
Contributor

We will investigate and get back to you when we have an update. When the Hotfix 5.2.1 is released later this week, could you try that and let us know if you are still having an issue.

@arellegue
Copy link
Contributor

Could you provide a runnable repro in a zip file, please?

@akhiltyagi98
Copy link
Author

@arellegue provided in MS Teams.

@akhiltyagi98 akhiltyagi98 changed the title performance downgrade after upgrading from version 5.1.1 to 5.1.4(5.1.1-->5.1.4) l performance downgrade after upgrading Microsoft.Data.SqlClient from version 5.1.1 to 5.1.4(5.1.1-->5.1.4) l May 30, 2024
@akhiltyagi98 akhiltyagi98 changed the title performance downgrade after upgrading Microsoft.Data.SqlClient from version 5.1.1 to 5.1.4(5.1.1-->5.1.4) l EnsureCreatedAsync() performance downgrade after upgrading Microsoft.Data.SqlClient from version 5.1.1 to 5.1.4(5.1.1-->5.1.4) May 30, 2024
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
@ajcvickers ajcvickers reopened this May 30, 2024
@arellegue arellegue removed the 🆕 Triage Needed For new issues, not triaged yet. label May 30, 2024
@arellegue
Copy link
Contributor

arellegue commented May 30, 2024

I have investigated this issue and found the root cause to be in the introduction of transient fault handling which added the delay.
We will discuss how to appropriately implement a fix. We will let you know when the issue is fixed. In the meantime, you may want to add ConnectRetryInterval=1 in the connection string or whichever interval time in seconds works for you best.

@arellegue arellegue added the 🆕 Triage Needed For new issues, not triaged yet. label May 31, 2024
@arellegue
Copy link
Contributor

arellegue commented May 31, 2024

Duplicate of #2481

This issue was caused by #1983. Hotfix will be published shortly.

@arellegue arellegue removed the 🆕 Triage Needed For new issues, not triaged yet. label May 31, 2024
@JRahnama
Copy link
Contributor

JRahnama commented Jun 1, 2024

@akhiltyagi98 htofix v5.2.1 is released. Can you test with that version?

@akhiltyagi98
Copy link
Author

@arellegue @JRahnama JRahnama

Thanks Team issue has been fixed in new release 5.2.1. Please mark this as close.

@akhiltyagi98
Copy link
Author

Fixed in hotfix release 5.2.1.

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

No branches or pull requests

5 participants