使用DB2和.NET进行异步查询

时间:2018-07-05 07:26:48

标签: c# .net asynchronous async-await db2

我正在尝试使用最新的DB2 .NET驱动程序(v11.1 fixpack 3)在带有VS2017的Windows 10和Windows 10上针对.NET 4.7对DB2执行异步查询,我希望性能有所提高,但似乎是异步变量不是真正的异步。我完全错了吗?异步和同步变体的性能大致相同。如果我用Task.Delay或其他任务替换查询,则异步性能会更好。

我认为我有错误或过高的期望,因为我没有正确使用DB2驱动程序进行异步或DB2驱动程序配置错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBM.Data.DB2;
using System.Data;
using System.Reflection;

namespace Db2AsyncTest
{
   public class Program
   {
        static void Main(string[] args)
        {
            //Warm up
            TestSync();
            TestAsync().Wait();
            TestSync();
            Console.ReadKey();
        }

    static async Task TestAsync()
    {
        var dt = DateTime.Now;
        var tasks = new List<Task>();
        for (int i = 0; i < 10; i++)
        {
            tasks.Add(ExecuteAsync());
        }
        await Task.WhenAll(tasks);
        var dt2 = DateTime.Now;
        var timeSpan = dt2 - dt;
        Console.WriteLine("Async Time: " + timeSpan.TotalMilliseconds);

    }

    static async Task ExecuteAsync()
    {
        using (var conn = new DB2Connection(""))
        {
            await conn.OpenAsync();
            var cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM EMPLOYEE";
            var reader = await cmd.ExecuteReaderAsync();
            var dt = new DataTable();
            dt.Load(reader);
        }
    }


    static void TestSync()
    {
        var dt = DateTime.Now;
        for (int i = 0; i < 10; i++)
        {
            ExecuteSync();
        }
        var dt2 = DateTime.Now;
        var timeSpan = dt2 - dt;
        Console.WriteLine("Sync Time: " + timeSpan.TotalMilliseconds);

    }



    static void ExecuteSync()
    {
        using (var conn = new DB2Connection(""))
        {
            conn.Open();
            var cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM EMPLOYEE";
            var reader = cmd.ExecuteReader();
            var dt = new DataTable();
            dt.Load(reader);
        }
    }

}

}

0 个答案:

没有答案