如何最好地模拟IO绑定工作? [英] How to best simulate IO bound work?

查看:212
本文介绍了如何最好地模拟IO绑定工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很容易模拟沉重的CPU负载.

It's easy to simulate a heavy CPU load.

public void ExerciseCPU()
{
  for (int i = 0; i < 999; i++)
    for (int j= 0; j < 999; j++)
      for (int k = 0; k < 999; k++)
        int x = i * j * k;    
}

模拟重载或慢速IO的可比较短代码块是什么?换句话说,什么是模拟CPU上缓慢的缓慢进程的技术?

What's a comparable short code block for simulating heavy or slow IO? In other words, what's a technique for simulating a slow process that's light on the CPU?

推荐答案

假设您要在IO期间在模拟/拦截的IO层中测试应用程序是否存在长时间的延迟:

Assuming you want to test your app for long delays during IO in your mocked/intercepted IO layer:

Thread.Sleep将为同步IO操作(对于async/await Task.Delay)完美地模拟非CPU负载.

Thread.Sleep will simulate non-CPU load perfectly fine for synchronous IO operations, for async/await Task.Delay.

class SlowFakeDataFromDeviceProvider : IDataFromDevice    
{
  Data GetFromDevice(...)
  {
    Thread.Sleep(1000);
    return ConstructFakeData();
  }
  ...

请注意,如果您确实需要大量的IO负载,请考虑对大文件进行无缓冲读取(以避免对缓存造成内存压力).

Note that if you actually need to have significant IO load - consider unbuffered read of a large file (to avoid memory pressure on cache).

这篇关于如何最好地模拟IO绑定工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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