|
| 1 | +/* |
| 2 | + * mbed Microcontroller Library |
| 3 | + * Copyright (c) 2006-2019 ARM Limited |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test |
| 22 | +*/ |
| 23 | + |
| 24 | +#include "mbed.h" |
| 25 | +#include "unity/unity.h" |
| 26 | +#include "greentea-client/test_env.h" |
| 27 | +#include <string> |
| 28 | + |
| 29 | +#define MAX_THREADS 5 |
| 30 | + |
| 31 | +#define MAX_RETRIES 3 |
| 32 | + |
| 33 | +#ifdef MBED_CONF_DOWNLOAD_TEST_URL_HOST |
| 34 | +const char dl_host[] = MBED_CONF_DOWNLOAD_TEST_URL_HOST; |
| 35 | +#else |
| 36 | +const char dl_host[] = "armmbed.github.io"; |
| 37 | +#endif |
| 38 | +#ifdef MBED_CONF_DOWNLOAD_TEST_URL_PATH |
| 39 | +const char dl_path[] = MBED_CONF_DOWNLOAD_TEST_URL_PATH; |
| 40 | +#else |
| 41 | +const char dl_path[] = "/mbed-test-files/sample.txt"; |
| 42 | +#endif |
| 43 | + |
| 44 | +const char req_template[] = "GET %s HTTP/1.1\nHost: %s\n\n"; |
| 45 | + |
| 46 | +#define REQ_BUF_SIZE 256 |
| 47 | +#define RECV_BUF_SIZE 1024 |
| 48 | + |
| 49 | +static char g_request_buffer[MAX_THREADS][REQ_BUF_SIZE]; // the default request is 65bytes long. |
| 50 | +static char g_receive_buffer[MAX_THREADS * RECV_BUF_SIZE]; |
| 51 | + |
| 52 | +static volatile bool event_fired[MAX_THREADS] = { }; |
| 53 | + |
| 54 | +static void socket_event_0(void) |
| 55 | +{ |
| 56 | + event_fired[0] = true; |
| 57 | +} |
| 58 | +static void socket_event_1(void) |
| 59 | +{ |
| 60 | + event_fired[1] = true; |
| 61 | +} |
| 62 | +static void socket_event_2(void) |
| 63 | +{ |
| 64 | + event_fired[2] = true; |
| 65 | +} |
| 66 | +static void socket_event_3(void) |
| 67 | +{ |
| 68 | + event_fired[3] = true; |
| 69 | +} |
| 70 | +static void socket_event_4(void) |
| 71 | +{ |
| 72 | + event_fired[4] = true; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +size_t download_test(NetworkInterface *interface, const unsigned char *data, size_t data_length, size_t buff_size, uint32_t thread_id) |
| 77 | +{ |
| 78 | + int result = -1; |
| 79 | + |
| 80 | + TEST_ASSERT_MESSAGE(MAX_THREADS > thread_id, "Unsupported thread ID"); |
| 81 | + TEST_ASSERT_MESSAGE((MAX_THREADS * RECV_BUF_SIZE) >= buff_size, "Cannot test with the requested buffer size"); |
| 82 | + |
| 83 | + /* setup TCP socket */ |
| 84 | + TCPSocket tcpsocket(interface); |
| 85 | + |
| 86 | + for (int tries = 0; tries < MAX_RETRIES; tries++) { |
| 87 | + result = tcpsocket.connect(dl_host, 80); |
| 88 | + TEST_ASSERT_MESSAGE(result != NSAPI_ERROR_NO_SOCKET, "out of sockets"); |
| 89 | + |
| 90 | + if (result == 0) { |
| 91 | + break; |
| 92 | + } |
| 93 | + ThisThread::sleep_for(1000); |
| 94 | + printf("[NET-%d] Connection failed. Retry %d of %d\r\n", thread_id, tries, MAX_RETRIES); |
| 95 | + } |
| 96 | + TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "failed to connect"); |
| 97 | + |
| 98 | + if (thread_id == 0) { |
| 99 | + // technically this is non-threaded mode |
| 100 | + tcpsocket.sigio(socket_event_0); |
| 101 | + } else if (thread_id == 1) { |
| 102 | + tcpsocket.sigio(socket_event_1); |
| 103 | + } else if (thread_id == 2) { |
| 104 | + tcpsocket.sigio(socket_event_2); |
| 105 | + } else if (thread_id == 3) { |
| 106 | + tcpsocket.sigio(socket_event_3); |
| 107 | + } else if (thread_id == 4) { |
| 108 | + tcpsocket.sigio(socket_event_4); |
| 109 | + } else { |
| 110 | + TEST_ASSERT_MESSAGE(0, "wrong thread id"); |
| 111 | + } |
| 112 | + printf("[NET-%d] Registered socket callback function\r\n", thread_id); |
| 113 | + event_fired[thread_id] = false; |
| 114 | + |
| 115 | + /* setup request */ |
| 116 | + char *request = g_request_buffer[thread_id]; |
| 117 | + |
| 118 | + /* construct request */ |
| 119 | + size_t req_len = snprintf(request, REQ_BUF_SIZE - 1, req_template, dl_path, dl_host); |
| 120 | + request[req_len] = 0; |
| 121 | + printf("[NET-%d] Request header (%u): %s\r\n", thread_id, req_len, request); |
| 122 | + |
| 123 | + /* send request to server */ |
| 124 | + result = tcpsocket.send(request, req_len); |
| 125 | + TEST_ASSERT_EQUAL_INT_MESSAGE(req_len, result, "failed to send"); |
| 126 | + |
| 127 | + /* read response */ |
| 128 | + char *receive_buffer = &g_receive_buffer[thread_id * RECV_BUF_SIZE]; |
| 129 | + |
| 130 | + tcpsocket.set_blocking(false); |
| 131 | + printf("[NET-%d] Non-blocking socket mode set\r\n", thread_id); |
| 132 | + |
| 133 | + size_t received_bytes = 0; |
| 134 | + int body_index = -1; |
| 135 | + |
| 136 | + float speed; |
| 137 | + float percent; |
| 138 | + uint32_t time_left; |
| 139 | + Timer timer; |
| 140 | + timer.start(); |
| 141 | + |
| 142 | + /* loop until all expected bytes have been received */ |
| 143 | + while (received_bytes < data_length) { |
| 144 | + /* wait for async event */ |
| 145 | + while (!event_fired[thread_id]) { |
| 146 | + if (thread_id > 0) { |
| 147 | + ThisThread::yield(); |
| 148 | + } |
| 149 | + } |
| 150 | + event_fired[thread_id] = false; |
| 151 | + |
| 152 | + /* loop until all data has been read from socket */ |
| 153 | + do { |
| 154 | + result = tcpsocket.recv(receive_buffer, buff_size); |
| 155 | + TEST_ASSERT_MESSAGE((result == NSAPI_ERROR_WOULD_BLOCK) || (result >= 0), |
| 156 | + "failed to read socket"); |
| 157 | + |
| 158 | + if (result > 0) { |
| 159 | + /* skip HTTP header */ |
| 160 | + if (body_index < 0) { |
| 161 | + /* note that there are no required Response headers and their length may greatly vary */ |
| 162 | + std::string header(receive_buffer, result); |
| 163 | + body_index = header.find("\r\n\r\n"); |
| 164 | + if (body_index < 0) { |
| 165 | + continue; |
| 166 | + } else { |
| 167 | + printf("[NET-%d] Found body index: %d\r\n", thread_id, body_index); |
| 168 | + |
| 169 | + /* remove header before comparison */ |
| 170 | + memmove(receive_buffer, &receive_buffer[body_index + 4], result - body_index - 4); |
| 171 | + |
| 172 | + TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(data, receive_buffer, result - body_index - 4, |
| 173 | + "character mismatch in header"); |
| 174 | + |
| 175 | + received_bytes += (result - body_index - 4); |
| 176 | + } |
| 177 | + } else { |
| 178 | + TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(&data[received_bytes], receive_buffer, result, |
| 179 | + "character mismatch in body"); |
| 180 | + |
| 181 | + received_bytes += result; |
| 182 | + } |
| 183 | + |
| 184 | + speed = float(received_bytes) / timer.read(); |
| 185 | + percent = float(received_bytes) * 100 / float(data_length); |
| 186 | + time_left = (data_length - received_bytes) / speed; |
| 187 | + printf("[NET-%d] Received bytes: %u, (%.2f%%, %.2fKB/s, ETA: %02d:%02d:%02d)\r\n", |
| 188 | + thread_id, received_bytes, percent, speed / 1024, |
| 189 | + time_left / 3600, (time_left / 60) % 60, time_left % 60); |
| 190 | + } |
| 191 | + } while ((result > 0) && (received_bytes < data_length)); |
| 192 | + } |
| 193 | + |
| 194 | + TEST_ASSERT_MESSAGE(body_index >= 0, "failed to find body"); |
| 195 | + |
| 196 | + timer.stop(); |
| 197 | + float f_received_bytes = float(received_bytes); |
| 198 | + printf("[NET-%d] Downloaded: %.2fKB (%.2fKB/s, %.2f secs)\r\n", thread_id, |
| 199 | + f_received_bytes / 1024., |
| 200 | + f_received_bytes / (timer.read() * 1024.), |
| 201 | + timer.read()); |
| 202 | + |
| 203 | + return received_bytes; |
| 204 | +} |
| 205 | + |
0 commit comments