1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Refine kbps more testable

This commit is contained in:
winlin 2018-12-23 20:30:12 +08:00
parent 16fa4c588a
commit 4c40370986
9 changed files with 144 additions and 14 deletions

View file

@ -39,7 +39,7 @@ SrsConnection::SrsConnection(IConnectionManager* cm, srs_netfd_t c, string cip)
create_time = srs_get_system_time_ms(); create_time = srs_get_system_time_ms();
skt = new SrsStSocket(); skt = new SrsStSocket();
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
kbps->set_io(skt, skt); kbps->set_io(skt, skt);
trd = new SrsSTCoroutine("conn", this); trd = new SrsSTCoroutine("conn", this);

View file

@ -120,7 +120,7 @@ SrsRtmpConn::SrsRtmpConn(SrsServer* svr, srs_netfd_t c, string cip)
bandwidth = new SrsBandwidth(); bandwidth = new SrsBandwidth();
security = new SrsSecurity(); security = new SrsSecurity();
duration = 0; duration = 0;
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
kbps->set_io(skt, skt); kbps->set_io(skt, skt);
wakable = NULL; wakable = NULL;

View file

@ -49,7 +49,7 @@ SrsStatisticVhost::SrsStatisticVhost()
{ {
id = srs_generate_id(); id = srs_generate_id();
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
kbps->set_io(NULL, NULL); kbps->set_io(NULL, NULL);
nb_clients = 0; nb_clients = 0;
@ -114,7 +114,7 @@ SrsStatisticStream::SrsStatisticStream()
width = 0; width = 0;
height = 0; height = 0;
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
kbps->set_io(NULL, NULL); kbps->set_io(NULL, NULL);
nb_clients = 0; nb_clients = 0;
@ -236,7 +236,7 @@ SrsStatistic::SrsStatistic()
{ {
_server_id = srs_generate_id(); _server_id = srs_generate_id();
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
kbps->set_io(NULL, NULL); kbps->set_io(NULL, NULL);
} }

View file

@ -49,7 +49,7 @@ int64_t SrsKbpsSlice::get_total_bytes()
void SrsKbpsSlice::sample() void SrsKbpsSlice::sample()
{ {
int64_t now = srs_get_system_time_ms(); int64_t now = clock->time_ms();
int64_t total_bytes = get_total_bytes(); int64_t total_bytes = get_total_bytes();
if (sample_30s.time <= 0) { if (sample_30s.time <= 0) {
@ -103,12 +103,27 @@ IKbpsDelta::~IKbpsDelta()
{ {
} }
SrsKbps::SrsKbps() SrsWallClock::SrsWallClock()
{ {
} }
SrsWallClock::~SrsWallClock()
{
}
int64_t SrsWallClock::time_ms()
{
return srs_get_system_time_ms();
}
SrsKbps::SrsKbps(SrsWallClock* c)
{
clock = c;
}
SrsKbps::~SrsKbps() SrsKbps::~SrsKbps()
{ {
srs_freep(clock);
} }
void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
@ -116,7 +131,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
// set input stream // set input stream
// now, set start time. // now, set start time.
if (is.starttime == 0) { if (is.starttime == 0) {
is.starttime = srs_get_system_time_ms(); is.starttime = clock->time_ms();
} }
// save the old in bytes. // save the old in bytes.
if (is.io.in) { if (is.io.in) {
@ -134,7 +149,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
// set output stream // set output stream
// now, set start time. // now, set start time.
if (os.starttime == 0) { if (os.starttime == 0) {
os.starttime = srs_get_system_time_ms(); os.starttime = clock->time_ms();
} }
// save the old in bytes. // save the old in bytes.
if (os.io.out) { if (os.io.out) {
@ -152,7 +167,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
int SrsKbps::get_send_kbps() int SrsKbps::get_send_kbps()
{ {
int64_t duration = srs_get_system_time_ms() - is.starttime; int64_t duration = clock->time_ms() - is.starttime;
if (duration <= 0) { if (duration <= 0) {
return 0; return 0;
} }
@ -162,7 +177,7 @@ int SrsKbps::get_send_kbps()
int SrsKbps::get_recv_kbps() int SrsKbps::get_recv_kbps()
{ {
int64_t duration = srs_get_system_time_ms() - os.starttime; int64_t duration = clock->time_ms() - os.starttime;
if (duration <= 0) { if (duration <= 0) {
return 0; return 0;
} }

View file

@ -126,6 +126,21 @@ public:
virtual void cleanup() = 0; virtual void cleanup() = 0;
}; };
/**
* A time source to provide wall clock.
*/
class SrsWallClock
{
public:
SrsWallClock();
virtual ~SrsWallClock();
public:
/**
* Current time in ms.
*/
virtual int64_t time_ms();
};
/** /**
* to statistic the kbps of io. * to statistic the kbps of io.
* itself can be a statistic source, for example, used for SRS bytes stat. * itself can be a statistic source, for example, used for SRS bytes stat.
@ -167,8 +182,10 @@ class SrsKbps : virtual public ISrsProtocolStatistic, virtual public IKbpsDelta
private: private:
SrsKbpsSlice is; SrsKbpsSlice is;
SrsKbpsSlice os; SrsKbpsSlice os;
SrsWallClock* clock;
public: public:
SrsKbps(); // We will free the clock c.
SrsKbps(SrsWallClock* c);
virtual ~SrsKbps(); virtual ~SrsKbps();
public: public:
/** /**

View file

@ -38,7 +38,7 @@ using namespace std;
SrsHttpClient::SrsHttpClient() SrsHttpClient::SrsHttpClient()
{ {
transport = NULL; transport = NULL;
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
parser = NULL; parser = NULL;
timeout = SRS_CONSTS_NO_TMMS; timeout = SRS_CONSTS_NO_TMMS;
port = 0; port = 0;

View file

@ -35,7 +35,7 @@ using namespace std;
SrsBasicRtmpClient::SrsBasicRtmpClient(string u, int64_t ctm, int64_t stm) SrsBasicRtmpClient::SrsBasicRtmpClient(string u, int64_t ctm, int64_t stm)
{ {
kbps = new SrsKbps(); kbps = new SrsKbps(new SrsWallClock());
url = u; url = u;
connect_timeout = ctm; connect_timeout = ctm;

View file

@ -212,6 +212,69 @@ srs_error_t MockBufferIO::read(void* buf, size_t size, ssize_t* nread)
return srs_success; return srs_success;
} }
MockStatistic::MockStatistic()
{
in = out = 0;
}
MockStatistic::~MockStatistic()
{
}
int64_t MockStatistic::get_recv_bytes()
{
return in;
}
int64_t MockStatistic::get_send_bytes()
{
return out;
}
MockStatistic* MockStatistic::set_in(int64_t v)
{
in = v;
return this;
}
MockStatistic* MockStatistic::set_out(int64_t v)
{
out = v;
return this;
}
MockStatistic* MockStatistic::add_in(int64_t v)
{
in += v;
return this;
}
MockStatistic* MockStatistic::add_out(int64_t v)
{
out += v;
return this;
}
MockWallClock::MockWallClock()
{
clock = 0;
}
MockWallClock::~MockWallClock()
{
}
int64_t MockWallClock::time_ms()
{
return clock;
}
MockWallClock* MockWallClock::set_clock(int64_t ms)
{
clock = ms;
return this;
}
#ifdef ENABLE_UTEST_PROTOCOL #ifdef ENABLE_UTEST_PROTOCOL
// verify the sha256 // verify the sha256
@ -5622,5 +5685,9 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
} }
} }
VOID TEST(ProtocolKbpsTest, ParseHTTPMessage)
{
}
#endif #endif

View file

@ -109,5 +109,36 @@ public:
virtual srs_error_t read(void* buf, size_t size, ssize_t* nread); virtual srs_error_t read(void* buf, size_t size, ssize_t* nread);
}; };
class MockStatistic : public ISrsProtocolStatistic
{
private:
int64_t in;
int64_t out;
public:
MockStatistic();
virtual ~MockStatistic();
public:
virtual int64_t get_recv_bytes();
virtual int64_t get_send_bytes();
public:
MockStatistic* set_in(int64_t v);
MockStatistic* set_out(int64_t v);
MockStatistic* add_in(int64_t v);
MockStatistic* add_out(int64_t v);
};
class MockWallClock
{
private:
int64_t clock;
public:
MockWallClock();
virtual MockWallClock();
public:
virtual int64_t time_ms();
public:
virtual MockWallClock* set_clock(int64_t ms);
};
#endif #endif