项目作者: pinpoint-solutions

项目描述 :
A helper class for load testing http api endpoints.
高级语言: C#
项目地址: git://github.com/pinpoint-solutions/HttpLoadRunner.git
创建时间: 2019-04-03T13:11:58Z
项目社区:https://github.com/pinpoint-solutions/HttpLoadRunner

开源协议:

下载


HttpLoadRunner

This is a helper class for load testing http api endpoints. You can specify global parameters for the class, then re-use the class for various tests. You can specify the following parameters :

  • ServiceUrl - this is the main url for your service
  • EndPoint - this is the route to your endpoint from the service url
  • BearerToken - in case the service is secured with Auth0 you will need an access token
  • UsedHttpMethod - the HttpMethod used
  • NumCalls - the number of times each endpoint is hit
  • NumThreads - the number of threads that are used

When the tests have completed, the following class properties are filled with results:

  • ResponseTimes - the list of response times for each call
  • ResponseMessages - the list of HttpResponseMessages for each call

The following is a basic example of how to use the HttpLoadRunner in your testclass and use it in 2 test methods:

  1. namespace ExampleTests
  2. {
  3. [TestClass]
  4. public class ExampleTest
  5. {
  6. private static HttpLoadRunner _httpLoadRunner;
  7. private int _maxResponseTime;
  8. private int _avgResponseTime;
  9. [TestInitialize]
  10. public void Init()
  11. {
  12. _maxResponseTime = 10000;
  13. _avgResponseTime = 50000;
  14. //-- Create Helper --
  15. _httpLoadRunner = new HttpLoadRunner
  16. {
  17. ServiceUrl = "https://test.api.com/",
  18. NumCalls = 10,
  19. NumThreads = 2
  20. };
  21. _httpLoadRunner.AddHttpRequestHeader("X-ClientId", "myClientId");
  22. }
  23. [TestMethod]
  24. public void Test01_Get()
  25. {
  26. //arrange
  27. _httpLoadRunner.ClearResults();
  28. _httpLoadRunner.EndPoint = "/api/version";
  29. _httpLoadRunner.UsedHttpMethod = HttpMethod.Get;
  30. //act
  31. _httpLoadRunner.Run();
  32. //assert
  33. _httpLoadRunner.ResponseMessages.All(p => p.IsSuccessStatusCode).Should().BeTrue();
  34. _httpLoadRunner.ResponseTimes.Max().Should().BeLessOrEqualTo(_maxResponseTime);
  35. _httpLoadRunner.ResponseTimes.Average().Should().BeLessOrEqualTo(_avgResponseTime);
  36. }
  37. [TestMethod]
  38. public void Test02_Post()
  39. {
  40. //arrange
  41. _httpLoadRunner.ClearResults();
  42. _httpLoadRunner.EndPoint = "/api/postmethod";
  43. _httpLoadRunner.UsedHttpMethod = HttpMethod.Post;
  44. _httpLoadRunner.AddPostData(new {id = 25});
  45. //act
  46. _httpLoadRunner.Run();
  47. //assert
  48. _httpLoadRunner.ResponseMessages.All(p => p.IsSuccessStatusCode).Should().BeTrue();
  49. _httpLoadRunner.ResponseTimes.Max().Should().BeLessOrEqualTo(_maxResponseTime);
  50. _httpLoadRunner.ResponseTimes.Average().Should().BeLessOrEqualTo(_avgResponseTime);
  51. }
  52. }
  53. }

You can add various PostData in case you want to call a Post method with different data for each call, just use the AddPostData method to add an object to the list. You should add the same amount of elements as numCalls, but if there is no corresponding object in the PostData the class will use the first element in PostData.

You can add multiple HttpRequestHeaders by calling AddHttpRequestHeader.