Skip to content

Commit

Permalink
Add stress test: cleanup, update readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
philljj committed Jan 3, 2024
1 parent db154b8 commit 9685820
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu-check-curl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Ubuntu Build Test with Curl Support

on:
push:
branches: [ '*' ]
branches: [ 'master', 'main', 'release/**' ]
pull_request:
branches: [ '*' ]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Ubuntu Build Test

on:
push:
branches: [ '*' ]
branches: [ 'master', 'main', 'release/**' ]
pull_request:
branches: [ '*' ]

Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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.
6 changes: 5 additions & 1 deletion examples/mqttexample.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions examples/mqttexample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 10 additions & 10 deletions examples/mqttnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/multithread/multithread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9685820

Please sign in to comment.