My take on Pico_Robustness_Test

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

Fork of Pico_Robustness_Test by TASS Belgium

Committer:
daniele
Date:
Thu Jul 25 09:37:32 2013 +0000
Revision:
2:6da40f9e8301
Parent:
1:97b1710fd9c3
Daniele's take on this test;

Who changed what in which revision?

UserRevisionLine numberNew 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
daniele 2:6da40f9e8301 11 #define READING_RETRIES 7u
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
daniele 2:6da40f9e8301 165
daniele 2:6da40f9e8301 166 ret = client.send_all(webStatistics[i].request,strlen(webStatistics[i].request));
daniele 2:6da40f9e8301 167 if (ret <= 0)
tass 0:8ac2e5a7f731 168 {
daniele 2:6da40f9e8301 169 printf("This test failed big time, ret=%d, err=%d!!\n", ret, pico_err);
daniele 2:6da40f9e8301 170 while(1);;;
daniele 2:6da40f9e8301 171 }
tass 0:8ac2e5a7f731 172
tass 0:8ac2e5a7f731 173 retries = 0;
tass 0:8ac2e5a7f731 174 // start reading
tass 0:8ac2e5a7f731 175 while(true)
tass 0:8ac2e5a7f731 176 {
daniele 2:6da40f9e8301 177 ret = client.receive(tmpBuffer,sizeof(tmpBuffer));
daniele 2:6da40f9e8301 178 if (ret == 0) {
daniele 2:6da40f9e8301 179 printf("Read timeout: ret = %d, err = %d, retry = %d\n", ret, pico_err, retries);
tass 0:8ac2e5a7f731 180 retries++;
daniele 2:6da40f9e8301 181 } else if (ret < 0) {
daniele 2:6da40f9e8301 182 if (pico_err != PICO_ERR_ESHUTDOWN) {
daniele 2:6da40f9e8301 183 printf("Read error, bailing out. Ret = %d, err = %d\n", ret, pico_err);
daniele 2:6da40f9e8301 184 while(1);;;
daniele 2:6da40f9e8301 185 }
daniele 2:6da40f9e8301 186 client.close();
daniele 2:6da40f9e8301 187 break;
daniele 2:6da40f9e8301 188 } else {
tass 0:8ac2e5a7f731 189 received += ret;
daniele 2:6da40f9e8301 190 }
tass 0:8ac2e5a7f731 191
daniele 2:6da40f9e8301 192 if(retries == READING_RETRIES) {
daniele 2:6da40f9e8301 193 printf("Read operation failed too many times. Giving up.\n");
daniele 2:6da40f9e8301 194 break;
daniele 2:6da40f9e8301 195 }
tass 0:8ac2e5a7f731 196 }
tass 0:8ac2e5a7f731 197
daniele 2:6da40f9e8301 198
daniele 2:6da40f9e8301 199
tass 0:8ac2e5a7f731 200 //Snapshot!
tass 0:8ac2e5a7f731 201 time = PICO_TIME_MS() - time;
tass 0:8ac2e5a7f731 202
tass 0:8ac2e5a7f731 203 if(webStatistics[i].maxReceived < received)
tass 0:8ac2e5a7f731 204 {
tass 0:8ac2e5a7f731 205 webStatistics[i].maxReceived = received;
tass 0:8ac2e5a7f731 206 }
tass 0:8ac2e5a7f731 207
tass 0:8ac2e5a7f731 208 if(webStatistics[i].minReceived > received)
tass 0:8ac2e5a7f731 209 {
tass 0:8ac2e5a7f731 210 webStatistics[i].minReceived = received;
tass 0:8ac2e5a7f731 211 }
tass 0:8ac2e5a7f731 212 printf("Received : %d bytes\n",received);
daniele 2:6da40f9e8301 213 if (received <= 0) {
daniele 2:6da40f9e8301 214 printf("Test FAILED!\n");
daniele 2:6da40f9e8301 215 while(1);;;
daniele 2:6da40f9e8301 216 }
tass 0:8ac2e5a7f731 217 webStatistics[i].avgDuration = ((webStatistics[i].avgDuration *(webStatistics[i].snapshots++) ) + time)/webStatistics[i].snapshots;
tass 0:8ac2e5a7f731 218 totalTime += time;
tass 1:97b1710fd9c3 219 memoryStamp();
tass 0:8ac2e5a7f731 220 }
tass 0:8ac2e5a7f731 221
tass 0:8ac2e5a7f731 222 printStatistics();
tass 0:8ac2e5a7f731 223 Thread::wait(500);
tass 0:8ac2e5a7f731 224 }
tass 0:8ac2e5a7f731 225 failure:
tass 0:8ac2e5a7f731 226 printf("Fatal error. Main thread is inactive\n");
tass 0:8ac2e5a7f731 227 while(1);
tass 0:8ac2e5a7f731 228 }