BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

extremely slow when benchmark test involves sqlconnection and sql datareader

Open daxu7509 opened this issue 3 years ago • 1 comments
trafficstars

hi, I want to compare some EDM lib's performance (i.e. automapper.data and dapper). So I created a quite simple test:

        using (var conn = new SqlConnection(@"xxxx"))
        {
            conn.Open();
            using (var cmd = new SqlCommand("select top 100 citicode, codetype, mexcode, sedolcode, isincode, unitcode, fundcode from sometable with(nolock)", conn))
            {
                for (int i = 0; i < loop; i++)
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                    using (var reader = cmd.ExecuteReader())
                    {
                        var result = _mapper.Map<IDataReader, IEnumerable<fxunit>>(reader);
                    }
                }
            }

        }

If I run this code in a console, this code runs really quick, but if I decorate it with a [Benchmark] attribute and run it with BenchmarkDotNet, this becomes really slow to finish.

image Is there a reason for this behaviour?

daxu7509 avatar Jul 07 '22 16:07 daxu7509

Hi @daxu7509,

BenchmarkDotNet executes each benchmark multiple times in the same process. Could you please try to do the same with your console application? Just wrap the code with for (int iter = 0; iter < 5; iter++) and measure the time of each iteration using Stopwatch (it's enough to notice the difference between 370us and 60s). I believe you should observe the same effect that you have with BenchmarkDotNet.

AndreyAkinshin avatar Jul 08 '22 11:07 AndreyAkinshin

No response for the provided hint, closing

adamsitnik avatar Oct 05 '22 10:10 adamsitnik