2
0

throughput.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "KSTest.hpp"
  23. #include "libks/ks_atomic.h"
  24. #include "libks/internal/ks_throughput.h"
  25. #include "catch/catch.hpp"
  26. using namespace signalwire::pal;
  27. using ThroughputHandle = ksutil::KSHandle<ks_throughput_ctx_t, (ksutil::KSHandleEnum)ks_handle_types::KS_HTYPE_THROUGHPUT, ks_throughput_t>;
  28. TEST_CASE("throughput")
  29. {
  30. ThroughputHandle rate;
  31. KCHECK(ks_throughput_create_ex(&rate, 2, 1));
  32. // Tell throughput we'll update now by hand and to use
  33. rate->static_now_sec = 1000;
  34. REQUIRE(rate->count_bucket == 0);
  35. KCHECK(ks_throughput_report(rate, 1024));
  36. REQUIRE(rate->count_bucket == 0);
  37. REQUIRE(rate->count_bucket == 0);
  38. REQUIRE(rate->current_bucket.count == 1);
  39. REQUIRE(rate->current_bucket.size == 1024);
  40. // Advance time by one second
  41. rate->static_now_sec += 1;
  42. KCHECK(ks_throughput_update(rate));
  43. REQUIRE(rate->count_bucket == 1);
  44. REQUIRE(rate->count_bucket == 1);
  45. REQUIRE(rate->current_bucket.count == 0);
  46. REQUIRE(rate->current_bucket.size == 0);
  47. REQUIRE(rate->buckets[0].count == 1);
  48. REQUIRE(rate->buckets[0].size == 1024);
  49. // Now we should have a rate
  50. ks_throughput_stats_t stats;
  51. KCHECK(ks_throughput_stats(rate, &stats));
  52. REQUIRE(stats.rate_count == 1.0);
  53. REQUIRE(stats.rate_size == 1024.0);
  54. char rate_str[512];
  55. std::cout << ks_throughput_stats_render(&stats, rate_str, sizeof(rate_str)) << std::endl;
  56. // If we report, without advancing the time, the rate shouldn't change but the first
  57. // bucket should be staged
  58. KCHECK(ks_throughput_report(rate, 1024));
  59. KCHECK(ks_throughput_stats(rate, &stats));
  60. REQUIRE(rate->count_bucket == 1);
  61. REQUIRE(stats.rate_count == 1.0);
  62. REQUIRE(stats.rate_size == 1024.0);
  63. // As the first bucket is still 'uncompleted' but it should be additive
  64. REQUIRE(rate->current_bucket.count == 1);
  65. REQUIRE(rate->current_bucket.size == 1024);
  66. REQUIRE(rate->buckets[0].count == 1);
  67. REQUIRE(rate->buckets[0].size == 1024);
  68. // Ok advance the clock by 1 second and update to see if it continues
  69. // to return the proper rate
  70. rate->static_now_sec += 1;
  71. KCHECK(ks_throughput_update(rate));
  72. KCHECK(ks_throughput_stats(rate, &stats));
  73. // Should have a new bucket at 0 with 1-2 rolled forward
  74. REQUIRE(rate->current_bucket.count == 0);
  75. REQUIRE(rate->current_bucket.size == 0);
  76. REQUIRE(rate->buckets[0].count == 1);
  77. REQUIRE(rate->buckets[0].size == 1024);
  78. REQUIRE(rate->buckets[1].count == 1);
  79. REQUIRE(rate->buckets[1].size == 1024);
  80. // Same rate as before
  81. REQUIRE(stats.rate_size == 1024.0);
  82. REQUIRE(stats.rate_count == 1.0);
  83. std::cout << ks_throughput_stats_render(&stats, rate_str, sizeof(rate_str)) << std::endl;
  84. // Now advance the time without reporting anything, we should start
  85. // seeing the rate trend downwards
  86. rate->static_now_sec += 1;
  87. KCHECK(ks_throughput_update(rate));
  88. KCHECK(ks_throughput_stats(rate, &stats));
  89. REQUIRE(rate->current_bucket.count == 0);
  90. REQUIRE(rate->current_bucket.size == 0);
  91. REQUIRE(rate->buckets[0].count == 0);
  92. REQUIRE(rate->buckets[0].size == 0);
  93. REQUIRE(rate->buckets[1].count == 1);
  94. REQUIRE(rate->buckets[1].size == 1024);
  95. // Rate should be cut in half
  96. REQUIRE(stats.rate_size == 1024.0 / 2);
  97. REQUIRE(stats.rate_count == 1.0 / 2);
  98. // Finally verify we can render the stats
  99. LOG(TEST, stats);
  100. }