我正在尝试对NorthWind数据库进行一些分析,以测试性能问题。
我的查询如何运行并永久挂起。奇怪的是,当我们说4个CONCURRENT_CALLS
时,一切正常我正在使用SQL Server 2016运行EF6
我知道实体框架在同一个上下文中对异步请求不起作用,所以我们在每个线程中产生一个新的上下文。
class Program
{
public static int MAX_IO_THREADS = 2000;
public static int MIN_IO_THREADS = 1000;
public static int CONCURRENT_CALLS = 10;
static void Main(string[] args)
{
ConfigureThreadPool();
WarmUpContexts();
ProfileAction();
Console.ReadKey();
}
protected static void ProfileAction()
{
var callRange = Enumerable.Range(0, CONCURRENT_CALLS);
var stopwatch = new Stopwatch();
stopwatch.Start();
var results = new ConcurrentDictionary<int, string>();
Parallel.ForEach(callRange, i =>
{
var callSw = new Stopwatch();
callSw.Start();
var result = RunTest().Result;
callSw.Stop();
results.TryAdd(i, $"Call Index: {i} took {callSw.ElapsedMilliseconds} milliseconds to complete");
});
stopwatch.Stop();
var milliseconds = stopwatch.ElapsedMilliseconds;
foreach (var result in results.OrderBy(x => x.Key))
{
Console.WriteLine(result);
}
Console.WriteLine("Total Time " + milliseconds);
}
public static void ConfigureThreadPool()
{
ThreadPool.GetMinThreads(out var defaultMinWorkerCount, out var _);
ThreadPool.GetMaxThreads(out var defaultMaxWorkerCount, out var _);
ThreadPool.SetMaxThreads(defaultMaxWorkerCount, MAX_IO_THREADS);
var successMin = ThreadPool.SetMinThreads(defaultMinWorkerCount, MIN_IO_THREADS);
}
static void WarmUpContexts()
{
using (var stagingContext = new NORTHWNDEntities())
{
stagingContext.Employees.First();
}
}
static async Task<int> RunTest()
{
using (var context = new NORTHWNDEntities())
{
var employees = await context.Employees.ToArrayAsync();
return employees.Length;
}
}
}
我也尝试修改gcServer以更好地使用并发性
<runtime>
<gcServer enabled="true" />
</runtime>
我修改了连接字符串以增加最小池大小
综合安全=真; pooling = true;最小池大小= 100;
一切都永远挂起。 当我在sql上运行sp_whoisactive时,我可以看到我的用户上有(636ms)ASYNC_NETWORK_IO被捕获,并且一次只显示一个连接。
这是entityframework中的错误吗?