Added connection statistics to API.
This commit is contained in:
parent
8e08df2fd2
commit
7798f09dc7
4 changed files with 96 additions and 9 deletions
|
@ -23,12 +23,56 @@
|
|||
|
||||
|
||||
#include <algorithm>
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
|
||||
#include "api/NetworkState.h"
|
||||
#include "net/SubmitResult.h"
|
||||
|
||||
|
||||
NetworkState::NetworkState() :
|
||||
diff(0),
|
||||
accepted(0),
|
||||
failures(0),
|
||||
rejected(0),
|
||||
total(0),
|
||||
m_active(false)
|
||||
{
|
||||
memset(pool, 0, sizeof(pool));
|
||||
}
|
||||
|
||||
|
||||
int NetworkState::connectionTime() const
|
||||
{
|
||||
return m_active ? ((uv_now(uv_default_loop()) - m_connectionTime) / 1000) : 0;
|
||||
}
|
||||
|
||||
|
||||
uint32_t NetworkState::avgTime() const
|
||||
{
|
||||
if (m_latency.empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (uint32_t) connectionTime() / m_latency.size();
|
||||
}
|
||||
|
||||
|
||||
uint32_t NetworkState::latency() const
|
||||
{
|
||||
const size_t calls = m_latency.size();
|
||||
if (calls == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto v = m_latency;
|
||||
std::nth_element(v.begin(), v.begin() + calls / 2, v.end());
|
||||
|
||||
return v[calls / 2];
|
||||
}
|
||||
|
||||
|
||||
void NetworkState::add(const SubmitResult &result, const char *error)
|
||||
{
|
||||
if (error) {
|
||||
|
@ -44,4 +88,25 @@ void NetworkState::add(const SubmitResult &result, const char *error)
|
|||
topDiff[ln] = result.actualDiff;
|
||||
std::sort(topDiff.rbegin(), topDiff.rend());
|
||||
}
|
||||
|
||||
m_latency.push_back(result.elapsed > 0xFFFF ? 0xFFFF : (uint16_t) result.elapsed);
|
||||
}
|
||||
|
||||
|
||||
void NetworkState::setPool(const char *host, int port, const char *ip)
|
||||
{
|
||||
snprintf(pool, sizeof(pool) - 1, "%s:%d", host, port);
|
||||
|
||||
m_active = true;
|
||||
m_connectionTime = uv_now(uv_default_loop());
|
||||
}
|
||||
|
||||
|
||||
void NetworkState::stop()
|
||||
{
|
||||
m_active = false;
|
||||
diff = 0;
|
||||
|
||||
failures++;
|
||||
m_latency.clear();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue