问题
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