From 2f28507eb6105b350c2436d54f1c7d984bcc5f26 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Fri, 27 Sep 2013 07:38:32 +0800 Subject: [PATCH] add both sdev and mdev, it was so confused most time ref: http://www.plug.org/pipermail/plug/2005-June/002144.html --- src/mod/applications/mod_sonar/mod_sonar.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/mod/applications/mod_sonar/mod_sonar.c b/src/mod/applications/mod_sonar/mod_sonar.c index 183a9884d1..446319b2a8 100644 --- a/src/mod/applications/mod_sonar/mod_sonar.c +++ b/src/mod/applications/mod_sonar/mod_sonar.c @@ -103,7 +103,7 @@ SWITCH_STANDARD_APP(sonar_app) int loops; int lost = 0; int x; - int avg = 0, mdev = 0; + int avg = 0, sdev = 0, mdev = 0; int sum2; switch_event_t *event; sonar_ping_helper_t ph = { 0 }; @@ -157,18 +157,28 @@ SWITCH_STANDARD_APP(sonar_app) if (ph.received > 0) avg = ph.sum / ph.received; + sum2 = 0; + for(x = 0; x < ph.received; x++) { + sum2 += abs(ph.samples[x] - avg); + } + + if (ph.received > 0) { + mdev = sum2 / ph.received; + } + + sum2 = 0; for(x = 0; x < ph.received; x++) { sum2 += (ph.samples[x] - avg) * (ph.samples[x] - avg); } if (ph.received > 1) { - mdev = sqrt(sum2 / (ph.received - 1)); + sdev = sqrt(sum2 / (ph.received - 1)); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, - "Sonar Ping (in ms): min:%d max:%d avg:%d mdev:%d sent:%d recv: %d lost:%d lost/send:%2.2f%%\n", - ph.min, ph.max, avg, mdev, loops, ph.received, lost, lost * 1.0 / loops); + "Sonar Ping (in ms): min:%d max:%d avg:%d sdev:%d mdev:%d sent:%d recv: %d lost:%d lost/send:%2.2f%%\n", + ph.min, ph.max, avg, sdev, mdev, loops, ph.received, lost, lost * 1.0 / loops); if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "sonar::ping") == SWITCH_STATUS_SUCCESS) { const char *verbose_event; @@ -176,6 +186,7 @@ SWITCH_STANDARD_APP(sonar_app) switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_min", "%d", ph.min); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_max", "%d", ph.max); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_avg", "%d", avg); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_sdev", "%d", sdev); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_mdev", "%d", mdev); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_sent", "%d", loops); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_recv", "%d", ph.received);