mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Refine kbps, stub wall clock
This commit is contained in:
parent
4c40370986
commit
c26b475bae
15 changed files with 70 additions and 27 deletions
|
@ -163,7 +163,8 @@ srs_error_t SrsBandwidth::bandwidth_check(SrsRtmpServer* rtmp, ISrsProtocolStati
|
|||
}
|
||||
|
||||
// create a limit object.
|
||||
SrsKbps kbps;
|
||||
SrsWallClock clk;
|
||||
SrsKbps kbps(&clk);
|
||||
kbps.set_io(io_stat, io_stat);
|
||||
|
||||
int limit_kbps = _srs_config->get_bw_check_limit_kbps(_req->vhost);
|
||||
|
|
|
@ -39,7 +39,8 @@ SrsConnection::SrsConnection(IConnectionManager* cm, srs_netfd_t c, string cip)
|
|||
create_time = srs_get_system_time_ms();
|
||||
|
||||
skt = new SrsStSocket();
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
clk = new SrsWallClock();
|
||||
kbps = new SrsKbps(clk);
|
||||
kbps->set_io(skt, skt);
|
||||
|
||||
trd = new SrsSTCoroutine("conn", this);
|
||||
|
@ -50,6 +51,7 @@ SrsConnection::~SrsConnection()
|
|||
dispose();
|
||||
|
||||
srs_freep(kbps);
|
||||
srs_freep(clk);
|
||||
srs_freep(skt);
|
||||
srs_freep(trd);
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
#include <srs_app_reload.hpp>
|
||||
#include <srs_service_conn.hpp>
|
||||
|
||||
class SrsWallClock;
|
||||
|
||||
/**
|
||||
* the basic connection of SRS,
|
||||
* all connections accept from listener must extends from this base class,
|
||||
|
@ -71,6 +73,7 @@ protected:
|
|||
* the SrsStatistic will use it indirectly to statistic the bytes delta of current connection.
|
||||
*/
|
||||
SrsKbps* kbps;
|
||||
SrsWallClock* clk;
|
||||
/**
|
||||
* the create time in milliseconds.
|
||||
* for current connection to log self create time and calculate the living time.
|
||||
|
|
|
@ -110,8 +110,7 @@ SrsClientInfo::~SrsClientInfo()
|
|||
srs_freep(res);
|
||||
}
|
||||
|
||||
SrsRtmpConn::SrsRtmpConn(SrsServer* svr, srs_netfd_t c, string cip)
|
||||
: SrsConnection(svr, c, cip)
|
||||
SrsRtmpConn::SrsRtmpConn(SrsServer* svr, srs_netfd_t c, string cip) : SrsConnection(svr, c, cip)
|
||||
{
|
||||
server = svr;
|
||||
|
||||
|
@ -120,8 +119,6 @@ SrsRtmpConn::SrsRtmpConn(SrsServer* svr, srs_netfd_t c, string cip)
|
|||
bandwidth = new SrsBandwidth();
|
||||
security = new SrsSecurity();
|
||||
duration = 0;
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
kbps->set_io(skt, skt);
|
||||
wakable = NULL;
|
||||
|
||||
mw_sleep = SRS_PERF_MW_SLEEP;
|
||||
|
@ -143,7 +140,6 @@ SrsRtmpConn::~SrsRtmpConn()
|
|||
srs_freep(refer);
|
||||
srs_freep(bandwidth);
|
||||
srs_freep(security);
|
||||
srs_freep(kbps);
|
||||
}
|
||||
|
||||
void SrsRtmpConn::dispose()
|
||||
|
|
|
@ -109,7 +109,6 @@ private:
|
|||
// for live play duration, for instance, rtmpdump to record.
|
||||
// @see https://github.com/ossrs/srs/issues/47
|
||||
int64_t duration;
|
||||
SrsKbps* kbps;
|
||||
// the MR(merged-write) sleep time in ms.
|
||||
int mw_sleep;
|
||||
// the MR(merged-write) only enabled for play.
|
||||
|
|
|
@ -49,7 +49,8 @@ SrsStatisticVhost::SrsStatisticVhost()
|
|||
{
|
||||
id = srs_generate_id();
|
||||
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
clk = new SrsWallClock();
|
||||
kbps = new SrsKbps(clk);
|
||||
kbps->set_io(NULL, NULL);
|
||||
|
||||
nb_clients = 0;
|
||||
|
@ -59,6 +60,7 @@ SrsStatisticVhost::SrsStatisticVhost()
|
|||
SrsStatisticVhost::~SrsStatisticVhost()
|
||||
{
|
||||
srs_freep(kbps);
|
||||
srs_freep(clk);
|
||||
}
|
||||
|
||||
srs_error_t SrsStatisticVhost::dumps(SrsJsonObject* obj)
|
||||
|
@ -114,7 +116,8 @@ SrsStatisticStream::SrsStatisticStream()
|
|||
width = 0;
|
||||
height = 0;
|
||||
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
clk = new SrsWallClock();
|
||||
kbps = new SrsKbps(clk);
|
||||
kbps->set_io(NULL, NULL);
|
||||
|
||||
nb_clients = 0;
|
||||
|
@ -124,6 +127,7 @@ SrsStatisticStream::SrsStatisticStream()
|
|||
SrsStatisticStream::~SrsStatisticStream()
|
||||
{
|
||||
srs_freep(kbps);
|
||||
srs_freep(clk);
|
||||
}
|
||||
|
||||
srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj)
|
||||
|
@ -236,13 +240,15 @@ SrsStatistic::SrsStatistic()
|
|||
{
|
||||
_server_id = srs_generate_id();
|
||||
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
clk = new SrsWallClock();
|
||||
kbps = new SrsKbps(clk);
|
||||
kbps->set_io(NULL, NULL);
|
||||
}
|
||||
|
||||
SrsStatistic::~SrsStatistic()
|
||||
{
|
||||
srs_freep(kbps);
|
||||
srs_freep(clk);
|
||||
|
||||
if (true) {
|
||||
std::map<int64_t, SrsStatisticVhost*>::iterator it;
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <srs_rtmp_stack.hpp>
|
||||
|
||||
class SrsKbps;
|
||||
class SrsWallClock;
|
||||
class SrsRequest;
|
||||
class SrsConnection;
|
||||
class SrsJsonObject;
|
||||
|
@ -51,6 +52,7 @@ public:
|
|||
* vhost total kbps.
|
||||
*/
|
||||
SrsKbps* kbps;
|
||||
SrsWallClock* clk;
|
||||
public:
|
||||
SrsStatisticVhost();
|
||||
virtual ~SrsStatisticVhost();
|
||||
|
@ -75,6 +77,7 @@ public:
|
|||
* stream total kbps.
|
||||
*/
|
||||
SrsKbps* kbps;
|
||||
SrsWallClock* clk;
|
||||
public:
|
||||
bool has_video;
|
||||
SrsVideoCodecId vcodec;
|
||||
|
@ -152,6 +155,7 @@ private:
|
|||
std::map<int, SrsStatisticClient*> clients;
|
||||
// server total kbps.
|
||||
SrsKbps* kbps;
|
||||
SrsWallClock* clk;
|
||||
private:
|
||||
SrsStatistic();
|
||||
virtual ~SrsStatistic();
|
||||
|
|
|
@ -31,8 +31,9 @@ SrsKbpsSample::SrsKbpsSample()
|
|||
kbps = 0;
|
||||
}
|
||||
|
||||
SrsKbpsSlice::SrsKbpsSlice()
|
||||
SrsKbpsSlice::SrsKbpsSlice(SrsWallClock* c)
|
||||
{
|
||||
clk = c;
|
||||
io.in = NULL;
|
||||
io.out = NULL;
|
||||
last_bytes = io_bytes_base = starttime = bytes = delta_bytes = 0;
|
||||
|
@ -49,7 +50,7 @@ int64_t SrsKbpsSlice::get_total_bytes()
|
|||
|
||||
void SrsKbpsSlice::sample()
|
||||
{
|
||||
int64_t now = clock->time_ms();
|
||||
int64_t now = clk->time_ms();
|
||||
int64_t total_bytes = get_total_bytes();
|
||||
|
||||
if (sample_30s.time <= 0) {
|
||||
|
@ -116,14 +117,13 @@ int64_t SrsWallClock::time_ms()
|
|||
return srs_get_system_time_ms();
|
||||
}
|
||||
|
||||
SrsKbps::SrsKbps(SrsWallClock* c)
|
||||
SrsKbps::SrsKbps(SrsWallClock* c) : is(c), os(c)
|
||||
{
|
||||
clock = c;
|
||||
clk = c;
|
||||
}
|
||||
|
||||
SrsKbps::~SrsKbps()
|
||||
{
|
||||
srs_freep(clock);
|
||||
}
|
||||
|
||||
void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
|
||||
|
@ -131,7 +131,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
|
|||
// set input stream
|
||||
// now, set start time.
|
||||
if (is.starttime == 0) {
|
||||
is.starttime = clock->time_ms();
|
||||
is.starttime = clk->time_ms();
|
||||
}
|
||||
// save the old in bytes.
|
||||
if (is.io.in) {
|
||||
|
@ -149,7 +149,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
|
|||
// set output stream
|
||||
// now, set start time.
|
||||
if (os.starttime == 0) {
|
||||
os.starttime = clock->time_ms();
|
||||
os.starttime = clk->time_ms();
|
||||
}
|
||||
// save the old in bytes.
|
||||
if (os.io.out) {
|
||||
|
@ -167,7 +167,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
|
|||
|
||||
int SrsKbps::get_send_kbps()
|
||||
{
|
||||
int64_t duration = clock->time_ms() - is.starttime;
|
||||
int64_t duration = clk->time_ms() - is.starttime;
|
||||
if (duration <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -177,7 +177,7 @@ int SrsKbps::get_send_kbps()
|
|||
|
||||
int SrsKbps::get_recv_kbps()
|
||||
{
|
||||
int64_t duration = clock->time_ms() - os.starttime;
|
||||
int64_t duration = clk->time_ms() - os.starttime;
|
||||
if (duration <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
|
||||
#include <srs_protocol_io.hpp>
|
||||
|
||||
class SrsWallClock;
|
||||
|
||||
/**
|
||||
* a kbps sample, for example, 1minute kbps,
|
||||
* 10minute kbps sample.
|
||||
|
@ -64,6 +66,7 @@ private:
|
|||
ISrsProtocolStatistic* in;
|
||||
ISrsProtocolStatistic* out;
|
||||
};
|
||||
SrsWallClock* clk;
|
||||
public:
|
||||
// the slice io used for SrsKbps to invoke,
|
||||
// the SrsKbpsSlice itself never use it.
|
||||
|
@ -88,7 +91,7 @@ public:
|
|||
// for the delta bytes.
|
||||
int64_t delta_bytes;
|
||||
public:
|
||||
SrsKbpsSlice();
|
||||
SrsKbpsSlice(SrsWallClock* clk);
|
||||
virtual ~SrsKbpsSlice();
|
||||
public:
|
||||
// Get current total bytes, not depend on sample().
|
||||
|
@ -182,7 +185,7 @@ class SrsKbps : virtual public ISrsProtocolStatistic, virtual public IKbpsDelta
|
|||
private:
|
||||
SrsKbpsSlice is;
|
||||
SrsKbpsSlice os;
|
||||
SrsWallClock* clock;
|
||||
SrsWallClock* clk;
|
||||
public:
|
||||
// We will free the clock c.
|
||||
SrsKbps(SrsWallClock* c);
|
||||
|
|
|
@ -38,7 +38,8 @@ using namespace std;
|
|||
SrsHttpClient::SrsHttpClient()
|
||||
{
|
||||
transport = NULL;
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
clk = new SrsWallClock();
|
||||
kbps = new SrsKbps(clk);
|
||||
parser = NULL;
|
||||
timeout = SRS_CONSTS_NO_TMMS;
|
||||
port = 0;
|
||||
|
@ -49,6 +50,7 @@ SrsHttpClient::~SrsHttpClient()
|
|||
disconnect();
|
||||
|
||||
srs_freep(kbps);
|
||||
srs_freep(clk);
|
||||
srs_freep(parser);
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ class SrsHttpParser;
|
|||
class ISrsHttpMessage;
|
||||
class SrsStSocket;
|
||||
class SrsKbps;
|
||||
class SrsWallClock;
|
||||
class SrsTcpClient;
|
||||
|
||||
// the default timeout for http client.
|
||||
|
@ -60,6 +61,7 @@ private:
|
|||
SrsHttpParser* parser;
|
||||
std::map<std::string, std::string> headers;
|
||||
SrsKbps* kbps;
|
||||
SrsWallClock* clk;
|
||||
private:
|
||||
// The timeout in ms.
|
||||
int64_t timeout;
|
||||
|
|
|
@ -35,7 +35,8 @@ using namespace std;
|
|||
|
||||
SrsBasicRtmpClient::SrsBasicRtmpClient(string u, int64_t ctm, int64_t stm)
|
||||
{
|
||||
kbps = new SrsKbps(new SrsWallClock());
|
||||
clk = new SrsWallClock();
|
||||
kbps = new SrsKbps(clk);
|
||||
|
||||
url = u;
|
||||
connect_timeout = ctm;
|
||||
|
@ -55,6 +56,7 @@ SrsBasicRtmpClient::~SrsBasicRtmpClient()
|
|||
{
|
||||
close();
|
||||
srs_freep(kbps);
|
||||
srs_freep(clk);
|
||||
}
|
||||
|
||||
srs_error_t SrsBasicRtmpClient::connect()
|
||||
|
|
|
@ -35,6 +35,7 @@ class SrsCommonMessage;
|
|||
class SrsSharedPtrMessage;
|
||||
class SrsPacket;
|
||||
class SrsKbps;
|
||||
class SrsWallClock;
|
||||
|
||||
/**
|
||||
* The simple RTMP client, provides friendly APIs.
|
||||
|
@ -57,6 +58,7 @@ private:
|
|||
SrsTcpClient* transport;
|
||||
SrsRtmpClient* client;
|
||||
SrsKbps* kbps;
|
||||
SrsWallClock* clk;
|
||||
int stream_id;
|
||||
public:
|
||||
// Constructor.
|
||||
|
|
|
@ -5685,8 +5685,28 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
|
|||
}
|
||||
}
|
||||
|
||||
VOID TEST(ProtocolKbpsTest, ParseHTTPMessage)
|
||||
VOID TEST(ProtocolKbpsTest, Connections)
|
||||
{
|
||||
if (true) {
|
||||
MockWallClock* clock = new MockWallClock();
|
||||
SrsAutoFree(MockWallClock, clock);
|
||||
MockStatistic* io = new MockStatistic();
|
||||
SrsAutoFree(MockStatistic, io);
|
||||
|
||||
SrsKbps* kbps = new SrsKbps(clock->set_clock(0));
|
||||
SrsAutoFree(SrsKbps, kbps);
|
||||
kbps->set_io(io, io);
|
||||
|
||||
kbps->sample();
|
||||
|
||||
EXPECT_EQ(0, kbps->get_recv_kbps());
|
||||
EXPECT_EQ(0, kbps->get_recv_kbps_30s());
|
||||
EXPECT_EQ(0, kbps->get_recv_kbps_5m());
|
||||
|
||||
EXPECT_EQ(0, kbps->get_send_kbps());
|
||||
EXPECT_EQ(0, kbps->get_send_kbps_30s());
|
||||
EXPECT_EQ(0, kbps->get_send_kbps_5m());
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <srs_rtmp_stack.hpp>
|
||||
#include <srs_rtmp_handshake.hpp>
|
||||
#include <srs_protocol_stream.hpp>
|
||||
#include <srs_protocol_kbps.hpp>
|
||||
|
||||
using namespace _srs_internal;
|
||||
|
||||
|
@ -127,13 +128,13 @@ public:
|
|||
MockStatistic* add_out(int64_t v);
|
||||
};
|
||||
|
||||
class MockWallClock
|
||||
class MockWallClock : public SrsWallClock
|
||||
{
|
||||
private:
|
||||
int64_t clock;
|
||||
public:
|
||||
MockWallClock();
|
||||
virtual MockWallClock();
|
||||
virtual ~MockWallClock();
|
||||
public:
|
||||
virtual int64_t time_ms();
|
||||
public:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue