Skip to content

Commit 2009f02

Browse files
authored
Merge pull request #8592 from deepikabhavnani/nw_stats_ver2
Network Socket Statistics
2 parents 1c16383 + 09f4d0b commit 2009f02

34 files changed

+565
-6
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ NetworkInterface *net;
3838
Timer tc_bucket; // Timer to limit a test cases run time
3939
}
4040

41+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
42+
mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
43+
#endif
44+
4145
char tcp_global::rx_buffer[RX_BUFF_SIZE];
4246
char tcp_global::tx_buffer[TX_BUFF_SIZE];
4347

@@ -115,6 +119,13 @@ int split2half_rmng_tcp_test_time()
115119
return (tcp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
116120
}
117121

122+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
123+
int fetch_stats()
124+
{
125+
return SocketStats::mbed_stats_socket_get_each(&tcp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
126+
}
127+
#endif
128+
118129
// Test setup
119130
utest::v1::status_t greentea_setup(const size_t number_of_cases)
120131
{

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ void fill_tx_buffer_ascii(char *buff, size_t len);
2424
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket &sock);
2525
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket &sock);
2626

27+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
28+
extern mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
29+
int fetch_stats(void);
30+
#endif
31+
2732
/**
2833
* Single testcase might take only half of the remaining execution time
2934
*/

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
114114

115115
void TCPSOCKET_ECHOTEST_NONBLOCK()
116116
{
117+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
118+
int j = 0;
119+
int count = fetch_stats();
120+
for (; j < count; j++) {
121+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
122+
}
123+
#endif
117124
tc_exec_time.start();
118125
time_allotted = split2half_rmng_tcp_test_time(); // [s]
119126

@@ -160,6 +167,15 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
160167
bytes2send -= sent;
161168
}
162169
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
170+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
171+
count = fetch_stats();
172+
for (j = 0; j < count; j++) {
173+
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
174+
break;
175+
}
176+
}
177+
TEST_ASSERT_EQUAL(bytes2send, tcp_stats[j].sent_bytes);
178+
#endif
163179
tx_sem.wait(split2half_rmng_tcp_test_time());
164180
thread->join();
165181
delete thread;

TESTS/netsocket/tcp/tcpsocket_open_close_repeat.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ using namespace utest::v1;
2626

2727
void TCPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
30+
int count = fetch_stats();
31+
for (int j = 0; j < count; j++) {
32+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33+
}
34+
#endif
2935
TCPSocket *sock = new TCPSocket;
3036
if (!sock) {
3137
TEST_FAIL();
@@ -36,4 +42,10 @@ void TCPSOCKET_OPEN_CLOSE_REPEAT()
3642
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
3743
}
3844
delete sock;
45+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
46+
count = fetch_stats();
47+
for (int j = 0; j < count; j++) {
48+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
49+
}
50+
#endif
3951
}

TESTS/netsocket/tcp/tcpsocket_open_limit.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ void TCPSOCKET_OPEN_LIMIT()
7070
break;
7171
}
7272

73+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
74+
int count = fetch_stats();
75+
int open_count = 0;
76+
for (int j = 0; j < count; j++) {
77+
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
78+
open_count++;
79+
}
80+
}
81+
TEST_ASSERT(open_count >= 4);
82+
#endif
83+
7384
TCPSocketItem *tmp;
7485
for (TCPSocketItem *it = socket_list_head; it;) {
7586
++open_sockets[i];

TESTS/netsocket/udp/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ namespace {
3737
NetworkInterface *net;
3838
}
3939

40+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
41+
mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
42+
#endif
43+
4044
NetworkInterface *get_interface()
4145
{
4246
return net;
@@ -76,6 +80,13 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
7680
}
7781
}
7882

83+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
84+
int fetch_stats()
85+
{
86+
return SocketStats::mbed_stats_socket_get_each(&udp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
87+
}
88+
#endif
89+
7990
// Test setup
8091
utest::v1::status_t greentea_setup(const size_t number_of_cases)
8192
{

TESTS/netsocket/udp/udp_tests.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ NetworkInterface *get_interface();
2222
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
2424

25+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
26+
extern mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
27+
int fetch_stats(void);
28+
#endif
29+
2530
/*
2631
* Test cases
2732
*/

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
121121

122122
void UDPSOCKET_ECHOTEST_NONBLOCK()
123123
{
124+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
125+
int j = 0;
126+
int count = fetch_stats();
127+
for (; j < count; j++) {
128+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
129+
}
130+
#endif
131+
124132
SocketAddress udp_addr;
125133
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
126134
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
@@ -174,11 +182,27 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
174182
}
175183
}
176184
free(stack_mem);
185+
177186
// Packet loss up to 30% tolerated
178187
if (packets_sent > 0) {
179188
double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
180189
printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
181190
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
191+
192+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
193+
count = fetch_stats();
194+
for (j = 0; j < count; j++) {
195+
if ((NSAPI_UDP == udp_stats[j].proto) && (SOCK_OPEN == udp_stats[j].state)) {
196+
TEST_ASSERT(udp_stats[j].sent_bytes != 0);
197+
TEST_ASSERT(udp_stats[j].recv_bytes != 0);
198+
break;
199+
}
200+
}
201+
loss_ratio = 1 - ((double)udp_stats[j].recv_bytes / (double)udp_stats[j].sent_bytes);
202+
printf("Bytes sent: %d, bytes received %d, loss ratio %.2lf\r\n", udp_stats[j].sent_bytes, udp_stats[j].recv_bytes, loss_ratio);
203+
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
204+
205+
#endif
182206
}
183207
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
184208
}

TESTS/netsocket/udp/udpsocket_open_close_repeat.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ using namespace utest::v1;
2626

2727
void UDPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
30+
int count = fetch_stats();
31+
for (int j = 0; j < count; j++) {
32+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
33+
}
34+
#endif
2935
UDPSocket *sock = new UDPSocket;
3036
if (!sock) {
3137
TEST_FAIL();
@@ -36,4 +42,10 @@ void UDPSOCKET_OPEN_CLOSE_REPEAT()
3642
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
3743
}
3844
delete sock;
45+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
46+
count = fetch_stats();
47+
for (int j = 0; j < count; j++) {
48+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
49+
}
50+
#endif
3951
}

TESTS/netsocket/udp/udpsocket_open_limit.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "UDPSocket.h"
2222
#include "unity/unity.h"
2323
#include "utest.h"
24+
#include "SocketStats.h"
2425

2526
using namespace utest::v1;
2627

@@ -69,7 +70,16 @@ void UDPSOCKET_OPEN_LIMIT()
6970
if (!socket_list_head) {
7071
break;
7172
}
72-
73+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
74+
int count = fetch_stats();
75+
int open_count = 0;
76+
for (int j = 0; j < count; j++) {
77+
if ((udp_stats[j].state == SOCK_OPEN) && (udp_stats[j].proto == NSAPI_UDP)) {
78+
open_count++;
79+
}
80+
}
81+
TEST_ASSERT(open_count >= 3);
82+
#endif
7383
UDPSocketItem *tmp;
7484
for (UDPSocketItem *it = socket_list_head; it;) {
7585
++open_sockets[i];

0 commit comments

Comments
 (0)