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

Committer:
tass
Date:
Fri Sep 06 10:11:46 2013 +0000
Revision:
5:7a73a14f5e85
Parent:
3:8892f28e2e58
Child:
6:e7d71e575d37
Updated to the latest libraries.

Who changed what in which revision?

UserRevisionLine numberNew 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 2:59703c8ca07e 10 uint32_t curFreeRam;
tass 0:ebcbe44e53dd 11 uint32_t minFreeRam;
tass 0:ebcbe44e53dd 12 uint32_t maxFreeRam;
tass 0:ebcbe44e53dd 13 uint32_t avgFreeRam;
tass 0:ebcbe44e53dd 14 uint32_t stackPointer;
tass 0:ebcbe44e53dd 15 uint32_t cntSamples;
tass 0:ebcbe44e53dd 16 };
tass 0:ebcbe44e53dd 17
tass 2:59703c8ca07e 18 struct MemoryUsage memoryStats = {0,0xFFFFFFFF,0,0,0};
tass 0:ebcbe44e53dd 19
tass 0:ebcbe44e53dd 20 int fakePrintf(void* pvHeapInfo, char const* pFormatString, ...)
tass 0:ebcbe44e53dd 21 {
tass 0:ebcbe44e53dd 22 struct MemoryUsage * memStat = (struct MemoryUsage *)pvHeapInfo;
tass 0:ebcbe44e53dd 23 static const char* freeRamFormat = "%d bytes in";
tass 0:ebcbe44e53dd 24 va_list valist;
tass 0:ebcbe44e53dd 25
tass 0:ebcbe44e53dd 26 if(memcmp(pFormatString,freeRamFormat,strlen(freeRamFormat)) == 0)
tass 0:ebcbe44e53dd 27 {
tass 0:ebcbe44e53dd 28 va_start(valist, pFormatString);
tass 0:ebcbe44e53dd 29 unsigned long freeSize = va_arg(valist, unsigned long);
tass 2:59703c8ca07e 30 memStat->curFreeRam = freeSize;
tass 0:ebcbe44e53dd 31 if(memStat->minFreeRam > freeSize)
tass 0:ebcbe44e53dd 32 memStat->minFreeRam = freeSize;
tass 0:ebcbe44e53dd 33
tass 0:ebcbe44e53dd 34 if(memStat->maxFreeRam < freeSize)
tass 0:ebcbe44e53dd 35 memStat->maxFreeRam = freeSize;
tass 0:ebcbe44e53dd 36
tass 0:ebcbe44e53dd 37 memStat->avgFreeRam = ((memStat->avgFreeRam * memStat->cntSamples) + freeSize)/(++memStat->cntSamples);
tass 0:ebcbe44e53dd 38 }
tass 0:ebcbe44e53dd 39 else
tass 0:ebcbe44e53dd 40 {
tass 0:ebcbe44e53dd 41 // ignore format
tass 0:ebcbe44e53dd 42 }
tass 0:ebcbe44e53dd 43 return 0;
tass 0:ebcbe44e53dd 44 }
tass 0:ebcbe44e53dd 45
tass 1:f014c48d0517 46 void inline printMemoryStats(void)
tass 1:f014c48d0517 47 {
tass 1:f014c48d0517 48 if(memoryStats.cntSamples == 1)
tass 1:f014c48d0517 49 printf("\n\n***** Initial Memory Report *****\n");
tass 1:f014c48d0517 50 else
tass 1:f014c48d0517 51 printf("\n\n********* Memory Report *********\n");
tass 1:f014c48d0517 52
tass 2:59703c8ca07e 53 printf("Current free memory : %d bytes\n",memoryStats.curFreeRam);
tass 1:f014c48d0517 54 printf("Maximum free memory : %d bytes\n",memoryStats.maxFreeRam);
tass 1:f014c48d0517 55 printf("Minimum free memory : %d bytes\n",memoryStats.minFreeRam);
tass 2:59703c8ca07e 56 printf("Average free memory : %d bytes\n",memoryStats.avgFreeRam);
tass 1:f014c48d0517 57 printf("Stack pointer address : %d\n",memoryStats.stackPointer);
tass 1:f014c48d0517 58 printf("****************************\n");
tass 1:f014c48d0517 59 }
tass 1:f014c48d0517 60
tass 0:ebcbe44e53dd 61 void inline memoryStamp(void)
tass 0:ebcbe44e53dd 62 {
tass 0:ebcbe44e53dd 63 __heapstats((__heapprt)fakePrintf, &memoryStats);
tass 0:ebcbe44e53dd 64 }
tass 0:ebcbe44e53dd 65
tass 0:ebcbe44e53dd 66 void inline stackPtrSnapshot(void)
tass 0:ebcbe44e53dd 67 {
tass 0:ebcbe44e53dd 68 memoryStats.stackPointer = __current_sp();
tass 0:ebcbe44e53dd 69 }
tass 0:ebcbe44e53dd 70
tass 0:ebcbe44e53dd 71 int main() {
tass 0:ebcbe44e53dd 72
tass 0:ebcbe44e53dd 73 printf("Ethernet Interface memory test....\n");
tass 1:f014c48d0517 74
tass 1:f014c48d0517 75 // Initial memory status
tass 1:f014c48d0517 76 memoryStamp();
tass 0:ebcbe44e53dd 77 stackPtrSnapshot(); // snapshot of the stack pointer before starting benchmark to see the SP.
tass 1:f014c48d0517 78 printMemoryStats();
tass 1:f014c48d0517 79
tass 0:ebcbe44e53dd 80 EthernetInterface eth;
tass 0:ebcbe44e53dd 81 int connections = 0;
tass 5:7a73a14f5e85 82 //eth.init("192.168.100.12","255.255.255.0","192.168.100.2"); //Use DHCP
tass 5:7a73a14f5e85 83 eth.init();
tass 0:ebcbe44e53dd 84 eth.connect();
tass 0:ebcbe44e53dd 85 printf("IP Address %s\n", eth.getIPAddress());
tass 0:ebcbe44e53dd 86
tass 0:ebcbe44e53dd 87 TCPSocketServer server;
tass 0:ebcbe44e53dd 88 server.bind(ECHO_SERVER_PORT);
tass 0:ebcbe44e53dd 89 server.listen();
tass 0:ebcbe44e53dd 90
tass 0:ebcbe44e53dd 91 while (true) {
tass 0:ebcbe44e53dd 92 printf("\nWait for new connection...\n");
tass 0:ebcbe44e53dd 93 printf("Client number %d\n",++connections);
tass 0:ebcbe44e53dd 94 TCPSocketConnection client;
tass 0:ebcbe44e53dd 95 server.accept(client);
tass 0:ebcbe44e53dd 96 client.set_blocking(false, 1500); // Timeout after (1.5)s
tass 0:ebcbe44e53dd 97 printf("Connection from: %s\n", client.get_address());
tass 0:ebcbe44e53dd 98 char buffer[1024];
tass 0:ebcbe44e53dd 99 while (true) {
tass 0:ebcbe44e53dd 100
tass 0:ebcbe44e53dd 101 int dataReceived = 0;
tass 0:ebcbe44e53dd 102 int dataSent = 0;
tass 0:ebcbe44e53dd 103
tass 0:ebcbe44e53dd 104 printf("\n\n\nStarting the receiving part...\n");
tass 0:ebcbe44e53dd 105 while(dataReceived < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 106 {
tass 0:ebcbe44e53dd 107 int n = client.receive(buffer, sizeof(buffer));
tass 0:ebcbe44e53dd 108 if (n <= 0) {
tass 0:ebcbe44e53dd 109 printf("Receive error\n");
tass 0:ebcbe44e53dd 110 break;
tass 0:ebcbe44e53dd 111 }
tass 0:ebcbe44e53dd 112 dataReceived += n;
tass 0:ebcbe44e53dd 113 memoryStamp();
tass 0:ebcbe44e53dd 114 }
tass 0:ebcbe44e53dd 115
tass 0:ebcbe44e53dd 116
tass 0:ebcbe44e53dd 117 printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY);
tass 0:ebcbe44e53dd 118 if(dataReceived < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 119 {
tass 0:ebcbe44e53dd 120 printf("Receiving part of the test has failed. Exiting connection.\n");
tass 0:ebcbe44e53dd 121 break;
tass 0:ebcbe44e53dd 122 }
tass 0:ebcbe44e53dd 123 else{
tass 0:ebcbe44e53dd 124 printf("Receiving has passed...\n");
tass 0:ebcbe44e53dd 125 }
tass 0:ebcbe44e53dd 126
tass 0:ebcbe44e53dd 127 printf("\n\n\nStarting the sending part...\n");
tass 0:ebcbe44e53dd 128 while(dataSent < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 129 {
tass 0:ebcbe44e53dd 130 int n = client.send_all(buffer, sizeof(buffer));
tass 0:ebcbe44e53dd 131 if (n <= 0) {
tass 0:ebcbe44e53dd 132 printf("Send error\n");
tass 0:ebcbe44e53dd 133 break;
tass 0:ebcbe44e53dd 134 }
tass 0:ebcbe44e53dd 135 dataSent += n;
tass 0:ebcbe44e53dd 136 memoryStamp();
tass 0:ebcbe44e53dd 137 }
tass 0:ebcbe44e53dd 138
tass 0:ebcbe44e53dd 139 printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY);
tass 0:ebcbe44e53dd 140 if(dataSent < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 141 {
tass 0:ebcbe44e53dd 142 printf("Sending part of the test has failed. Exiting connection.\n");
tass 0:ebcbe44e53dd 143 break;
tass 0:ebcbe44e53dd 144 }
tass 0:ebcbe44e53dd 145 else
tass 0:ebcbe44e53dd 146 {
tass 0:ebcbe44e53dd 147 printf("Sending test has passed...\n");
tass 0:ebcbe44e53dd 148 }
tass 0:ebcbe44e53dd 149
tass 0:ebcbe44e53dd 150
tass 0:ebcbe44e53dd 151 printf("\n\n\nStarting echo part...\n");
tass 0:ebcbe44e53dd 152 dataReceived = dataSent = 0;
tass 0:ebcbe44e53dd 153 while((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 154 {
tass 0:ebcbe44e53dd 155 int n = client.receive(buffer, sizeof(buffer));
tass 0:ebcbe44e53dd 156 if (n <= 0) {
tass 0:ebcbe44e53dd 157 printf("Receive error\n");
tass 0:ebcbe44e53dd 158 break;
tass 0:ebcbe44e53dd 159 }
tass 0:ebcbe44e53dd 160 dataReceived += n;
tass 0:ebcbe44e53dd 161
tass 0:ebcbe44e53dd 162 n = client.send_all(buffer, n);
tass 0:ebcbe44e53dd 163 if (n <= 0) {
tass 0:ebcbe44e53dd 164 printf("Send error\n");
tass 0:ebcbe44e53dd 165 break;
tass 0:ebcbe44e53dd 166 }
tass 0:ebcbe44e53dd 167 dataSent += n;
tass 0:ebcbe44e53dd 168 memoryStamp();
tass 0:ebcbe44e53dd 169 }
tass 0:ebcbe44e53dd 170
tass 0:ebcbe44e53dd 171 printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY);
tass 0:ebcbe44e53dd 172 if((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 173 {
tass 0:ebcbe44e53dd 174 printf("Echo test has failed.Exiting connection...\n");
tass 0:ebcbe44e53dd 175 }
tass 0:ebcbe44e53dd 176 else
tass 0:ebcbe44e53dd 177 {
tass 0:ebcbe44e53dd 178 printf("Echo test has passed...\n");
tass 0:ebcbe44e53dd 179 }
tass 3:8892f28e2e58 180 break;
tass 0:ebcbe44e53dd 181 }
tass 0:ebcbe44e53dd 182 client.close();
tass 1:f014c48d0517 183 printf("Test was finished...\n");
tass 1:f014c48d0517 184 printMemoryStats();
tass 0:ebcbe44e53dd 185 }
tass 0:ebcbe44e53dd 186
tass 0:ebcbe44e53dd 187 return 0;
tass 0:ebcbe44e53dd 188 }