From 4c40370986e20e3c14e7d3e2599c68f29b5a2859 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 23 Dec 2018 20:30:12 +0800 Subject: [PATCH] Refine kbps more testable --- trunk/src/app/srs_app_conn.cpp | 2 +- trunk/src/app/srs_app_rtmp_conn.cpp | 2 +- trunk/src/app/srs_app_statistic.cpp | 6 +- trunk/src/protocol/srs_protocol_kbps.cpp | 27 ++++++-- trunk/src/protocol/srs_protocol_kbps.hpp | 19 +++++- trunk/src/service/srs_service_http_client.cpp | 2 +- trunk/src/service/srs_service_rtmp_conn.cpp | 2 +- trunk/src/utest/srs_utest_protocol.cpp | 67 +++++++++++++++++++ trunk/src/utest/srs_utest_protocol.hpp | 31 +++++++++ 9 files changed, 144 insertions(+), 14 deletions(-) diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 597a851ac..21ada2fc3 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -39,7 +39,7 @@ SrsConnection::SrsConnection(IConnectionManager* cm, srs_netfd_t c, string cip) create_time = srs_get_system_time_ms(); skt = new SrsStSocket(); - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); kbps->set_io(skt, skt); trd = new SrsSTCoroutine("conn", this); diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index cfdc6da76..e69fcc60c 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -120,7 +120,7 @@ SrsRtmpConn::SrsRtmpConn(SrsServer* svr, srs_netfd_t c, string cip) bandwidth = new SrsBandwidth(); security = new SrsSecurity(); duration = 0; - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); kbps->set_io(skt, skt); wakable = NULL; diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index 37ee18204..342e43f52 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -49,7 +49,7 @@ SrsStatisticVhost::SrsStatisticVhost() { id = srs_generate_id(); - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); kbps->set_io(NULL, NULL); nb_clients = 0; @@ -114,7 +114,7 @@ SrsStatisticStream::SrsStatisticStream() width = 0; height = 0; - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); kbps->set_io(NULL, NULL); nb_clients = 0; @@ -236,7 +236,7 @@ SrsStatistic::SrsStatistic() { _server_id = srs_generate_id(); - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); kbps->set_io(NULL, NULL); } diff --git a/trunk/src/protocol/srs_protocol_kbps.cpp b/trunk/src/protocol/srs_protocol_kbps.cpp index 88a8daecb..a5e354fcc 100644 --- a/trunk/src/protocol/srs_protocol_kbps.cpp +++ b/trunk/src/protocol/srs_protocol_kbps.cpp @@ -49,7 +49,7 @@ int64_t SrsKbpsSlice::get_total_bytes() void SrsKbpsSlice::sample() { - int64_t now = srs_get_system_time_ms(); + int64_t now = clock->time_ms(); int64_t total_bytes = get_total_bytes(); 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() { + srs_freep(clock); } void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) @@ -116,7 +131,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) // set input stream // now, set start time. if (is.starttime == 0) { - is.starttime = srs_get_system_time_ms(); + is.starttime = clock->time_ms(); } // save the old in bytes. if (is.io.in) { @@ -134,7 +149,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) // set output stream // now, set start time. if (os.starttime == 0) { - os.starttime = srs_get_system_time_ms(); + os.starttime = clock->time_ms(); } // save the old in bytes. if (os.io.out) { @@ -152,7 +167,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) 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) { return 0; } @@ -162,7 +177,7 @@ int SrsKbps::get_send_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) { return 0; } diff --git a/trunk/src/protocol/srs_protocol_kbps.hpp b/trunk/src/protocol/srs_protocol_kbps.hpp index 0de8bbb77..bef4fffb2 100644 --- a/trunk/src/protocol/srs_protocol_kbps.hpp +++ b/trunk/src/protocol/srs_protocol_kbps.hpp @@ -126,6 +126,21 @@ public: 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. * 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: SrsKbpsSlice is; SrsKbpsSlice os; + SrsWallClock* clock; public: - SrsKbps(); + // We will free the clock c. + SrsKbps(SrsWallClock* c); virtual ~SrsKbps(); public: /** diff --git a/trunk/src/service/srs_service_http_client.cpp b/trunk/src/service/srs_service_http_client.cpp index 759b8b8b8..95b924e82 100644 --- a/trunk/src/service/srs_service_http_client.cpp +++ b/trunk/src/service/srs_service_http_client.cpp @@ -38,7 +38,7 @@ using namespace std; SrsHttpClient::SrsHttpClient() { transport = NULL; - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); parser = NULL; timeout = SRS_CONSTS_NO_TMMS; port = 0; diff --git a/trunk/src/service/srs_service_rtmp_conn.cpp b/trunk/src/service/srs_service_rtmp_conn.cpp index 266a21c91..e6253c93d 100644 --- a/trunk/src/service/srs_service_rtmp_conn.cpp +++ b/trunk/src/service/srs_service_rtmp_conn.cpp @@ -35,7 +35,7 @@ using namespace std; SrsBasicRtmpClient::SrsBasicRtmpClient(string u, int64_t ctm, int64_t stm) { - kbps = new SrsKbps(); + kbps = new SrsKbps(new SrsWallClock()); url = u; connect_timeout = ctm; diff --git a/trunk/src/utest/srs_utest_protocol.cpp b/trunk/src/utest/srs_utest_protocol.cpp index f0ce52eb6..f555c3710 100644 --- a/trunk/src/utest/srs_utest_protocol.cpp +++ b/trunk/src/utest/srs_utest_protocol.cpp @@ -212,6 +212,69 @@ srs_error_t MockBufferIO::read(void* buf, size_t size, ssize_t* nread) 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 // verify the sha256 @@ -5622,5 +5685,9 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage) } } +VOID TEST(ProtocolKbpsTest, ParseHTTPMessage) +{ +} + #endif diff --git a/trunk/src/utest/srs_utest_protocol.hpp b/trunk/src/utest/srs_utest_protocol.hpp index e4cebd93a..166ed1a58 100644 --- a/trunk/src/utest/srs_utest_protocol.hpp +++ b/trunk/src/utest/srs_utest_protocol.hpp @@ -109,5 +109,36 @@ public: 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