How to best simulate IO bound work?

一个人想着一个人 提交于 2019-12-23 05:33:16

问题


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;    
}

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?


回答1:


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

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();
  }
  ...

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



来源:https://stackoverflow.com/questions/24459573/how-to-best-simulate-io-bound-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!