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