AuroraRuntime/Source/IO/Net/SocketStatAverageBps.hpp
Reece 99c5e1fa65 A pretty large patch not worth breaking up into separate commits
[*] Split up Aurora Async
[*] Split Async app into seperate ThreadPool concept
[*] Fix various OSThread bugs and tls transfer issues
[*] Set default affinity to 0xFFFFFFFF
[*] Update Build script
[+] Add AuTuplePopFront
[+] New Network Interface (unimplemented)
[*] Stub out the interfaces required for a better logger
[*] Fix Win32 ShellExecute bug; windows 11 struggles without explicit com init per the docs - now deferring to thread pool
[*] Update gitignore
[*] Follow XDG home standard
[*] Refactor some namespaces to use the shorthand aliases
[*] Various stability fixes
2021-11-05 17:34:23 +00:00

86 lines
2.8 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SocketStatAverageBps.hpp
Date: 2021-10-31
Author: Reece
***/
#pragma once
namespace Aurora::IO::Net
{
struct SocketStatAverageBps
{
inline AuUInt32 GetBytesPerMS(AuUInt32 timeNow)
{
return (double(bytesTransferred) / (double(frameStart - timeNow) + std::numeric_limits<double>::epsilon()));
}
inline AuUInt32 GetBytesPerSecondNormalized(AuUInt32 timeNow)
{
AU_LOCK_GUARD(lock);
// If timeNow isn't at least that of one socket frame, then we just have to return some old data
if (frameZero) return GetLastBytesPerSecond();
// Cool we can extrapolate usage using a time weight at least that of one server frame
// Some useful variables...
auto frameAtPoint = GetBytesPerMS(timeNow);
auto timeElapsed = frameStart - timeNow;
// Edge case: we aren't receiving much data. if we have more than 1 second of data, we should just average it
if (timeElapsed > 1000) return frameAtPoint / 1000; // from ms
// else assume constant usage will continue to trend for at least another second
// the actual extrapolation
auto weight = double(1000) / double(timeElapsed);
return double(frameAtPoint) * weight;
}
inline AuUInt32 GetLastBytesPerSecond()
{
AU_LOCK_GUARD(lock);
return (double(lastBytesTransferred) / (double(frameLastEnd - frameLastStart) + std::numeric_limits<double>::epsilon())) / 1000;
}
inline void Reset()
{
bytesTransferred = 0;
frameStart = 0;
frameLastEnd = 0;
frameLastStart = 0;
lastBytesTransferred = 0;
}
inline void Add(AuUInt32 timeNow, AuUInt32 bytes)
{
AU_LOCK_GUARD(lock);
auto nextTick = frameLastEnd + 1000;
if (nextTick < timeNow)
{
frameLastEnd = timeNow;
frameLastStart = std::exchange(frameStart, timeNow);
lastBytesTransferred = std::exchange(bytesTransferred, bytes);
frameZero = true;
}
else
{
frameZero = false;
bytesTransferred += bytes;
}
}
AuThreadPrimitives::SpinLock lock;
AuUInt32 frameStart {};
AuUInt32 bytesTransferred {};
AuUInt32 frameLastStart {};
AuUInt32 frameLastEnd {};
AuUInt32 lastBytesTransferred {};
bool frameZero {true};
};
}