Refine SrsWallClock and SrsKbps in time unit.

pull/1651/head
winlin 6 years ago
parent 5560b529a7
commit cfab73b552

@ -33,11 +33,11 @@ typedef int64_t srs_utime_t;
#define SRS_UTIME_MILLISECONDS 1000
// Convert srs_utime_t as ms.
#define srsu2ms(us) (us / SRS_UTIME_MILLISECONDS)
#define srsu2msi(us) int(us / SRS_UTIME_MILLISECONDS)
#define srsu2ms(us) ((us) / SRS_UTIME_MILLISECONDS)
#define srsu2msi(us) int((us) / SRS_UTIME_MILLISECONDS)
// The time unit in ms, for example 120 * SRS_UTIME_SECONDS means 120s.
#define SRS_UTIME_SECONDS 1000000
#define SRS_UTIME_SECONDS 1000000LL
// The time unit in minutes, for example 3 * SRS_UTIME_MINUTES means 3m.
#define SRS_UTIME_MINUTES 60000000LL

@ -35,7 +35,7 @@ SrsKbpsSample::~SrsKbpsSample()
{
}
SrsKbpsSample* SrsKbpsSample::update(int64_t b, int64_t t, int k)
SrsKbpsSample* SrsKbpsSample::update(int64_t b, srs_utime_t t, int k)
{
bytes = b;
time = t;
@ -61,7 +61,7 @@ int64_t SrsKbpsSlice::get_total_bytes()
void SrsKbpsSlice::sample()
{
int64_t now = clk->time_ms();
srs_utime_t now = clk->now();
int64_t total_bytes = get_total_bytes();
if (sample_30s.time < 0) {
@ -77,20 +77,20 @@ void SrsKbpsSlice::sample()
sample_60m.update(total_bytes, now, 0);
}
if (now - sample_30s.time >= 30 * 1000) {
int kbps = (int)((total_bytes - sample_30s.bytes) * 8 / (now - sample_30s.time));
if (now - sample_30s.time >= 30 * SRS_UTIME_SECONDS) {
int kbps = (int)((total_bytes - sample_30s.bytes) * 8 / srsu2ms(now - sample_30s.time));
sample_30s.update(total_bytes, now, kbps);
}
if (now - sample_1m.time >= 60 * 1000) {
int kbps = (int)((total_bytes - sample_1m.bytes) * 8 / (now - sample_1m.time));
if (now - sample_1m.time >= 60 * SRS_UTIME_SECONDS) {
int kbps = (int)((total_bytes - sample_1m.bytes) * 8 / srsu2ms(now - sample_1m.time));
sample_1m.update(total_bytes, now, kbps);
}
if (now - sample_5m.time >= 300 * 1000) {
int kbps = (int)((total_bytes - sample_5m.bytes) * 8 / (now - sample_5m.time));
if (now - sample_5m.time >= 300 * SRS_UTIME_SECONDS) {
int kbps = (int)((total_bytes - sample_5m.bytes) * 8 / srsu2ms(now - sample_5m.time));
sample_5m.update(total_bytes, now, kbps);
}
if (now - sample_60m.time >= 3600 * 1000) {
int kbps = (int)((total_bytes - sample_60m.bytes) * 8 / (now - sample_60m.time));
if (now - sample_60m.time >= 3600 * SRS_UTIME_SECONDS) {
int kbps = (int)((total_bytes - sample_60m.bytes) * 8 / srsu2ms(now - sample_60m.time));
sample_60m.update(total_bytes, now, kbps);
}
}
@ -111,9 +111,9 @@ SrsWallClock::~SrsWallClock()
{
}
int64_t SrsWallClock::time_ms()
srs_utime_t SrsWallClock::now()
{
return srsu2ms(srs_get_system_time());
return srs_get_system_time();
}
SrsKbps::SrsKbps(SrsWallClock* c) : is(c), os(c)
@ -130,7 +130,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
// set input stream
// now, set start time.
if (is.starttime == 0) {
is.starttime = clk->time_ms();
is.starttime = clk->now();
}
// save the old in bytes.
if (is.io) {
@ -148,7 +148,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
// set output stream
// now, set start time.
if (os.starttime == 0) {
os.starttime = clk->time_ms();
os.starttime = clk->now();
}
// save the old in bytes.
if (os.io) {
@ -166,22 +166,22 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
int SrsKbps::get_send_kbps()
{
int64_t duration = clk->time_ms() - is.starttime;
srs_utime_t duration = clk->now() - is.starttime;
if (duration <= 0) {
return 0;
}
int64_t bytes = get_send_bytes();
return (int)(bytes * 8 / duration);
return (int)(bytes * 8 / srsu2ms(duration));
}
int SrsKbps::get_recv_kbps()
{
int64_t duration = clk->time_ms() - os.starttime;
srs_utime_t duration = clk->now() - os.starttime;
if (duration <= 0) {
return 0;
}
int64_t bytes = get_recv_bytes();
return (int)(bytes * 8 / duration);
return (int)(bytes * 8 / srsu2ms(duration));
}
int SrsKbps::get_send_kbps_30s()

@ -38,13 +38,13 @@ class SrsKbpsSample
{
public:
int64_t bytes;
int64_t time;
srs_utime_t time;
int kbps;
public:
SrsKbpsSample();
virtual ~SrsKbpsSample();
public:
virtual SrsKbpsSample* update(int64_t b, int64_t t, int k);
virtual SrsKbpsSample* update(int64_t b, srs_utime_t t, int k);
};
/**
@ -74,7 +74,7 @@ public:
// @remark, use total_bytes() to get the total bytes of slice.
int64_t bytes;
// slice starttime, the first time to record bytes.
int64_t starttime;
srs_utime_t starttime;
// session startup bytes number for io when set it,
// the base offset of bytes for io.
int64_t io_bytes_base;
@ -129,9 +129,9 @@ public:
virtual ~SrsWallClock();
public:
/**
* Current time in ms.
* Current time in srs_utime_t.
*/
virtual int64_t time_ms();
virtual srs_utime_t now();
};
/**

@ -264,14 +264,14 @@ MockWallClock::~MockWallClock()
{
}
int64_t MockWallClock::time_ms()
srs_utime_t MockWallClock::now()
{
return clock;
}
MockWallClock* MockWallClock::set_clock(int64_t ms)
MockWallClock* MockWallClock::set_clock(srs_utime_t v)
{
clock = ms;
clock = v;
return this;
}
@ -5707,7 +5707,7 @@ VOID TEST(ProtocolKbpsTest, Connections)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 30s.
clock->set_clock(30 * 1000);
clock->set_clock(30 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_in(30 * 100 * 1000)->set_out(30 * 100 * 1000);
kbps->sample();
@ -5720,7 +5720,7 @@ VOID TEST(ProtocolKbpsTest, Connections)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 300s.
clock->set_clock(330 * 1000);
clock->set_clock(330 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_in(330 * 100 * 1000)->set_out(330 * 100 * 1000);
kbps->sample();
@ -5755,7 +5755,7 @@ VOID TEST(ProtocolKbpsTest, Connections)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 30s.
clock->set_clock(30 * 1000);
clock->set_clock(30 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_in(30 * 100 * 1000);
kbps->sample();
@ -5768,7 +5768,7 @@ VOID TEST(ProtocolKbpsTest, Connections)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 300s.
clock->set_clock(330 * 1000);
clock->set_clock(330 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_in(330 * 100 * 1000);
kbps->sample();
@ -5803,7 +5803,7 @@ VOID TEST(ProtocolKbpsTest, Connections)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 30s.
clock->set_clock(30 * 1000);
clock->set_clock(30 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_out(30 * 100 * 1000);
kbps->sample();
@ -5816,7 +5816,7 @@ VOID TEST(ProtocolKbpsTest, Connections)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 300s.
clock->set_clock(330 * 1000);
clock->set_clock(330 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_out(330 * 100 * 1000);
kbps->sample();
@ -5901,7 +5901,7 @@ VOID TEST(ProtocolKbpsTest, Delta)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 30s.
clock->set_clock(30 * 1000);
clock->set_clock(30 * 1000 * SRS_UTIME_MILLISECONDS);
kbps->add_delta(30 * in, 30 * out);
kbps->sample();
@ -5943,7 +5943,7 @@ VOID TEST(ProtocolKbpsTest, RAWStatistic)
EXPECT_EQ(0, kbps->get_send_kbps_5m());
// 800kbps in 30s.
clock->set_clock(30 * 1000);
clock->set_clock(30 * 1000 * SRS_UTIME_MILLISECONDS);
io->set_out(30 * 100 * 1000);
kbps->sample();

@ -136,9 +136,9 @@ public:
MockWallClock();
virtual ~MockWallClock();
public:
virtual int64_t time_ms();
virtual srs_utime_t now();
public:
virtual MockWallClock* set_clock(int64_t ms);
virtual MockWallClock* set_clock(srs_utime_t v);
};
#endif

Loading…
Cancel
Save