Changeset 5 for pjproject/main/pjlib/src/pjlib-test/echo_clt.c
- Timestamp:
- Nov 1, 2005 9:46:17 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/main/pjlib/src/pjlib-test/echo_clt.c
r4 r5 29 29 }; 30 30 31 static pj_sem_t *sem; 32 static pj_mutex_t *mutex; 33 static pj_size_t total_bw; 34 static unsigned total_poster; 35 static pj_time_val first_report; 31 static pj_atomic_t *totalBytes; 36 32 37 33 #define MSEC_PRINT_DURATION 1000 … … 62 58 struct client *client = arg; 63 59 pj_status_t last_recv_err = PJ_SUCCESS, last_send_err = PJ_SUCCESS; 64 65 pj_time_val last_report, next_report; 66 pj_size_t thread_total; 60 unsigned counter = 0; 67 61 68 62 rc = app_socket(PJ_AF_INET, client->sock_type, 0, -1, &sock); … … 90 84 pj_ntohs(addr.sin_port))); 91 85 92 pj_ create_random_string(send_buf, BUF_SIZE);93 thread_total = 0;86 pj_memset(send_buf, 'A', BUF_SIZE); 87 send_buf[BUF_SIZE-1]='\0'; 94 88 95 89 /* Give other thread chance to initialize themselves! */ 96 pj_thread_sleep(500); 97 98 pj_gettimeofday(&last_report); 99 next_report = first_report; 90 pj_thread_sleep(200); 100 91 101 92 //PJ_LOG(3,("", "...thread %p running", pj_thread_this())); … … 104 95 int rc; 105 96 pj_ssize_t bytes; 106 pj_time_val now; 97 98 ++counter; 107 99 108 100 /* Send a packet. */ … … 148 140 } 149 141 150 /* Accumulate total received. */151 thread_total = thread_total + bytes;152 153 /* Report current bandwidth on due. */154 pj_gettimeofday(&now);155 156 if (PJ_TIME_VAL_GTE(now, next_report)) {157 pj_uint32_t bw;158 pj_bool_t signal_parent = 0;159 pj_time_val duration;160 pj_uint32_t msec;161 162 duration = now;163 PJ_TIME_VAL_SUB(duration, last_report);164 msec = PJ_TIME_VAL_MSEC(duration);165 166 bw = thread_total * 1000 / msec;167 168 /* Post result to parent */169 pj_mutex_lock(mutex);170 total_bw += bw;171 total_poster++;172 //PJ_LOG(3,("", "...thread %p posting result", pj_thread_this()));173 if (total_poster >= ECHO_CLIENT_MAX_THREADS)174 signal_parent = 1;175 pj_mutex_unlock(mutex);176 177 thread_total = 0;178 last_report = now;179 next_report.sec++;180 181 if (signal_parent) {182 pj_sem_post(sem);183 }184 185 pj_thread_sleep(0);186 }187 188 142 if (bytes == 0) 189 143 continue; 190 144 191 145 if (pj_memcmp(send_buf, recv_buf, BUF_SIZE) != 0) { 192 //PJ_LOG(3,("", "...error: buffer has changed!")); 193 break; 194 } 146 recv_buf[BUF_SIZE-1] = '\0'; 147 PJ_LOG(3,("", "...error: buffer %u has changed!\n" 148 "send_buf=%s\n" 149 "recv_buf=%s\n", 150 counter, send_buf, recv_buf)); 151 //break; 152 } 153 154 /* Accumulate total received. */ 155 pj_atomic_add(totalBytes, bytes); 195 156 } 196 157 … … 206 167 struct client client; 207 168 int i; 169 pj_atomic_value_t last_received; 170 pj_timestamp last_report; 208 171 209 172 client.sock_type = sock_type; … … 213 176 pool = pj_pool_create( mem, NULL, 4000, 4000, NULL ); 214 177 215 rc = pj_sem_create(pool, NULL, 0, ECHO_CLIENT_MAX_THREADS+1, &sem); 216 if (rc != PJ_SUCCESS) { 217 PJ_LOG(3,("", "...error: unable to create semaphore", rc)); 218 return -10; 219 } 220 221 rc = pj_mutex_create_simple(pool, NULL, &mutex); 222 if (rc != PJ_SUCCESS) { 223 PJ_LOG(3,("", "...error: unable to create mutex", rc)); 224 return -20; 225 } 226 227 /* 228 rc = pj_atomic_create(pool, 0, &atom); 178 rc = pj_atomic_create(pool, 0, &totalBytes); 229 179 if (rc != PJ_SUCCESS) { 230 180 PJ_LOG(3,("", "...error: unable to create atomic variable", rc)); 231 181 return -30; 232 182 } 233 */234 183 235 184 PJ_LOG(3,("", "Echo client started")); … … 237 186 ECHO_SERVER_ADDRESS, ECHO_SERVER_START_PORT)); 238 187 PJ_LOG(3,("", " Press Ctrl-C to exit")); 239 240 pj_gettimeofday(&first_report);241 first_report.sec += 2;242 188 243 189 for (i=0; i<ECHO_CLIENT_MAX_THREADS; ++i) { … … 251 197 } 252 198 199 last_received = 0; 200 pj_get_timestamp(&last_report); 201 253 202 for (;;) { 254 pj_uint32_t bw; 255 256 pj_sem_wait(sem); 257 258 pj_mutex_lock(mutex); 259 bw = total_bw; 260 total_bw = 0; 261 total_poster = 0; 262 pj_mutex_unlock(mutex); 203 pj_timestamp now; 204 unsigned long received, cur_received; 205 unsigned msec; 206 pj_highprec_t bw; 207 pj_time_val elapsed; 208 unsigned bw32; 209 210 pj_thread_sleep(1000); 211 212 pj_get_timestamp(&now); 213 elapsed = pj_elapsed_time(&last_report, &now); 214 msec = PJ_TIME_VAL_MSEC(elapsed); 215 216 received = pj_atomic_get(totalBytes); 217 cur_received = received - last_received; 218 219 bw = cur_received; 220 pj_highprec_mul(bw, 1000); 221 pj_highprec_div(bw, msec); 222 223 bw32 = (unsigned)bw; 224 225 last_report = now; 226 last_received = received; 263 227 264 228 PJ_LOG(3,("", "...%d threads, total bandwidth: %d KB/s", 265 ECHO_CLIENT_MAX_THREADS, bw /1000));229 ECHO_CLIENT_MAX_THREADS, bw32/1000)); 266 230 } 267 231
Note: See TracChangeset
for help on using the changeset viewer.