diff options
author | Benny Prijono <bennylp@teluu.com> | 2005-11-02 17:38:33 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2005-11-02 17:38:33 +0000 |
commit | 58aee2809c36f43a3b66dac7d9db5d13070114b9 (patch) | |
tree | 77865769e330f1ff836bbea80506b64c5a1c5046 /pjlib/src/pjlib-test/udp_echo_srv_sync.c | |
parent | ae4b852ee91f45558c1e00d280cabc7bc8232437 (diff) |
Use delay based bandwidth calculation.
git-svn-id: http://svn.pjsip.org/repos/pjproject/main@10 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib/src/pjlib-test/udp_echo_srv_sync.c')
-rw-r--r-- | pjlib/src/pjlib-test/udp_echo_srv_sync.c | 116 |
1 files changed, 46 insertions, 70 deletions
diff --git a/pjlib/src/pjlib-test/udp_echo_srv_sync.c b/pjlib/src/pjlib-test/udp_echo_srv_sync.c index 9d534aa0..0e73b134 100644 --- a/pjlib/src/pjlib-test/udp_echo_srv_sync.c +++ b/pjlib/src/pjlib-test/udp_echo_srv_sync.c @@ -3,26 +3,16 @@ #include "test.h" #include <pjlib.h> -static pj_sem_t *sem; -static pj_mutex_t *mutex; -static pj_size_t total_bw; +static pj_atomic_t *total_bytes;
static int worker_thread(void *arg) { pj_sock_t sock = (pj_sock_t)arg; char buf[1516]; - pj_size_t received; - pj_time_val last_print; pj_status_t last_recv_err = PJ_SUCCESS, last_write_err = PJ_SUCCESS; - received = 0; - pj_gettimeofday(&last_print); - for (;;) { pj_ssize_t len; - pj_uint32_t delay_msec; - pj_time_val now; - pj_highprec_t bw; pj_status_t rc; pj_sockaddr_in addr; int addrlen; @@ -38,7 +28,7 @@ static int worker_thread(void *arg) continue; } - received += len; + pj_atomic_add(total_bytes, len); rc = pj_sock_sendto(sock, buf, &len, 0, &addr, addrlen); if (rc != PJ_SUCCESS) { @@ -48,26 +38,6 @@ static int worker_thread(void *arg) } continue; } - - pj_gettimeofday(&now); - PJ_TIME_VAL_SUB(now, last_print); - delay_msec = PJ_TIME_VAL_MSEC(now); - - if (delay_msec < 1000) - continue; - - bw = received; - pj_highprec_mul(bw, 1000); - pj_highprec_div(bw, delay_msec); - - pj_mutex_lock(mutex); - total_bw = total_bw + (pj_size_t)bw; - pj_mutex_unlock(mutex); - - pj_gettimeofday(&last_print); - received = 0; - pj_sem_post(sem); - pj_thread_sleep(0); } } @@ -77,27 +47,22 @@ int echo_srv_sync(void) pj_pool_t *pool; pj_sock_t sock; pj_thread_t *thread[ECHO_SERVER_MAX_THREADS]; - pj_status_t rc; - pj_highprec_t abs_total; - unsigned count; + pj_status_t rc;
+ pj_highprec_t last_received, avg_bw, highest_bw;
+ pj_time_val last_print;
+ unsigned count;
int i; pool = pj_pool_create(mem, NULL, 4000, 4000, NULL); if (!pool) return -5; - rc = pj_sem_create(pool, NULL, 0, ECHO_SERVER_MAX_THREADS, &sem); + rc = pj_atomic_create(pool, 0, &total_bytes); if (rc != PJ_SUCCESS) { - app_perror("...unable to create semaphore", rc); + app_perror("...unable to create atomic_var", rc); return -6; } - rc = pj_mutex_create_simple(pool, NULL, &mutex); - if (rc != PJ_SUCCESS) { - app_perror("...unable to create mutex", rc); - return -7; - } - rc = app_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, ECHO_SERVER_START_PORT, &sock); if (rc != PJ_SUCCESS) { app_perror("...socket error", rc); @@ -118,41 +83,52 @@ int echo_srv_sync(void) ECHO_SERVER_MAX_THREADS, ECHO_SERVER_START_PORT)); PJ_LOG(3,("", "...Press Ctrl-C to abort")); - abs_total = 0; + last_received = 0;
+ pj_gettimeofday(&last_print);
+ avg_bw = highest_bw = 0;
count = 0; - for (;;) { - pj_uint32_t avg32; - pj_highprec_t avg; - - for (i=0; i<ECHO_SERVER_MAX_THREADS; ++i) - pj_sem_wait(sem); - - /* calculate average so far: - avg = abs_total / count; - */ - count++; - abs_total += total_bw; - avg = abs_total; - pj_highprec_div(avg, count); - avg32 = (pj_uint32_t)avg; - - + for (;;) {
+ pj_highprec_t received, cur_received, bw; + unsigned msec;
+ pj_time_val now, duration;
+
+ pj_thread_sleep(1000);
+ + received = cur_received = pj_atomic_get(total_bytes); + cur_received = cur_received - last_received;
+ + pj_gettimeofday(&now);
+ duration = now;
+ PJ_TIME_VAL_SUB(duration, last_print);
+ msec = PJ_TIME_VAL_MSEC(duration); +
+ bw = cur_received;
+ pj_highprec_mul(bw, 1000);
+ pj_highprec_div(bw, msec);
+
+ last_print = now;
+ last_received = received;
+
+ avg_bw = avg_bw + bw;
+ count++;
+
PJ_LOG(3,("", "Synchronous UDP (%d threads): %u KB/s (avg=%u KB/s) %s", ECHO_SERVER_MAX_THREADS, - total_bw / 1000, - avg32 / 1000, + (unsigned)(bw / 1000), + (unsigned)(avg_bw / count / 1000), (count==20 ? "<ses avg>" : ""))); - total_bw = 0; - if (count==20) { - count = 0; - abs_total = 0; + if (avg_bw/count > highest_bw)
+ highest_bw = avg_bw/count;
+
+ count = 0;
+ avg_bw = 0;
+
+ PJ_LOG(3,("", "Highest average bandwidth=%u KB/s",
+ (unsigned)(highest_bw/1000))); } - - while (pj_sem_trywait(sem) == PJ_SUCCESS) - ; } } |