|
HOME
SIP/media Features
High Performance SIP
Small Footprint SIP
Symbian Port
FAQ
Documentation
Licensing
Download
Development (Trac)
Projects using pjsip
Mailing List
Open Source Links
About: PJLIB, PJLIB-UTIL, PJSIP, and PJMEDIA are created by: Benny Prijono <bennylp pjsip.org>
|
|
Home --> Documentations --> PJLIB Reference
This file provides implementation of timestamp_test()
This tests whether timestamp API works.
API tested:
This file is pjlib-test/timestamp.c
#include "test.h"
#include <pj/os.h>
#include <pj/log.h>
#include <pj/rand.h>
#if INCLUDE_TIMESTAMP_TEST
#define THIS_FILE "timestamp"
static int timestamp_accuracy()
{
pj_timestamp freq, t1, t2;
pj_time_val tv1, tv2, tvtmp;
pj_int64_t msec, tics;
pj_int64_t diff;
PJ_LOG(3,(THIS_FILE, "...testing frequency accuracy (pls wait)"));
pj_get_timestamp_freq(&freq);
pj_gettimeofday(&tvtmp);
do {
pj_gettimeofday(&tv1);
} while (PJ_TIME_VAL_EQ(tvtmp, tv1));
pj_get_timestamp(&t1);
pj_thread_sleep(10000);
pj_gettimeofday(&tvtmp);
do {
pj_gettimeofday(&tv2);
} while (PJ_TIME_VAL_EQ(tvtmp, tv2));
pj_get_timestamp(&t2);
PJ_TIME_VAL_SUB(tv2, tv1);
msec = PJ_TIME_VAL_MSEC(tv2);
tics = t2.u64 - t1.u64;
diff = tics - (msec * freq.u64 / 1000);
if (diff < 0)
diff = -diff;
if (diff > (pj_int64_t)(freq.u64 / 1000)) {
PJ_LOG(3,(THIS_FILE, "....error: timestamp drifted by %d usec after "
"%d msec",
(pj_uint32_t)(diff * 1000000 / freq.u64),
msec));
return -2000;
} else if (diff > (pj_int64_t)(freq.u64 / 1000000)) {
PJ_LOG(3,(THIS_FILE, "....warning: timestamp drifted by %d usec after "
"%d msec",
(pj_uint32_t)(diff * 1000000 / freq.u64),
msec));
} else {
PJ_LOG(3,(THIS_FILE, "....good. Timestamp is accurate down to"
" nearest usec."));
}
return 0;
}
int timestamp_test(void)
{
enum { CONSECUTIVE_LOOP = 100 };
volatile unsigned i;
pj_timestamp freq, t1, t2;
pj_time_val tv1, tv2;
unsigned elapsed;
pj_status_t rc;
PJ_LOG(3,(THIS_FILE, "...Testing timestamp (high res time)"));
if ((rc=pj_get_timestamp_freq(&freq)) != PJ_SUCCESS) {
app_perror("...ERROR: get timestamp freq", rc);
return -1000;
}
PJ_LOG(3,(THIS_FILE, "....frequency: hiword=%lu loword=%lu",
freq.u32.hi, freq.u32.lo));
PJ_LOG(3,(THIS_FILE, "...checking if time can run backwards (pls wait).."));
rc = pj_get_timestamp(&t1);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_get_timestamp", rc);
return -1001;
}
rc = pj_gettimeofday(&tv1);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_gettimeofday", rc);
return -1002;
}
for (i=0; i<CONSECUTIVE_LOOP; ++i) {
pj_thread_sleep(pj_rand() % 100);
rc = pj_get_timestamp(&t2);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_get_timestamp", rc);
return -1003;
}
rc = pj_gettimeofday(&tv2);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_gettimeofday", rc);
return -1004;
}
if (t2.u32.hi < t1.u32.hi ||
(t2.u32.hi == t1.u32.hi && t2.u32.lo < t1.u32.lo))
{
PJ_LOG(3,(THIS_FILE, "...ERROR: timestamp run backwards!"));
return -1005;
}
if (PJ_TIME_VAL_LT(tv2, tv1)) {
PJ_LOG(3,(THIS_FILE, "...ERROR: time run backwards!"));
return -1006;
}
}
PJ_LOG(3,(THIS_FILE, "....testing simple 1000000 loop"));
if ((rc=pj_get_timestamp(&t1)) != PJ_SUCCESS) {
app_perror("....error: cat't get timestamp", rc);
return -1010;
}
for (i=0; i<1000000; ++i) {
null_func();
}
pj_thread_sleep(0);
pj_get_timestamp(&t2);
elapsed = pj_elapsed_usec(&t1, &t2);
PJ_LOG(3,(THIS_FILE, "....elapsed: %u usec", (unsigned)elapsed));
if (elapsed < 1 || elapsed > 1000000) {
PJ_LOG(3,(THIS_FILE, "....error: elapsed time outside window (%u, "
"t1.u32.hi=%u, t1.u32.lo=%u, "
"t2.u32.hi=%u, t2.u32.lo=%u)",
elapsed,
t1.u32.hi, t1.u32.lo, t2.u32.hi, t2.u32.lo));
return -1030;
}
rc = timestamp_accuracy();
if (rc != 0)
return rc;
return 0;
}
#else
int dummy_timestamp_test;
#endif
PJLIB Open Source, high performance, small footprint, and very very portable framework
(C)2001-2008 Benny Prijono
|
|