Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2015, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | /* Create and send a message, and receive it */ |
| 37 | |
| 38 | #include "test_app.h" |
| 39 | #include <stdio.h> |
| 40 | |
| 41 | #ifndef __APPLE__ /* they deprecated the semaphore there... */ |
| 42 | #include <semaphore.h> |
| 43 | |
| 44 | #define my_sem_t sem_t |
| 45 | #define my_sem_init sem_init |
| 46 | #define my_sem_destroy sem_destroy |
| 47 | #define my_sem_timedwait sem_timedwait |
| 48 | #define my_sem_post sem_post |
| 49 | |
| 50 | #else // on APPLE |
| 51 | #include <sched.h> |
| 52 | #include <dispatch/dispatch.h> |
| 53 | |
| 54 | #define my_sem_t dispatch_semaphore_t |
| 55 | |
| 56 | static int my_sem_init(my_sem_t * s, int pshared, unsigned int value ) { |
| 57 | *s = dispatch_semaphore_create(value); |
| 58 | if (*s == NULL) |
| 59 | return ENOMEM; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int my_sem_destroy(my_sem_t *s) { |
| 64 | dispatch_release(*s); |
| 65 | *s = NULL; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int my_sem_timedwait(my_sem_t * s, struct timespec *ts) { |
| 70 | struct timespec tsn; |
| 71 | int64_t nsec; |
| 72 | dispatch_time_t when; |
| 73 | |
| 74 | CHECK_SYS( clock_gettime(CLOCK_REALTIME, &tsn) ); |
| 75 | |
| 76 | nsec = (ts->tv_sec * 1000000000) + ts->tv_nsec |
| 77 | - (tsn.tv_sec * 1000000000) - tsn.tv_nsec; |
| 78 | |
| 79 | when = dispatch_time ( DISPATCH_TIME_NOW, nsec ); |
| 80 | |
| 81 | return dispatch_semaphore_wait ( *s, when ) ? ETIMEDOUT : 0; |
| 82 | } |
| 83 | |
| 84 | static int my_sem_post(my_sem_t *s) { |
| 85 | dispatch_semaphore_signal(*s); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | #endif // APPLE |
| 90 | |
| 91 | |
| 92 | |
| 93 | struct ta_mess_info { |
| 94 | int32_t randval; /* a random value to store in Test-AVP */ |
| 95 | struct timespec ts; /* Time of sending the message */ |
| 96 | }; |
| 97 | |
| 98 | static my_sem_t ta_sem; /* To handle the concurrency */ |
| 99 | |
| 100 | /* Cb called when an answer is received */ |
| 101 | static void ta_cb_ans(void * data, struct msg ** msg) |
| 102 | { |
| 103 | struct ta_mess_info * mi = (struct ta_mess_info *)data; |
| 104 | struct timespec ts; |
| 105 | struct avp * avp; |
| 106 | struct avp_hdr * hdr; |
| 107 | unsigned long dur; |
| 108 | |
| 109 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &ts), return ); |
| 110 | |
| 111 | /* Value of Result Code */ |
| 112 | CHECK_FCT_DO( fd_msg_search_avp ( *msg, ta_res_code, &avp), return ); |
| 113 | if (avp) { |
| 114 | CHECK_FCT_DO( fd_msg_avp_hdr( avp, &hdr ), return ); |
| 115 | } |
| 116 | if (!avp || !hdr || hdr->avp_value->i32 != 2001) { |
| 117 | /* error */ |
| 118 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 119 | ta_conf->stats.nb_errs++; |
| 120 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 121 | goto end; |
| 122 | } |
| 123 | |
| 124 | /* Check value of Test-AVP */ |
| 125 | CHECK_FCT_DO( fd_msg_search_avp ( *msg, ta_avp, &avp), return ); |
| 126 | if (avp) { |
| 127 | CHECK_FCT_DO( fd_msg_avp_hdr( avp, &hdr ), return ); |
| 128 | ASSERT(hdr->avp_value->i32 == mi->randval); |
| 129 | } |
| 130 | |
| 131 | /* Compute how long it took */ |
| 132 | dur = ((ts.tv_sec - mi->ts.tv_sec) * 1000000) + ((ts.tv_nsec - mi->ts.tv_nsec) / 1000); |
| 133 | |
| 134 | /* Add this value to the stats */ |
| 135 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 136 | |
| 137 | if (ta_conf->stats.nb_recv) { |
| 138 | /* Ponderate in the avg */ |
| 139 | ta_conf->stats.avg = (ta_conf->stats.avg * ta_conf->stats.nb_recv + dur) / (ta_conf->stats.nb_recv + 1); |
| 140 | /* Min, max */ |
| 141 | if (dur < ta_conf->stats.shortest) |
| 142 | ta_conf->stats.shortest = dur; |
| 143 | if (dur > ta_conf->stats.longest) |
| 144 | ta_conf->stats.longest = dur; |
| 145 | } else { |
| 146 | ta_conf->stats.shortest = dur; |
| 147 | ta_conf->stats.longest = dur; |
| 148 | ta_conf->stats.avg = dur; |
| 149 | } |
| 150 | ta_conf->stats.nb_recv++; |
| 151 | |
| 152 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 153 | |
| 154 | end: |
| 155 | /* Free the message */ |
| 156 | CHECK_FCT_DO(fd_msg_free(*msg), ); |
| 157 | *msg = NULL; |
| 158 | |
| 159 | free(mi); |
| 160 | |
| 161 | /* Post the semaphore */ |
| 162 | CHECK_SYS_DO( my_sem_post(&ta_sem), ); |
| 163 | |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | /* Create a test message */ |
| 168 | static void ta_bench_test_message() |
| 169 | { |
| 170 | struct msg * req = NULL; |
| 171 | struct avp * avp; |
| 172 | union avp_value val; |
| 173 | struct ta_mess_info * mi = NULL; |
| 174 | |
| 175 | TRACE_DEBUG(FULL, "Creating a new message for sending."); |
| 176 | |
| 177 | /* Create the request */ |
| 178 | CHECK_FCT_DO( fd_msg_new( ta_cmd_r, MSGFL_ALLOC_ETEID, &req ), goto out ); |
| 179 | |
| 180 | /* Create a new session */ |
| 181 | #define TEST_APP_SID_OPT "app_testb" |
| 182 | CHECK_FCT_DO( fd_msg_new_session( req, (os0_t)TEST_APP_SID_OPT, CONSTSTRLEN(TEST_APP_SID_OPT) ), goto out ); |
| 183 | |
| 184 | /* Create the random value to store with the session */ |
| 185 | mi = malloc(sizeof(struct ta_mess_info)); |
| 186 | if (mi == NULL) { |
| 187 | fd_log_debug("malloc failed: %s", strerror(errno)); |
| 188 | goto out; |
| 189 | } |
| 190 | |
| 191 | mi->randval = (int32_t)random(); |
| 192 | |
| 193 | /* Now set all AVPs values */ |
| 194 | |
| 195 | /* Set the Destination-Realm AVP */ |
| 196 | { |
| 197 | CHECK_FCT_DO( fd_msg_avp_new ( ta_dest_realm, 0, &avp ), goto out ); |
| 198 | val.os.data = (unsigned char *)(ta_conf->dest_realm); |
| 199 | val.os.len = strlen(ta_conf->dest_realm); |
| 200 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 201 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 202 | } |
| 203 | |
| 204 | /* Set the Destination-Host AVP if needed*/ |
| 205 | if (ta_conf->dest_host) { |
| 206 | CHECK_FCT_DO( fd_msg_avp_new ( ta_dest_host, 0, &avp ), goto out ); |
| 207 | val.os.data = (unsigned char *)(ta_conf->dest_host); |
| 208 | val.os.len = strlen(ta_conf->dest_host); |
| 209 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 210 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 211 | } |
| 212 | |
| 213 | /* Set Origin-Host & Origin-Realm */ |
| 214 | CHECK_FCT_DO( fd_msg_add_origin ( req, 0 ), goto out ); |
| 215 | |
| 216 | /* Set the User-Name AVP if needed*/ |
| 217 | if (ta_conf->user_name) { |
| 218 | CHECK_FCT_DO( fd_msg_avp_new ( ta_user_name, 0, &avp ), goto out ); |
| 219 | val.os.data = (unsigned char *)(ta_conf->user_name); |
| 220 | val.os.len = strlen(ta_conf->user_name); |
| 221 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 222 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 223 | } |
| 224 | |
| 225 | /* Set the Test-AVP AVP */ |
| 226 | { |
| 227 | CHECK_FCT_DO( fd_msg_avp_new ( ta_avp, 0, &avp ), goto out ); |
| 228 | val.i32 = mi->randval; |
| 229 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 230 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 231 | } |
| 232 | |
| 233 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &mi->ts), goto out ); |
| 234 | |
| 235 | /* Send the request */ |
| 236 | CHECK_FCT_DO( fd_msg_send( &req, ta_cb_ans, mi ), goto out ); |
| 237 | |
| 238 | /* Increment the counter */ |
| 239 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 240 | ta_conf->stats.nb_sent++; |
| 241 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 242 | |
| 243 | out: |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | /* The function called when the signal is received */ |
| 248 | static void ta_bench_start() { |
| 249 | struct timespec end_time, now; |
| 250 | struct ta_stats start, end; |
| 251 | int nsec = 0; |
| 252 | |
| 253 | /* Save the initial stats */ |
| 254 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 255 | memcpy(&start, &ta_conf->stats, sizeof(struct ta_stats)); |
| 256 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 257 | |
| 258 | /* We will run for ta_conf->bench_duration seconds */ |
| 259 | LOG_N("Starting benchmark client, %ds", ta_conf->bench_duration); |
| 260 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &end_time), ); |
| 261 | end_time.tv_sec += ta_conf->bench_duration; |
| 262 | |
| 263 | /* Now loop until timeout is reached */ |
| 264 | do { |
| 265 | /* Do not create more that NB_CONCURRENT_MESSAGES in paralel */ |
| 266 | int ret = my_sem_timedwait(&ta_sem, &end_time); |
| 267 | if (ret == -1) { |
| 268 | ret = errno; |
| 269 | if (ret != ETIMEDOUT) { |
| 270 | CHECK_POSIX_DO(ret, ); /* Just to log it */ |
| 271 | } |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | /* Update the current time */ |
| 276 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &now), ); |
| 277 | |
| 278 | if (!TS_IS_INFERIOR(&now, &end_time)) |
| 279 | break; |
| 280 | |
| 281 | /* Create and send a new test message */ |
| 282 | ta_bench_test_message(); |
| 283 | } while (1); |
| 284 | |
| 285 | do { |
| 286 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 287 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &now), ); /* Re-read the time because we might have spent some time wiating for the mutex */ |
| 288 | memcpy(&end, &ta_conf->stats, sizeof(struct ta_stats)); |
| 289 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 290 | |
| 291 | /* Now, display the statistics */ |
| 292 | LOG_N( "------- app_test Benchmark results, end sending +%ds ---------", nsec); |
| 293 | if (now.tv_nsec >= end_time.tv_nsec) { |
| 294 | LOG_N( " Executing for: %d.%06ld sec", |
| 295 | (int)(now.tv_sec + ta_conf->bench_duration - end_time.tv_sec), |
| 296 | (long)(now.tv_nsec - end_time.tv_nsec) / 1000); |
| 297 | } else { |
| 298 | LOG_N( " Executing for: %d.%06ld sec", |
| 299 | (int)(now.tv_sec + ta_conf->bench_duration - 1 - end_time.tv_sec), |
| 300 | (long)(now.tv_nsec + 1000000000 - end_time.tv_nsec) / 1000); |
| 301 | } |
| 302 | LOG_N( " %llu messages sent", end.nb_sent - start.nb_sent); |
| 303 | LOG_N( " %llu error(s) received", end.nb_errs - start.nb_errs); |
| 304 | LOG_N( " %llu answer(s) received", end.nb_recv - start.nb_recv); |
| 305 | LOG_N( " Overall:"); |
| 306 | LOG_N( " fastest: %ld.%06ld sec.", end.shortest / 1000000, end.shortest % 1000000); |
| 307 | LOG_N( " slowest: %ld.%06ld sec.", end.longest / 1000000, end.longest % 1000000); |
| 308 | LOG_N( " Average: %ld.%06ld sec.", end.avg / 1000000, end.avg % 1000000); |
| 309 | LOG_N( " Throughput: %llu messages / sec", (end.nb_recv - start.nb_recv) / (( now.tv_sec + ta_conf->bench_duration - end_time.tv_sec ) + ((now.tv_nsec - end_time.tv_nsec) / 1000000000))); |
| 310 | LOG_N( "-------------------------------------"); |
| 311 | |
| 312 | nsec ++; |
| 313 | sleep(1); |
| 314 | } while ( (end.nb_sent - start.nb_sent) > (end.nb_errs - start.nb_errs) + (end.nb_recv - start.nb_recv) ); |
| 315 | LOG_N( "--------------- Test Complete --------------"); |
| 316 | |
| 317 | } |
| 318 | |
| 319 | |
| 320 | int ta_bench_init(void) |
| 321 | { |
| 322 | CHECK_SYS( my_sem_init( &ta_sem, 0, ta_conf->bench_concur) ); |
| 323 | |
| 324 | CHECK_FCT( fd_event_trig_regcb(ta_conf->signal, "test_app.bench", ta_bench_start ) ); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | void ta_bench_fini(void) |
| 330 | { |
| 331 | // CHECK_FCT_DO( fd_sig_unregister(ta_conf->signal), /* continue */ ); |
| 332 | |
| 333 | CHECK_SYS_DO( my_sem_destroy(&ta_sem), ); |
| 334 | |
| 335 | return; |
| 336 | }; |