具有大量并行任务的WPF GUI性能 [英] WPF GUI Performance with a large number of parallel tasks

查看:73
本文介绍了具有大量并行任务的WPF GUI性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个小型客户端(WPF),对我们的系统进行了一些压力测试.本质上,它必须在Asp.Net WebApi端点上并行调用各种方法.

I developed a small client (WPF) to make some stress test on our systems. Essentially it has to call various methods on an Asp.Net WebApi endpoint in parallel.

每次按下开始"按钮时,它都会并行生成4000个任务(异步-等待),并发出压力请求,等待它们全部完成,然后再次执行-直到用户单击停止"按钮. GUI装饰有进度条和一些计数器:错误的请求,已完成的请求,正在进行的请求.我获得这些信息是因为发出大量压力请求的对象暴露了一些事件:

Each time you press "Start" it generates 4000 tasks (Async - Await) in parallel with request to stress, waits until they all finish, then it does it again - until the user clicks the stop button. The GUI is decorated with a progress bar and some counters: requests in error, completed request, in progress requests. I obtain these informations because the object that makes the batch of stress requests exposes some events:

var stressTestTask = new stressTestTask(LogService, configuration);

stressTestTask.ErrorRequestCountChanged += stressTestTask_ErrorRequestCountChanged;
stressTestTask.GoodRequestCountChanged += stressTestTask_GoodRequestCountChanged;
stressTestTask.TryRequestCountChanged += stressTestTask_TryRequestCountChanged;

_executionCancellationToken = new CancellationTokenSource();

await Task.Run(
    () => stressTestTask.ApiStressTestTask(_executionCancellationToken.Token),
    _executionCancellationToken.Token);

整个执行从ICommand(MVVM)开始:

The whole execution is started from an ICommand (MVVM):

private RelayCommand _startCommand;
public RelayCommand StartCommand
{
    get
    {
        return _startCommand ?? (_startCommand = new RelayCommand(
            async () =>
            {
                await StartStressTest();
            }));
    }
}

RelayCommand是库Mvvm-LightICommand的实现.

我不了解这种行为:如果我将一批任务配置为少量"任务,例如2000,则GUI在执行时不会冻结.相反,如果我选择了5000个任务,那么一段时间后它会冻结.如果然后我打开客户端的.exe的另一个实例,并在每个实例上选择2000,则GUI都可以响应.

What I don't understand is this behaviour: if I configure my batch of tasks with a "low" number of tasks, for example 2000, the GUI doesn't freeze while executing. If instead I choose 5000 tasks, after a while it freezes. If then I open another instance of the .exe of my client and I choose 2000 on each, the GUI is responsive in both.

我的第一个问题是:为什么在响应性方面打开带有x任务的实例比打开带有x/n任务的n实例更糟糕?它与Windows Scheduler和第一种情况下我只有一个进程有关吗?

My first question is: why opening one instance with x tasks is worse in terms of responsivness than opening n instances with x/n tasks? Is it something related to Windows Scheduler and the fact that in the first case I have only one process?

我的第二个问题是:如何解决这个问题,以使所有工作都在单个GUI上进行?我考虑过用单批压力测试制作一个控制台应用程序,并为我想要的每个实例从GUI调用一个命令,以便为每个批处理生成一个进程.

My second questions is: how can I address the problem to make everything work on a single GUI? I thought about making a console application with the single batch of stress tests and calling a command from the GUI for each instance I want, in order to generate a process for every batch.

推荐答案

您是否通过调用UI上下文来处理这些API事件?如果发生许多调用,您将使调度程序充满操作,并使UI挂起并落后于用户输入.

Are you handling those API events by invoking to the UI context? If you have many invocations occurring you will flood the dispatcher with operations and cause the UI to hang and lag behind user input.

尝试批处理UI更新.

这篇关于具有大量并行任务的WPF GUI性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆