Ignore:
Timestamp:
Nov 2, 2005 5:38:33 PM (18 years ago)
Author:
bennylp
Message:

Use delay based bandwidth calculation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/main/pjlib/src/pjlib-test/udp_echo_srv_sync.c

    r6 r10  
    44#include <pjlib.h> 
    55 
    6 static pj_sem_t    *sem; 
    7 static pj_mutex_t  *mutex; 
    8 static pj_size_t    total_bw; 
     6static pj_atomic_t *total_bytes; 
    97 
    108static int worker_thread(void *arg) 
     
    1210    pj_sock_t    sock = (pj_sock_t)arg; 
    1311    char         buf[1516]; 
    14     pj_size_t    received; 
    15     pj_time_val  last_print; 
    1612    pj_status_t  last_recv_err = PJ_SUCCESS, last_write_err = PJ_SUCCESS; 
    17  
    18     received = 0; 
    19     pj_gettimeofday(&last_print); 
    2013 
    2114    for (;;) { 
    2215        pj_ssize_t len; 
    23         pj_uint32_t delay_msec; 
    24         pj_time_val now; 
    25         pj_highprec_t bw; 
    2616        pj_status_t rc; 
    2717        pj_sockaddr_in addr; 
     
    3929        } 
    4030 
    41         received += len; 
     31        pj_atomic_add(total_bytes, len); 
    4232 
    4333        rc = pj_sock_sendto(sock, buf, &len, 0, &addr, addrlen); 
     
    4939            continue; 
    5040        } 
    51  
    52         pj_gettimeofday(&now); 
    53         PJ_TIME_VAL_SUB(now, last_print); 
    54         delay_msec = PJ_TIME_VAL_MSEC(now); 
    55  
    56         if (delay_msec < 1000) 
    57             continue; 
    58   
    59         bw = received; 
    60         pj_highprec_mul(bw, 1000); 
    61         pj_highprec_div(bw, delay_msec); 
    62  
    63         pj_mutex_lock(mutex); 
    64         total_bw = total_bw + (pj_size_t)bw; 
    65         pj_mutex_unlock(mutex); 
    66  
    67         pj_gettimeofday(&last_print); 
    68         received = 0; 
    69         pj_sem_post(sem); 
    70         pj_thread_sleep(0); 
    7141    } 
    7242} 
     
    7949    pj_thread_t *thread[ECHO_SERVER_MAX_THREADS]; 
    8050    pj_status_t rc; 
    81     pj_highprec_t abs_total; 
     51    pj_highprec_t last_received, avg_bw, highest_bw; 
     52    pj_time_val last_print; 
    8253    unsigned count; 
    8354    int i; 
     
    8758        return -5; 
    8859 
    89     rc = pj_sem_create(pool, NULL, 0, ECHO_SERVER_MAX_THREADS, &sem); 
     60    rc = pj_atomic_create(pool, 0, &total_bytes); 
    9061    if (rc != PJ_SUCCESS) { 
    91         app_perror("...unable to create semaphore", rc); 
     62        app_perror("...unable to create atomic_var", rc); 
    9263        return -6; 
    93     } 
    94  
    95     rc = pj_mutex_create_simple(pool, NULL, &mutex); 
    96     if (rc != PJ_SUCCESS) { 
    97         app_perror("...unable to create mutex", rc); 
    98         return -7; 
    9964    } 
    10065 
     
    11984    PJ_LOG(3,("", "...Press Ctrl-C to abort")); 
    12085 
    121     abs_total = 0; 
     86    last_received = 0; 
     87    pj_gettimeofday(&last_print); 
     88    avg_bw = highest_bw = 0; 
    12289    count = 0; 
    12390 
    12491    for (;;) { 
    125         pj_uint32_t avg32; 
    126         pj_highprec_t avg; 
     92        pj_highprec_t received, cur_received, bw; 
     93        unsigned msec; 
     94        pj_time_val now, duration; 
    12795 
    128         for (i=0; i<ECHO_SERVER_MAX_THREADS; ++i) 
    129             pj_sem_wait(sem); 
     96        pj_thread_sleep(1000); 
    13097 
    131         /* calculate average so far: 
    132            avg = abs_total / count; 
    133          */ 
     98        received = cur_received = pj_atomic_get(total_bytes); 
     99        cur_received = cur_received - last_received; 
     100 
     101        pj_gettimeofday(&now); 
     102        duration = now; 
     103        PJ_TIME_VAL_SUB(duration, last_print); 
     104        msec = PJ_TIME_VAL_MSEC(duration); 
     105         
     106        bw = cur_received; 
     107        pj_highprec_mul(bw, 1000); 
     108        pj_highprec_div(bw, msec); 
     109 
     110        last_print = now; 
     111        last_received = received; 
     112 
     113        avg_bw = avg_bw + bw; 
    134114        count++; 
    135         abs_total += total_bw; 
    136         avg = abs_total; 
    137         pj_highprec_div(avg, count); 
    138         avg32 = (pj_uint32_t)avg; 
    139115 
    140          
    141116        PJ_LOG(3,("", "Synchronous UDP (%d threads): %u KB/s  (avg=%u KB/s) %s",  
    142117                  ECHO_SERVER_MAX_THREADS,  
    143                   total_bw / 1000, 
    144                   avg32 / 1000, 
     118                  (unsigned)(bw / 1000), 
     119                  (unsigned)(avg_bw / count / 1000), 
    145120                  (count==20 ? "<ses avg>" : ""))); 
    146121 
    147         total_bw = 0; 
     122        if (count==20) { 
     123            if (avg_bw/count > highest_bw) 
     124                highest_bw = avg_bw/count; 
    148125 
    149         if (count==20) { 
    150126            count = 0; 
    151             abs_total = 0; 
     127            avg_bw = 0; 
     128 
     129            PJ_LOG(3,("", "Highest average bandwidth=%u KB/s", 
     130                          (unsigned)(highest_bw/1000))); 
    152131        } 
    153  
    154         while (pj_sem_trywait(sem) == PJ_SUCCESS) 
    155             ; 
    156132    } 
    157133} 
Note: See TracChangeset for help on using the changeset viewer.