PicoTCP robustness Test
Dependencies: PicoTCP lpc1768-picotcp-eth mbed-rtos mbed
main.cpp@5:16d6db68e32f, 2014-04-09 (annotated)
- Committer:
- tass
- Date:
- Wed Apr 09 13:50:55 2014 +0000
- Revision:
- 5:16d6db68e32f
- Parent:
- 4:0406e4e7639f
Robustness test now continues on multiple read time-outs in staid of hanging forever. A more resent and robust PicoTCP version is used.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tass | 5:16d6db68e32f | 1 | /* |
tass | 5:16d6db68e32f | 2 | * This is an endurance test that downloads text, an image and a pdf file from the web in a loop using PicoTCP |
tass | 5:16d6db68e32f | 3 | * Debug output is printed to the USB serial port (what it is doing and memory stats) |
tass | 5:16d6db68e32f | 4 | */ |
tass | 0:8ac2e5a7f731 | 5 | #include "mbed.h" |
tass | 1:97b1710fd9c3 | 6 | #include <stdarg.h> |
tass | 0:8ac2e5a7f731 | 7 | #include "EthernetInterface.h" |
tass | 0:8ac2e5a7f731 | 8 | |
tass | 0:8ac2e5a7f731 | 9 | #define msgdbg(...) |
tass | 0:8ac2e5a7f731 | 10 | |
tass | 0:8ac2e5a7f731 | 11 | //#define msgdbg printf |
tass | 0:8ac2e5a7f731 | 12 | |
tass | 5:16d6db68e32f | 13 | #define SENDING_RETRIES 3 |
tass | 5:16d6db68e32f | 14 | #define READING_RETRIES 10 |
tass | 5:16d6db68e32f | 15 | #define DHCP_RETRIES 10 |
tass | 0:8ac2e5a7f731 | 16 | #define BUFFER_SIZE 256 |
tass | 0:8ac2e5a7f731 | 17 | |
tass | 5:16d6db68e32f | 18 | #define NUMBER_OF_HOSTS 3 |
tass | 0:8ac2e5a7f731 | 19 | |
tass | 5:16d6db68e32f | 20 | #define HOST_1 "www.mbed.org" |
tass | 5:16d6db68e32f | 21 | #define HOST_1_REQUEST "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n" |
tass | 0:8ac2e5a7f731 | 22 | |
tass | 5:16d6db68e32f | 23 | #define HOST_2 "www.mbed.org" |
tass | 5:16d6db68e32f | 24 | #define HOST_2_REQUEST "GET /media/img/boardlogos/lpc1768/pinout.png\nHost: www.mbed.org\n\n" |
tass | 0:8ac2e5a7f731 | 25 | |
tass | 5:16d6db68e32f | 26 | #define HOST_3 "www.nxp.com" |
tass | 5:16d6db68e32f | 27 | #define HOST_3_REQUEST "GET /documents/user_manual/UM10360.pdf HTTP/1.0\nHost: www.nxp.com\n\n" |
tass | 0:8ac2e5a7f731 | 28 | |
tass | 0:8ac2e5a7f731 | 29 | int totalTime = 0; |
tass | 0:8ac2e5a7f731 | 30 | |
tass | 0:8ac2e5a7f731 | 31 | struct WebStats |
tass | 0:8ac2e5a7f731 | 32 | { |
tass | 0:8ac2e5a7f731 | 33 | char request[70]; |
tass | 0:8ac2e5a7f731 | 34 | char host[20]; |
tass | 5:16d6db68e32f | 35 | int sumDuration; |
tass | 0:8ac2e5a7f731 | 36 | int maxReceived; |
tass | 0:8ac2e5a7f731 | 37 | int minReceived; |
tass | 0:8ac2e5a7f731 | 38 | int snapshots; |
tass | 0:8ac2e5a7f731 | 39 | }; |
tass | 0:8ac2e5a7f731 | 40 | |
tass | 5:16d6db68e32f | 41 | struct WebStats webStatistics[NUMBER_OF_HOSTS]= |
tass | 0:8ac2e5a7f731 | 42 | { |
tass | 5:16d6db68e32f | 43 | {HOST_1_REQUEST,HOST_1,0,0,0x7FFFFFFF,0}, |
tass | 5:16d6db68e32f | 44 | {HOST_2_REQUEST,HOST_2,0,0,0x7FFFFFFF,0}, |
tass | 5:16d6db68e32f | 45 | {HOST_3_REQUEST,HOST_3,0,0,0x7FFFFFFF,0} |
tass | 0:8ac2e5a7f731 | 46 | }; |
tass | 0:8ac2e5a7f731 | 47 | |
tass | 1:97b1710fd9c3 | 48 | void inline printMemoryStats(void); |
tass | 0:8ac2e5a7f731 | 49 | void printStatistics(void) |
tass | 0:8ac2e5a7f731 | 50 | { |
tass | 5:16d6db68e32f | 51 | printf("\nTotal Time : %f seconds\n",(float)totalTime/1000.0f); |
tass | 0:8ac2e5a7f731 | 52 | for(int i=0;i<NUMBER_OF_HOSTS;i++) |
tass | 0:8ac2e5a7f731 | 53 | { |
tass | 0:8ac2e5a7f731 | 54 | printf("Host number : %d | %s\n",i, webStatistics[i].host); |
tass | 5:16d6db68e32f | 55 | printf("MaxRecv : %d\n", webStatistics[i].maxReceived); |
tass | 5:16d6db68e32f | 56 | printf("MinRecv : %d\n", webStatistics[i].minReceived); |
tass | 5:16d6db68e32f | 57 | printf("Avg duration : %d ms\n", webStatistics[i].sumDuration / webStatistics[i].snapshots); |
tass | 0:8ac2e5a7f731 | 58 | printf("Total snapshots : %d\n\n",webStatistics[i].snapshots); |
tass | 0:8ac2e5a7f731 | 59 | } |
tass | 4:0406e4e7639f | 60 | printMemoryStats(); |
tass | 1:97b1710fd9c3 | 61 | } |
tass | 1:97b1710fd9c3 | 62 | |
tass | 1:97b1710fd9c3 | 63 | struct MemoryUsage |
tass | 1:97b1710fd9c3 | 64 | { |
tass | 1:97b1710fd9c3 | 65 | uint32_t curFreeRam; |
tass | 1:97b1710fd9c3 | 66 | uint32_t minFreeRam; |
tass | 1:97b1710fd9c3 | 67 | uint32_t maxFreeRam; |
tass | 1:97b1710fd9c3 | 68 | uint32_t avgFreeRam; |
tass | 1:97b1710fd9c3 | 69 | uint32_t stackPointer; |
tass | 1:97b1710fd9c3 | 70 | uint32_t cntSamples; |
tass | 1:97b1710fd9c3 | 71 | }; |
tass | 1:97b1710fd9c3 | 72 | |
tass | 1:97b1710fd9c3 | 73 | struct MemoryUsage memoryStats = {0,0xFFFFFFFF,0,0,0}; |
tass | 1:97b1710fd9c3 | 74 | |
tass | 1:97b1710fd9c3 | 75 | int fakePrintf(void* pvHeapInfo, char const* pFormatString, ...) |
tass | 1:97b1710fd9c3 | 76 | { |
tass | 1:97b1710fd9c3 | 77 | struct MemoryUsage * memStat = (struct MemoryUsage *)pvHeapInfo; |
tass | 1:97b1710fd9c3 | 78 | static const char* freeRamFormat = "%d bytes in"; |
tass | 1:97b1710fd9c3 | 79 | va_list valist; |
tass | 5:16d6db68e32f | 80 | |
tass | 1:97b1710fd9c3 | 81 | if(memcmp(pFormatString,freeRamFormat,strlen(freeRamFormat)) == 0) |
tass | 1:97b1710fd9c3 | 82 | { |
tass | 1:97b1710fd9c3 | 83 | va_start(valist, pFormatString); |
tass | 1:97b1710fd9c3 | 84 | unsigned long freeSize = va_arg(valist, unsigned long); |
tass | 1:97b1710fd9c3 | 85 | memStat->curFreeRam = freeSize; |
tass | 1:97b1710fd9c3 | 86 | if(memStat->minFreeRam > freeSize) |
tass | 1:97b1710fd9c3 | 87 | memStat->minFreeRam = freeSize; |
tass | 5:16d6db68e32f | 88 | |
tass | 1:97b1710fd9c3 | 89 | if(memStat->maxFreeRam < freeSize) |
tass | 1:97b1710fd9c3 | 90 | memStat->maxFreeRam = freeSize; |
tass | 5:16d6db68e32f | 91 | |
tass | 5:16d6db68e32f | 92 | memStat->avgFreeRam = ((memStat->avgFreeRam * memStat->cntSamples) + freeSize)/(memStat->cntSamples + 1); |
tass | 5:16d6db68e32f | 93 | memStat->cntSamples++; |
tass | 1:97b1710fd9c3 | 94 | } |
tass | 1:97b1710fd9c3 | 95 | else |
tass | 1:97b1710fd9c3 | 96 | { |
tass | 1:97b1710fd9c3 | 97 | // ignore format |
tass | 1:97b1710fd9c3 | 98 | } |
tass | 1:97b1710fd9c3 | 99 | return 0; |
tass | 0:8ac2e5a7f731 | 100 | } |
tass | 0:8ac2e5a7f731 | 101 | |
tass | 1:97b1710fd9c3 | 102 | void inline printMemoryStats(void) |
tass | 1:97b1710fd9c3 | 103 | { |
tass | 1:97b1710fd9c3 | 104 | if(memoryStats.cntSamples == 1) |
tass | 1:97b1710fd9c3 | 105 | printf("\n\n***** Initial Memory Report *****\n"); |
tass | 1:97b1710fd9c3 | 106 | else |
tass | 1:97b1710fd9c3 | 107 | printf("\n\n********* Memory Report *********\n"); |
tass | 5:16d6db68e32f | 108 | |
tass | 5:16d6db68e32f | 109 | printf("Current free memory : %d bytes\n", (int)memoryStats.curFreeRam); |
tass | 5:16d6db68e32f | 110 | printf("Maximum free memory : %d bytes\n", (int)memoryStats.maxFreeRam); |
tass | 5:16d6db68e32f | 111 | printf("Minimum free memory : %d bytes\n", (int)memoryStats.minFreeRam); |
tass | 5:16d6db68e32f | 112 | printf("Average free memory : %d bytes\n", (int)memoryStats.avgFreeRam); |
tass | 1:97b1710fd9c3 | 113 | printf("****************************\n"); |
tass | 1:97b1710fd9c3 | 114 | } |
tass | 1:97b1710fd9c3 | 115 | |
tass | 1:97b1710fd9c3 | 116 | void inline memoryStamp(void) |
tass | 1:97b1710fd9c3 | 117 | { |
tass | 1:97b1710fd9c3 | 118 | __heapstats((__heapprt)fakePrintf, &memoryStats); |
tass | 1:97b1710fd9c3 | 119 | } |
tass | 1:97b1710fd9c3 | 120 | |
tass | 5:16d6db68e32f | 121 | int main() { |
tass | 1:97b1710fd9c3 | 122 | |
tass | 0:8ac2e5a7f731 | 123 | int retries = 0; |
tass | 0:8ac2e5a7f731 | 124 | EthernetInterface eth; |
tass | 0:8ac2e5a7f731 | 125 | TCPSocketConnection client; |
tass | 5:16d6db68e32f | 126 | |
tass | 0:8ac2e5a7f731 | 127 | printf("Initialising...\n"); |
tass | 0:8ac2e5a7f731 | 128 | |
tass | 0:8ac2e5a7f731 | 129 | // use DHCP |
tass | 0:8ac2e5a7f731 | 130 | eth.init(); |
tass | 0:8ac2e5a7f731 | 131 | |
tass | 0:8ac2e5a7f731 | 132 | // attempt DHCP and if timing out then try again |
tass | 0:8ac2e5a7f731 | 133 | while (eth.connect()) { |
tass | 0:8ac2e5a7f731 | 134 | retries++; |
tass | 3:457043c6a3a6 | 135 | printf("[%llu] DHCP timeout %d\n",PICO_TIME_MS(),retries); |
tass | 0:8ac2e5a7f731 | 136 | if(retries >= DHCP_RETRIES) |
tass | 0:8ac2e5a7f731 | 137 | { |
tass | 0:8ac2e5a7f731 | 138 | printf("DHCP failed. Bailing out..\n"); |
tass | 0:8ac2e5a7f731 | 139 | goto failure; |
tass | 0:8ac2e5a7f731 | 140 | } |
tass | 0:8ac2e5a7f731 | 141 | }; |
tass | 5:16d6db68e32f | 142 | |
tass | 3:457043c6a3a6 | 143 | printf("[%llu] Starting the robustness test...\n",PICO_TIME_MS()); |
tass | 5:16d6db68e32f | 144 | |
tass | 5:16d6db68e32f | 145 | while(1) |
tass | 0:8ac2e5a7f731 | 146 | { |
tass | 5:16d6db68e32f | 147 | printf("Starting cycle ****************************************\n"); |
tass | 0:8ac2e5a7f731 | 148 | for(int i=0;i<NUMBER_OF_HOSTS;i++,client.close()) |
tass | 0:8ac2e5a7f731 | 149 | { |
tass | 0:8ac2e5a7f731 | 150 | int retries = 0; |
tass | 0:8ac2e5a7f731 | 151 | int received = 0; |
tass | 0:8ac2e5a7f731 | 152 | int time = 0; |
tass | 0:8ac2e5a7f731 | 153 | char tmpBuffer[BUFFER_SIZE]; |
tass | 0:8ac2e5a7f731 | 154 | int ret; |
tass | 5:16d6db68e32f | 155 | |
tass | 0:8ac2e5a7f731 | 156 | printf("Mbed --> %s\n",webStatistics[i].host); |
tass | 5:16d6db68e32f | 157 | |
tass | 0:8ac2e5a7f731 | 158 | time = PICO_TIME_MS(); |
tass | 0:8ac2e5a7f731 | 159 | // connecting |
tass | 0:8ac2e5a7f731 | 160 | if (client.connect(webStatistics[i].host, 80)) { |
tass | 0:8ac2e5a7f731 | 161 | printf("Failed to connect to : %s\n",webStatistics[i].host); |
tass | 0:8ac2e5a7f731 | 162 | continue; |
tass | 5:16d6db68e32f | 163 | } |
tass | 5:16d6db68e32f | 164 | |
tass | 5:16d6db68e32f | 165 | client.set_blocking(false, 8000); |
tass | 0:8ac2e5a7f731 | 166 | retries = 0; |
tass | 5:16d6db68e32f | 167 | |
tass | 0:8ac2e5a7f731 | 168 | // sending request |
tass | 2:c0838e12f038 | 169 | ret = client.send_all(webStatistics[i].request,strlen(webStatistics[i].request)); |
tass | 5:16d6db68e32f | 170 | if (ret <= 0) { |
tass | 2:c0838e12f038 | 171 | printf("This test failed big time, ret=%d, err=%d!!\n", ret, pico_err); |
tass | 2:c0838e12f038 | 172 | while(1);;; |
tass | 2:c0838e12f038 | 173 | } |
tass | 5:16d6db68e32f | 174 | |
tass | 0:8ac2e5a7f731 | 175 | retries = 0; |
tass | 0:8ac2e5a7f731 | 176 | // start reading |
tass | 0:8ac2e5a7f731 | 177 | while(true) |
tass | 0:8ac2e5a7f731 | 178 | { |
tass | 2:c0838e12f038 | 179 | ret = client.receive(tmpBuffer,sizeof(tmpBuffer)); |
tass | 2:c0838e12f038 | 180 | if (ret == 0) { |
tass | 2:c0838e12f038 | 181 | printf("Read timeout: ret = %d, err = %d, retry = %d\n", ret, pico_err, retries); |
tass | 0:8ac2e5a7f731 | 182 | retries++; |
tass | 2:c0838e12f038 | 183 | } else if (ret < 0) { |
tass | 2:c0838e12f038 | 184 | if (pico_err != PICO_ERR_ESHUTDOWN) { |
tass | 5:16d6db68e32f | 185 | printf("Read error, skipping. Ret = %d, err = %d\n", ret, pico_err); |
tass | 5:16d6db68e32f | 186 | break; |
tass | 2:c0838e12f038 | 187 | } |
tass | 2:c0838e12f038 | 188 | client.close(); |
tass | 2:c0838e12f038 | 189 | break; |
tass | 2:c0838e12f038 | 190 | } else { |
tass | 0:8ac2e5a7f731 | 191 | received += ret; |
tass | 2:c0838e12f038 | 192 | } |
tass | 5:16d6db68e32f | 193 | |
tass | 2:c0838e12f038 | 194 | if(retries == READING_RETRIES) { |
tass | 5:16d6db68e32f | 195 | printf("Read operation failed too many times: skipping.\n"); |
tass | 2:c0838e12f038 | 196 | break; |
tass | 5:16d6db68e32f | 197 | } |
tass | 0:8ac2e5a7f731 | 198 | } |
tass | 5:16d6db68e32f | 199 | |
tass | 0:8ac2e5a7f731 | 200 | //Snapshot! |
tass | 0:8ac2e5a7f731 | 201 | time = PICO_TIME_MS() - time; |
tass | 5:16d6db68e32f | 202 | |
tass | 0:8ac2e5a7f731 | 203 | if(webStatistics[i].maxReceived < received) |
tass | 0:8ac2e5a7f731 | 204 | webStatistics[i].maxReceived = received; |
tass | 5:16d6db68e32f | 205 | |
tass | 0:8ac2e5a7f731 | 206 | if(webStatistics[i].minReceived > received) |
tass | 0:8ac2e5a7f731 | 207 | webStatistics[i].minReceived = received; |
tass | 5:16d6db68e32f | 208 | |
tass | 0:8ac2e5a7f731 | 209 | printf("Received : %d bytes\n",received); |
tass | 5:16d6db68e32f | 210 | webStatistics[i].sumDuration += time; |
tass | 5:16d6db68e32f | 211 | webStatistics[i].snapshots++; |
tass | 0:8ac2e5a7f731 | 212 | totalTime += time; |
tass | 1:97b1710fd9c3 | 213 | memoryStamp(); |
tass | 0:8ac2e5a7f731 | 214 | } |
tass | 5:16d6db68e32f | 215 | |
tass | 0:8ac2e5a7f731 | 216 | printStatistics(); |
tass | 0:8ac2e5a7f731 | 217 | Thread::wait(500); |
tass | 0:8ac2e5a7f731 | 218 | } |
tass | 5:16d6db68e32f | 219 | failure: |
tass | 0:8ac2e5a7f731 | 220 | printf("Fatal error. Main thread is inactive\n"); |
tass | 0:8ac2e5a7f731 | 221 | while(1); |
tass | 0:8ac2e5a7f731 | 222 | } |
tass | 5:16d6db68e32f | 223 |