Picotcp memory usage benchmark (still beta testing)
Dependencies: PicoTCP lpc1768-picotcp-eth mbed-rtos mbed
This project represents a throughtput test for the EthernetInterface, based on the PicoTCP library.
The purpose of this is to be able to make a comparison of full speed (Rx,Tx and Rx+Tx) and memory
between the EthernetInterface based on the lwIP and the one based on PicoTCP .
The benchmark consists of three parts:
- we are sending 1 Megabyte from the board to host
- we are waiting for the host to send us 1 Megabyte of data
- on the last part we are using a TCP Echo Server to send and receive back 1 Megabyte of data
we send/receive 1024 packets of 1024 bytes each and we measure how many bits per second we transmit and receive.
The result:
TX+RX Throughput: 4194304 bytes in 3 seconds (10.457) Mbits/s
Memory report
Current free memory : 19148 bytes
Maximum free memory : 24716 bytes
Minimum free memory : 15476 bytes
Average free memory : 17376 bytes
The python script used
#!/usr/bin/python import sys, socket import random, string import select from time import time #ECHO_SERVER_ADDRESS = "10.20.30.139" ECHO_SERVER_ADDRESS = "192.168.100.12" ECHO_SERVER_PORT = 7 N_PACKETS = 1024 LEN_PACKET = 1024 TOT_BITS = (LEN_PACKET * N_PACKETS * 8) * 4. # TX bits + RX bits PACKET = ''.join(random.choice(string.ascii_uppercase+string.digits) for _ in range(LEN_PACKET)) MEGA = 1024 * 1024. UPDATE_STEP = (N_PACKETS/20) # Make the update step such as one step = 5% UPDATE_R_STEP = (N_PACKETS/20) #Make the update step such as one step = 1% s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT)) start = time() rx_total = 0 tx_total = 0 data = '' i = 0 lp = [] p = select.poll() while(True): # keeping this in for robustness testing, when the test will run for days #Stage I - sending data from host towards mbed board print "Started sending data...." while (i < N_PACKETS): p.register(s, select.POLLOUT) lp = p.poll(1) if (len(lp) > 0): s.sendall(PACKET) i+=1 if (i % UPDATE_STEP) == 0: print "sent %d/%d (%.2f%%)" % (i, N_PACKETS, float(i)/ float(N_PACKETS) * 100.) continue #Stage II - receving data from the mbed board print "Started receiving data...." while(len(data) < N_PACKETS * LEN_PACKET): p.register(s, select.POLLIN) lp = p.poll(1) if(len(lp) > 0): r = s.recv(LEN_PACKET) if (r != ''): data += r if (len(data) / LEN_PACKET) % UPDATE_R_STEP: print "recvd %d/%d (%.2f%%)" % (len(data)/LEN_PACKET, N_PACKETS, float(len(data))/ float(N_PACKETS * LEN_PACKET) * 100.) else: print "Error receiving !" sys.exit(1) #Stage III - echo between host and mbed board print "Started echoing data...." for i in range(N_PACKETS): if (i % UPDATE_STEP) == 0: print "%.2f%%" % (float(i)/float(N_PACKETS) * 100.) s.sendall(PACKET) data = s.recv(LEN_PACKET) if (len(data) != LEN_PACKET): print "Error echoing !" sys.exit(1) print "Test was finished!" break; t = time() - start s.close() print "TX+RX Throughput: %d bytes in %d seconds (%.3f)Mbits/s" % (TOT_BITS / 8, t, ((TOT_BITS / t) / MEGA))
This test is based on the following libraries
Import librarylpc1768-picotcp-eth
A PicoTCP driver for the lpc1768 mbed board
Import libraryPicoTCP
Free (GPLv2) TCP/IP stack developed by TASS Belgium
Import librarymbed-rtos
Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.
Click here to see the results for lwIP
main.cpp@1:f014c48d0517, 2013-07-09 (annotated)
- Committer:
- tass
- Date:
- Tue Jul 09 06:15:37 2013 +0000
- Revision:
- 1:f014c48d0517
- Parent:
- 0:ebcbe44e53dd
- Child:
- 2:59703c8ca07e
Created function that prints memory report.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tass | 0:ebcbe44e53dd | 1 | #include <mbed.h> |
tass | 0:ebcbe44e53dd | 2 | #include <stdarg.h> |
tass | 0:ebcbe44e53dd | 3 | #include "EthernetInterface.h" |
tass | 0:ebcbe44e53dd | 4 | |
tass | 0:ebcbe44e53dd | 5 | #define ECHO_SERVER_PORT 7 |
tass | 0:ebcbe44e53dd | 6 | #define BUFFER_QUANTITY (1024*1024) |
tass | 0:ebcbe44e53dd | 7 | |
tass | 0:ebcbe44e53dd | 8 | struct MemoryUsage |
tass | 0:ebcbe44e53dd | 9 | { |
tass | 0:ebcbe44e53dd | 10 | uint32_t minFreeRam; |
tass | 0:ebcbe44e53dd | 11 | uint32_t maxFreeRam; |
tass | 0:ebcbe44e53dd | 12 | uint32_t avgFreeRam; |
tass | 0:ebcbe44e53dd | 13 | uint32_t stackPointer; |
tass | 0:ebcbe44e53dd | 14 | uint32_t cntSamples; |
tass | 0:ebcbe44e53dd | 15 | }; |
tass | 0:ebcbe44e53dd | 16 | |
tass | 0:ebcbe44e53dd | 17 | struct MemoryUsage memoryStats = {0xFFFFFFFF,0,0,0}; |
tass | 0:ebcbe44e53dd | 18 | |
tass | 0:ebcbe44e53dd | 19 | int fakePrintf(void* pvHeapInfo, char const* pFormatString, ...) |
tass | 0:ebcbe44e53dd | 20 | { |
tass | 0:ebcbe44e53dd | 21 | struct MemoryUsage * memStat = (struct MemoryUsage *)pvHeapInfo; |
tass | 0:ebcbe44e53dd | 22 | static const char* freeRamFormat = "%d bytes in"; |
tass | 0:ebcbe44e53dd | 23 | va_list valist; |
tass | 0:ebcbe44e53dd | 24 | |
tass | 0:ebcbe44e53dd | 25 | if(memcmp(pFormatString,freeRamFormat,strlen(freeRamFormat)) == 0) |
tass | 0:ebcbe44e53dd | 26 | { |
tass | 0:ebcbe44e53dd | 27 | va_start(valist, pFormatString); |
tass | 0:ebcbe44e53dd | 28 | unsigned long freeSize = va_arg(valist, unsigned long); |
tass | 0:ebcbe44e53dd | 29 | |
tass | 0:ebcbe44e53dd | 30 | if(memStat->minFreeRam > freeSize) |
tass | 0:ebcbe44e53dd | 31 | memStat->minFreeRam = freeSize; |
tass | 0:ebcbe44e53dd | 32 | |
tass | 0:ebcbe44e53dd | 33 | if(memStat->maxFreeRam < freeSize) |
tass | 0:ebcbe44e53dd | 34 | memStat->maxFreeRam = freeSize; |
tass | 0:ebcbe44e53dd | 35 | |
tass | 0:ebcbe44e53dd | 36 | memStat->avgFreeRam = ((memStat->avgFreeRam * memStat->cntSamples) + freeSize)/(++memStat->cntSamples); |
tass | 0:ebcbe44e53dd | 37 | } |
tass | 0:ebcbe44e53dd | 38 | else |
tass | 0:ebcbe44e53dd | 39 | { |
tass | 0:ebcbe44e53dd | 40 | // ignore format |
tass | 0:ebcbe44e53dd | 41 | } |
tass | 0:ebcbe44e53dd | 42 | return 0; |
tass | 0:ebcbe44e53dd | 43 | } |
tass | 0:ebcbe44e53dd | 44 | |
tass | 1:f014c48d0517 | 45 | void inline printMemoryStats(void) |
tass | 1:f014c48d0517 | 46 | { |
tass | 1:f014c48d0517 | 47 | if(memoryStats.cntSamples == 1) |
tass | 1:f014c48d0517 | 48 | printf("\n\n***** Initial Memory Report *****\n"); |
tass | 1:f014c48d0517 | 49 | else |
tass | 1:f014c48d0517 | 50 | printf("\n\n********* Memory Report *********\n"); |
tass | 1:f014c48d0517 | 51 | |
tass | 1:f014c48d0517 | 52 | printf("Maximum free memory : %d bytes\n",memoryStats.maxFreeRam); |
tass | 1:f014c48d0517 | 53 | printf("Minimum free memory : %d bytes\n",memoryStats.minFreeRam); |
tass | 1:f014c48d0517 | 54 | printf("Average free memory : %d\n",memoryStats.avgFreeRam); |
tass | 1:f014c48d0517 | 55 | printf("Stack pointer address : %d\n",memoryStats.stackPointer); |
tass | 1:f014c48d0517 | 56 | printf("****************************\n"); |
tass | 1:f014c48d0517 | 57 | } |
tass | 1:f014c48d0517 | 58 | |
tass | 0:ebcbe44e53dd | 59 | void inline memoryStamp(void) |
tass | 0:ebcbe44e53dd | 60 | { |
tass | 0:ebcbe44e53dd | 61 | __heapstats((__heapprt)fakePrintf, &memoryStats); |
tass | 0:ebcbe44e53dd | 62 | } |
tass | 0:ebcbe44e53dd | 63 | |
tass | 0:ebcbe44e53dd | 64 | void inline stackPtrSnapshot(void) |
tass | 0:ebcbe44e53dd | 65 | { |
tass | 0:ebcbe44e53dd | 66 | memoryStats.stackPointer = __current_sp(); |
tass | 0:ebcbe44e53dd | 67 | } |
tass | 0:ebcbe44e53dd | 68 | |
tass | 0:ebcbe44e53dd | 69 | int main() { |
tass | 0:ebcbe44e53dd | 70 | |
tass | 0:ebcbe44e53dd | 71 | printf("Ethernet Interface memory test....\n"); |
tass | 1:f014c48d0517 | 72 | |
tass | 1:f014c48d0517 | 73 | // Initial memory status |
tass | 1:f014c48d0517 | 74 | memoryStamp(); |
tass | 0:ebcbe44e53dd | 75 | stackPtrSnapshot(); // snapshot of the stack pointer before starting benchmark to see the SP. |
tass | 1:f014c48d0517 | 76 | printMemoryStats(); |
tass | 1:f014c48d0517 | 77 | |
tass | 0:ebcbe44e53dd | 78 | EthernetInterface eth; |
tass | 0:ebcbe44e53dd | 79 | int connections = 0; |
tass | 0:ebcbe44e53dd | 80 | eth.init(); //Use DHCP |
tass | 0:ebcbe44e53dd | 81 | eth.connect(); |
tass | 0:ebcbe44e53dd | 82 | printf("IP Address %s\n", eth.getIPAddress()); |
tass | 0:ebcbe44e53dd | 83 | |
tass | 0:ebcbe44e53dd | 84 | TCPSocketServer server; |
tass | 0:ebcbe44e53dd | 85 | server.bind(ECHO_SERVER_PORT); |
tass | 0:ebcbe44e53dd | 86 | server.listen(); |
tass | 0:ebcbe44e53dd | 87 | |
tass | 0:ebcbe44e53dd | 88 | while (true) { |
tass | 0:ebcbe44e53dd | 89 | printf("\nWait for new connection...\n"); |
tass | 0:ebcbe44e53dd | 90 | printf("Client number %d\n",++connections); |
tass | 0:ebcbe44e53dd | 91 | TCPSocketConnection client; |
tass | 0:ebcbe44e53dd | 92 | server.accept(client); |
tass | 0:ebcbe44e53dd | 93 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
tass | 0:ebcbe44e53dd | 94 | printf("Connection from: %s\n", client.get_address()); |
tass | 0:ebcbe44e53dd | 95 | char buffer[1024]; |
tass | 0:ebcbe44e53dd | 96 | while (true) { |
tass | 0:ebcbe44e53dd | 97 | |
tass | 0:ebcbe44e53dd | 98 | int dataReceived = 0; |
tass | 0:ebcbe44e53dd | 99 | int dataSent = 0; |
tass | 0:ebcbe44e53dd | 100 | |
tass | 0:ebcbe44e53dd | 101 | printf("\n\n\nStarting the receiving part...\n"); |
tass | 0:ebcbe44e53dd | 102 | while(dataReceived < BUFFER_QUANTITY) |
tass | 0:ebcbe44e53dd | 103 | { |
tass | 0:ebcbe44e53dd | 104 | int n = client.receive(buffer, sizeof(buffer)); |
tass | 0:ebcbe44e53dd | 105 | if (n <= 0) { |
tass | 0:ebcbe44e53dd | 106 | printf("Receive error\n"); |
tass | 0:ebcbe44e53dd | 107 | break; |
tass | 0:ebcbe44e53dd | 108 | } |
tass | 0:ebcbe44e53dd | 109 | dataReceived += n; |
tass | 0:ebcbe44e53dd | 110 | memoryStamp(); |
tass | 0:ebcbe44e53dd | 111 | } |
tass | 0:ebcbe44e53dd | 112 | |
tass | 0:ebcbe44e53dd | 113 | |
tass | 0:ebcbe44e53dd | 114 | printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY); |
tass | 0:ebcbe44e53dd | 115 | if(dataReceived < BUFFER_QUANTITY) |
tass | 0:ebcbe44e53dd | 116 | { |
tass | 0:ebcbe44e53dd | 117 | printf("Receiving part of the test has failed. Exiting connection.\n"); |
tass | 0:ebcbe44e53dd | 118 | break; |
tass | 0:ebcbe44e53dd | 119 | } |
tass | 0:ebcbe44e53dd | 120 | else{ |
tass | 0:ebcbe44e53dd | 121 | printf("Receiving has passed...\n"); |
tass | 0:ebcbe44e53dd | 122 | } |
tass | 0:ebcbe44e53dd | 123 | |
tass | 0:ebcbe44e53dd | 124 | printf("\n\n\nStarting the sending part...\n"); |
tass | 0:ebcbe44e53dd | 125 | while(dataSent < BUFFER_QUANTITY) |
tass | 0:ebcbe44e53dd | 126 | { |
tass | 0:ebcbe44e53dd | 127 | int n = client.send_all(buffer, sizeof(buffer)); |
tass | 0:ebcbe44e53dd | 128 | if (n <= 0) { |
tass | 0:ebcbe44e53dd | 129 | printf("Send error\n"); |
tass | 0:ebcbe44e53dd | 130 | break; |
tass | 0:ebcbe44e53dd | 131 | } |
tass | 0:ebcbe44e53dd | 132 | dataSent += n; |
tass | 0:ebcbe44e53dd | 133 | memoryStamp(); |
tass | 0:ebcbe44e53dd | 134 | } |
tass | 0:ebcbe44e53dd | 135 | |
tass | 0:ebcbe44e53dd | 136 | printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY); |
tass | 0:ebcbe44e53dd | 137 | if(dataSent < BUFFER_QUANTITY) |
tass | 0:ebcbe44e53dd | 138 | { |
tass | 0:ebcbe44e53dd | 139 | printf("Sending part of the test has failed. Exiting connection.\n"); |
tass | 0:ebcbe44e53dd | 140 | break; |
tass | 0:ebcbe44e53dd | 141 | } |
tass | 0:ebcbe44e53dd | 142 | else |
tass | 0:ebcbe44e53dd | 143 | { |
tass | 0:ebcbe44e53dd | 144 | printf("Sending test has passed...\n"); |
tass | 0:ebcbe44e53dd | 145 | } |
tass | 0:ebcbe44e53dd | 146 | |
tass | 0:ebcbe44e53dd | 147 | |
tass | 0:ebcbe44e53dd | 148 | printf("\n\n\nStarting echo part...\n"); |
tass | 0:ebcbe44e53dd | 149 | dataReceived = dataSent = 0; |
tass | 0:ebcbe44e53dd | 150 | while((dataReceived+dataSent) < 2*BUFFER_QUANTITY) |
tass | 0:ebcbe44e53dd | 151 | { |
tass | 0:ebcbe44e53dd | 152 | int n = client.receive(buffer, sizeof(buffer)); |
tass | 0:ebcbe44e53dd | 153 | if (n <= 0) { |
tass | 0:ebcbe44e53dd | 154 | printf("Receive error\n"); |
tass | 0:ebcbe44e53dd | 155 | break; |
tass | 0:ebcbe44e53dd | 156 | } |
tass | 0:ebcbe44e53dd | 157 | dataReceived += n; |
tass | 0:ebcbe44e53dd | 158 | |
tass | 0:ebcbe44e53dd | 159 | n = client.send_all(buffer, n); |
tass | 0:ebcbe44e53dd | 160 | if (n <= 0) { |
tass | 0:ebcbe44e53dd | 161 | printf("Send error\n"); |
tass | 0:ebcbe44e53dd | 162 | break; |
tass | 0:ebcbe44e53dd | 163 | } |
tass | 0:ebcbe44e53dd | 164 | dataSent += n; |
tass | 0:ebcbe44e53dd | 165 | memoryStamp(); |
tass | 0:ebcbe44e53dd | 166 | } |
tass | 0:ebcbe44e53dd | 167 | |
tass | 0:ebcbe44e53dd | 168 | printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY); |
tass | 0:ebcbe44e53dd | 169 | if((dataReceived+dataSent) < 2*BUFFER_QUANTITY) |
tass | 0:ebcbe44e53dd | 170 | { |
tass | 0:ebcbe44e53dd | 171 | printf("Echo test has failed.Exiting connection...\n"); |
tass | 0:ebcbe44e53dd | 172 | break; |
tass | 0:ebcbe44e53dd | 173 | } |
tass | 0:ebcbe44e53dd | 174 | else |
tass | 0:ebcbe44e53dd | 175 | { |
tass | 0:ebcbe44e53dd | 176 | printf("Echo test has passed...\n"); |
tass | 0:ebcbe44e53dd | 177 | } |
tass | 0:ebcbe44e53dd | 178 | } |
tass | 0:ebcbe44e53dd | 179 | client.close(); |
tass | 1:f014c48d0517 | 180 | printf("Test was finished...\n"); |
tass | 1:f014c48d0517 | 181 | printMemoryStats(); |
tass | 0:ebcbe44e53dd | 182 | } |
tass | 0:ebcbe44e53dd | 183 | |
tass | 0:ebcbe44e53dd | 184 | return 0; |
tass | 0:ebcbe44e53dd | 185 | } |