-1

I am trying to create a performance test in .NET and one of the steps is to "call" server at defined speed like: persistent ~400 calls per second.

The problem is that running server call has different length from: 10ms to 100ms. My first try was simple for loop with Thread.Sleep but this gives me results form 250-600 calls per second, depending on moment and server responses :(

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • 1
    This is a very broad question but you should try using adaptive delays where you calculate a delay based on past server response times. This won't give you a perfectly steady call rate but it'll get you close. – xxbbcc May 08 '15 at 17:48
  • 1
    Do you need equal period between calls? You can issue 400 calls as fast as you can and then wait for the next second. – werewindle May 08 '15 at 17:49
  • 1
    In line with @xxbbcc suggestion: You could try and maintain a **windowed** moving average of the round trip time and use the current average to estimate your delay. To schedule your calls, instead of sleeping and associated wake up inaccuracies, you may want to do a busy waiting loop. An implementation of a windowed moving average calculator can be found here: http://stackoverflow.com/questions/29265626/calculate-a-moving-standard-deviation/29269137#29269137 – Alex May 08 '15 at 18:01
  • You may also want to use the `System.Threading.Thread` class since these threads are not managed by the thread pool. Using anything from TPL will be managed by the thread pool and fight with other system resources. If you would rather use something from TPL, creating a list of list of tasks with 400 tasks each and then iterate each list to start each task from that list on another thread. This way you can `Task.WaitAll` on each new thread that starts the 400 tasks, do any calculations in that thread and then `Task.WaitAny` on your master list to return results as they come in. – Jacob Roberts May 08 '15 at 18:10
  • Why not show what you tried, so we can help you improve it? – John Saunders May 08 '15 at 18:20
  • @JohnSaunders I exactly described my code. It was simple infinite for loop with Thread.Sleep. You really thinks that posting this 4 lines of code will be useful? – Piotr Stapp May 09 '15 at 07:42

2 Answers2

1

I assume you need a steady rate of calls per second (that is, you need 400 calls per second distributed evenly over the second as much as possible).

You need adaptive delays during your test - if your server sends responses slowly, you need less delay; when the server is fast, you need more delay between calls. You can keep track of past server response times and calculate the delay for the current call based on that to come as close to the 400 calls / second rate as possible. It'll still fluctuate somewhat but it'll be much better than a simple hard-coded Thread.Sleep.

You need to keep in mind, that with many active threads (depending on hardware resources), the sleep duration of a thread may fluctuate as well. I don't know how familiar you are with the nuances of Thread.Sleep - you should read this answer to understand the limitations, as this may play a role in scheduling your test calls.

Community
  • 1
  • 1
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
0

I think implementing loads and performance tests for your functions could be complicated considering all the threading stuff that needs to be simulated. You can use Visual Studio Load test for this purpose and it has wizards to help you simulate what kind of load you need... Take a look at this article for your reference: https://msdn.microsoft.com/en-us/library/vstudio/dd293540(v=vs.110).aspx

Just note that for LoadTest you will need the VS Ultimate version...

Aram
  • 5,537
  • 2
  • 30
  • 41