Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less verbose request stats logs #543

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/http-request/include/besturlpicker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BestURLPicker {
struct ResponseTimeStats {
constexpr bool operator==(const ResponseTimeStats &) const noexcept = default;

constexpr auto score() const { return static_cast<uint32_t>(avgResponseTimeInMs) + avgDeviationInMs; }
constexpr auto score() const noexcept { return static_cast<uint32_t>(avgResponseTimeInMs) + avgDeviationInMs; }

uint16_t nbRequestsDone; // when reaching max, all stats are reset to give equal chances to all base URLs
uint16_t avgResponseTimeInMs; // approximation of moving average
Expand Down
31 changes: 18 additions & 13 deletions src/http-request/src/besturlpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void BestURLPicker::storeResponseTimePerBaseURL(int8_t baseUrlPos, uint32_t resp

// How many requests we consider to compute stats?
using NbRequestType = decltype(stats.nbRequestsDone);
static constexpr NbRequestType kMaxLastNbRequestsToConsider = 20;

if (stats.nbRequestsDone == std::numeric_limits<NbRequestType>::max()) {
// If one URL has reached the max number of requests done, we reset all stats and give an equal chance for all Base
Expand All @@ -64,36 +63,42 @@ void BestURLPicker::storeResponseTimePerBaseURL(int8_t baseUrlPos, uint32_t resp

++stats.nbRequestsDone;

static constexpr NbRequestType kMaxLastNbRequestsToConsider = 20;

NbRequestType nbRequestsToConsider = std::min(stats.nbRequestsDone, kMaxLastNbRequestsToConsider);

// Update moving average
uint64_t sumResponseTime = static_cast<uint64_t>(stats.avgResponseTimeInMs) * (nbRequestsToConsider - 1);
sumResponseTime += responseTimeInMs;
uint64_t newAverageResponseTime = sumResponseTime / nbRequestsToConsider;
const uint64_t sumResponseTime =
static_cast<uint64_t>(stats.avgResponseTimeInMs) * (nbRequestsToConsider - 1) + responseTimeInMs;
const uint64_t newAverageResponseTime = sumResponseTime / nbRequestsToConsider;
using RTType = decltype(stats.avgResponseTimeInMs);
if (newAverageResponseTime > static_cast<uint64_t>(std::numeric_limits<RTType>::max())) {
log::warn("Cannot update accurately the new average response time {} because of overflow", newAverageResponseTime);
// Cannot update accurately the new average response time because of overflow
stats.avgResponseTimeInMs = std::numeric_limits<RTType>::max();
} else {
stats.avgResponseTimeInMs = static_cast<RTType>(newAverageResponseTime);
}

// Update moving deviation
uint64_t sumDeviation = static_cast<uint64_t>(ipow(stats.avgDeviationInMs, 2)) * (nbRequestsToConsider - 1);
sumDeviation += static_cast<uint64_t>(
ipow(static_cast<int64_t>(stats.avgResponseTimeInMs) - static_cast<int64_t>(responseTimeInMs), 2));
auto newDeviationResponseTime = static_cast<uint64_t>(std::sqrt(sumDeviation / nbRequestsToConsider));
const uint64_t sumDeviation =
static_cast<uint64_t>(ipow(stats.avgDeviationInMs, 2)) * (nbRequestsToConsider - 1) +
static_cast<uint64_t>(
ipow(static_cast<int64_t>(stats.avgResponseTimeInMs) - static_cast<int64_t>(responseTimeInMs), 2));
const auto newDeviationResponseTime = static_cast<uint64_t>(std::sqrt(sumDeviation / nbRequestsToConsider));
using DevType = decltype(stats.avgDeviationInMs);
if (newDeviationResponseTime > static_cast<uint64_t>(std::numeric_limits<DevType>::max())) {
log::warn("Cannot update accurately the new deviation response time {} because of overflow",
newDeviationResponseTime);
// Cannot update accurately the new deviation response time because of overflow
stats.avgDeviationInMs = std::numeric_limits<DevType>::max();
} else {
stats.avgDeviationInMs = static_cast<DevType>(newDeviationResponseTime);
}

log::debug("Response time stats for '{}': Avg: {} ms, Dev: {} ms, Nb: {} (last: {} ms)", _pBaseUrls[baseUrlPos],
stats.avgResponseTimeInMs, stats.avgDeviationInMs, stats.nbRequestsDone, responseTimeInMs);
static constexpr NbRequestType kBitmapFrequencyPrintStats = (2 << 5) - 1;

if ((stats.nbRequestsDone & kBitmapFrequencyPrintStats) == NbRequestType{}) {
log::info("Response time stats for '{}': Avg: {} ms, Dev: {} ms, Nb: {} (last: {} ms)", _pBaseUrls[baseUrlPos],
stats.avgResponseTimeInMs, stats.avgDeviationInMs, stats.nbRequestsDone, responseTimeInMs);
}
}

int BestURLPicker::nbRequestsDone() const {
Expand Down
26 changes: 19 additions & 7 deletions src/tech/include/mathhelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,51 @@ constexpr int64_t ipow(int64_t base, uint8_t exp) noexcept {
case 255: // we use 255 as an overflow marker and return 0 on overflow/underflow
return base == 1 ? 1 : (base == -1 ? (1 - 2 * (exp & 1)) : 0);
case 6:
if (exp & 1) result *= base;
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 5:
if (exp & 1) result *= base;
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 4:
if (exp & 1) result *= base;
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 3:
if (exp & 1) result *= base;
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 2:
if (exp & 1) result *= base;
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 1:
if (exp & 1) result *= base;
if ((exp & 1U) != 0) {
result *= base;
}
[[fallthrough]];
default:
return result;
}
}

/// Optimization of ipow(int64_t base, 10)
/// Optimization of ipow(10, uint8_t exp)
constexpr int64_t ipow10(uint8_t exp) noexcept {
constexpr const int64_t kPow10Table[] = {1LL,
10LL,
Expand Down