lwip memory and full speed throughput test.

Dependencies:   EthernetInterface mbed-rtos mbed

This project represents a throughtput test for the EthernetInterface, based on the lwIP 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 41 seconds (0.774) Mbits/s

Memory report
Current free memory : 22644 bytes
Maximum free memory : 24716 bytes
Minimum free memory : 22644 bytes
Average free memory : 22644 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 librarymbed-rtos

Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Import libraryEthernetInterface

mbed IP library over Ethernet

Click here to see the results for PicoTCP

Committer:
tass
Date:
Mon Jul 08 12:25:13 2013 +0000
Revision:
0:bfe7b0aefe02
Child:
1:fd8283c28901
lwIP memory usage benchmark.

Who changed what in which revision?

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