From 96858205ea2e0c369f88fcbd1da9d2663ef9229e Mon Sep 17 00:00:00 2001 From: philljj Date: Wed, 3 Jan 2024 10:53:32 -0600 Subject: [PATCH] Add stress test: cleanup, update readme. --- .github/workflows/ubuntu-check-curl.yml | 2 +- .github/workflows/ubuntu-check.yml | 2 +- CMakeLists.txt | 3 +++ README.md | 17 ++++++++++++++++- examples/mqttexample.c | 6 +++++- examples/mqttexample.h | 1 + examples/mqttnet.c | 20 ++++++++++---------- examples/multithread/multithread.c | 2 +- 8 files changed, 38 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ubuntu-check-curl.yml b/.github/workflows/ubuntu-check-curl.yml index 6e03adb1..b42e6246 100644 --- a/.github/workflows/ubuntu-check-curl.yml +++ b/.github/workflows/ubuntu-check-curl.yml @@ -2,7 +2,7 @@ name: Ubuntu Build Test with Curl Support on: push: - branches: [ '*' ] + branches: [ 'master', 'main', 'release/**' ] pull_request: branches: [ '*' ] diff --git a/.github/workflows/ubuntu-check.yml b/.github/workflows/ubuntu-check.yml index 9c20d6e7..e38aa61d 100644 --- a/.github/workflows/ubuntu-check.yml +++ b/.github/workflows/ubuntu-check.yml @@ -2,7 +2,7 @@ name: Ubuntu Build Test on: push: - branches: [ '*' ] + branches: [ 'master', 'main', 'release/**' ] pull_request: branches: [ '*' ] diff --git a/CMakeLists.txt b/CMakeLists.txt index 482f71cc..40074463 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,6 +156,9 @@ if (WOLFMQTT_CURL) list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_CURL") endif() +# Note: not adding stress option to cmake build as of yet. stress is for +# testing only and requires the scripts/ dir to be useful. + # generate options file message("Generating user options header...") diff --git a/README.md b/README.md index 1c1f16bb..cba3d903 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ The Sensor Network client implements the MQTT-SN protocol for low-bandwidth netw More about MQTT-SN examples in [examples/sn-client/README.md](examples/sn-client/README.md) ### Multithread Example -This example exercises the multithreading capabilities of the client library. The client implements two tasks: one that publishes to the broker; and another that waits for messages from the broker. The publish thread is created `NUM_PUB_TASKS` times (10 by default) and sends unique messages to the broker. This feature is enabled using the `--enable-mt` configuration option. The example is located in `/examples/multithread/`. +This example exercises the multithreading capabilities of the client library. The client implements two tasks: one that publishes to the broker; and another that waits for messages from the broker. The publish thread is created `NUM_PUB_TASKS` times (5 by default) and sends unique messages to the broker. This feature is enabled using the `--enable-mt` configuration option. The example is located in `/examples/multithread/`. The multi-threading feature can also be used with the non-blocking socket (--enable-nonblock). @@ -380,3 +380,18 @@ The `--enable-curl` option works with these combinations: However `--enable-curl` is incompatible and not supported with these options: - `--enable-all` - `--enable-sn` + +## Stress Build Option + +To simplify testing a stress build option has been added, `--enable-stress=[args]`. +The Stress option enables multithreading and nonblocking, and adds defines for +`TEST_NONBLOCK`, `NUM_PUB_TASKS`, and `NUM_PUB_PER_TASK`. + +Examples of useage: +- `--enable-stress`: stress with default options. +- `--enable-stress=t7,p8`: stress with 7 threads, and 8 publishes per thread. +- `--enable-stress=t7,p8 --enable-curl`: same as above, but with curl backend. + +Note: When stress is enabled, the Multithread Example becomes localhost only +and will not connect to remote servers. Additionally the test `scripts/stress.test` +is added to `make check`, and all other tests are disabled. diff --git a/examples/mqttexample.c b/examples/mqttexample.c index f1b0dbe4..5c802782 100644 --- a/examples/mqttexample.c +++ b/examples/mqttexample.c @@ -563,7 +563,11 @@ int mqtt_check_timeout(int rc, word32* start_sec, word32 timeout_sec) return rc; } - if (timeout_sec == 0) { timeout_sec = 2; } + /* Default to 2s timeout. This function sometimes incorrectly + * triggers if 1s is used because of rounding. */ + if (timeout_sec == 0) { + timeout_sec = DEFAULT_CHK_TIMEOUT_S; + } elapsed_sec = mqtt_get_timer_seconds(); if (*start_sec < elapsed_sec) { diff --git a/examples/mqttexample.h b/examples/mqttexample.h index 58122518..744e6900 100644 --- a/examples/mqttexample.h +++ b/examples/mqttexample.h @@ -76,6 +76,7 @@ #define DEFAULT_CMD_TIMEOUT_MS 30000 #define DEFAULT_CON_TIMEOUT_MS 5000 +#define DEFAULT_CHK_TIMEOUT_S 2 #define DEFAULT_MQTT_QOS MQTT_QOS_0 #define DEFAULT_KEEP_ALIVE_SEC 60 #define DEFAULT_CLIENT_ID "WolfMQTTClient" diff --git a/examples/mqttnet.c b/examples/mqttnet.c index e636647f..53111939 100644 --- a/examples/mqttnet.c +++ b/examples/mqttnet.c @@ -391,7 +391,7 @@ static int NetRead(void *context, byte* buf, int buf_len, * MQTT_CODE_CONTINUE, or proceed with a smaller buffer read/write. * Used for testing nonblocking. */ static int -mqttcurl_test_nonblock(int * buf_len, int for_recv) +mqttcurl_test_nonblock(int* buf_len, int for_recv) { static int testNbAlt = 0; static int testSmallerBuf = 0; @@ -414,10 +414,10 @@ mqttcurl_test_nonblock(int * buf_len, int for_recv) if (*buf_len > 2) { *buf_len /= 2; testSmallerBuf = 1; - #if defined(WOLFMQTT_DEBUG_SOCKET) + #if defined(WOLFMQTT_DEBUG_SOCKET) PRINTF("mqttcurl_test_nonblock(%d): testing small buff: %d", for_recv, *buf_len); - #endif + #endif } } else { @@ -427,7 +427,7 @@ mqttcurl_test_nonblock(int * buf_len, int for_recv) return MQTT_CODE_SUCCESS; } -#endif /* defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK) */ +#endif /* WOLFMQTT_NONBLOCK && WOLFMQTT_TEST_NONBLOCK */ static int mqttcurl_wait(curl_socket_t sockfd, int for_recv, int timeout_ms, @@ -490,7 +490,7 @@ mqttcurl_wait(curl_socket_t sockfd, int for_recv, int timeout_ms, } static int -mqttcurl_connect(SocketContext * sock, const char* host, word16 port, +mqttcurl_connect(SocketContext* sock, const char* host, word16 port, int timeout_ms) { CURLcode res = CURLE_OK; @@ -749,7 +749,7 @@ static int NetWrite(void *context, const byte* buf, int buf_len, return MQTT_CODE_CONTINUE; } } -#endif /* defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK) */ +#endif /* WOLFMQTT_NONBLOCK && WOLFMQTT_TEST_NONBLOCK */ /* get the active socket from libcurl */ res = curl_easy_getinfo(sock->curl, CURLINFO_ACTIVESOCKET, &sockfd); @@ -832,7 +832,7 @@ static int NetRead(void *context, byte* buf, int buf_len, return MQTT_CODE_CONTINUE; } } -#endif /* defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK) */ +#endif /* WOLFMQTT_NONBLOCK && WOLFMQTT_TEST_NONBLOCK */ /* get the active socket from libcurl */ res = curl_easy_getinfo(sock->curl, CURLINFO_ACTIVESOCKET, &sockfd); @@ -933,21 +933,21 @@ static void tcp_setup_timeout(struct timeval* tv, int timeout_ms) } } -static void tcp_set_fds(SocketContext * sock, fd_set * recvfds, fd_set * errfds) +static void tcp_set_fds(SocketContext* sock, fd_set* recvfds, fd_set* errfds) { /* Setup select file descriptors to watch */ FD_ZERO(errfds); FD_SET(sock->fd, errfds); FD_ZERO(recvfds); FD_SET(sock->fd, recvfds); - #ifdef WOLFMQTT_ENABLE_STDIN_CAP +#ifdef WOLFMQTT_ENABLE_STDIN_CAP #ifdef WOLFMQTT_MULTITHREAD FD_SET(sock->pfd[0], recvfds); #endif if (!sock->mqttCtx->test_mode) { FD_SET(STDIN, recvfds); } - #endif /* WOLFMQTT_ENABLE_STDIN_CAP */ +#endif /* WOLFMQTT_ENABLE_STDIN_CAP */ } #ifdef WOLFMQTT_NONBLOCK diff --git a/examples/multithread/multithread.c b/examples/multithread/multithread.c index 4a00c8a4..e713e7df 100644 --- a/examples/multithread/multithread.c +++ b/examples/multithread/multithread.c @@ -38,7 +38,7 @@ /* Configuration */ /* Number of publish tasks. Each will send a unique message to the broker. */ -#if !defined NUM_PUB_TASKS && !defined NUM_PUB_PER_TASK +#if !defined(NUM_PUB_TASKS) && !defined(NUM_PUB_PER_TASK) #define NUM_PUB_TASKS 5 #define NUM_PUB_PER_TASK 2 #endif