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:
Mon Oct 07 10:43:36 2013 +0000
Revision:
6:e7d71e575d37
Parent:
5:7a73a14f5e85
Added new memory and stack measure mechanism.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tass 6:e7d71e575d37 1 /*
tass 6:e7d71e575d37 2 * To enable stack measurement go to pico_mbed.h and add
tass 6:e7d71e575d37 3 * #define PICO_MEASURE_STACK
tass 6:e7d71e575d37 4 * to read the current number of free stack words (integers) call stack_get_free_words
tass 6:e7d71e575d37 5 *
tass 6:e7d71e575d37 6 * To enable memory measurement go to pico_mbed.h and add
tass 6:e7d71e575d37 7 * #define MEMORY_MEASURE
tass 6:e7d71e575d37 8 */
tass 6:e7d71e575d37 9
tass 0:ebcbe44e53dd 10 #include <mbed.h>
tass 0:ebcbe44e53dd 11 #include <stdarg.h>
tass 0:ebcbe44e53dd 12 #include "EthernetInterface.h"
tass 0:ebcbe44e53dd 13
tass 6:e7d71e575d37 14 extern "C"
tass 6:e7d71e575d37 15 {
tass 6:e7d71e575d37 16 #include "pico_mbed.h"
tass 6:e7d71e575d37 17 #ifdef MEMORY_MEASURE
tass 6:e7d71e575d37 18 uint32_t max_mem;
tass 6:e7d71e575d37 19 uint32_t cur_mem;
tass 6:e7d71e575d37 20 #endif
tass 6:e7d71e575d37 21 }
tass 6:e7d71e575d37 22
tass 0:ebcbe44e53dd 23 #define ECHO_SERVER_PORT 7
tass 0:ebcbe44e53dd 24 #define BUFFER_QUANTITY (1024*1024)
tass 0:ebcbe44e53dd 25
tass 6:e7d71e575d37 26 //#define printf(...) do{}while(0)
tass 0:ebcbe44e53dd 27
tass 6:e7d71e575d37 28 void printMemoryStats(void)
tass 0:ebcbe44e53dd 29 {
tass 6:e7d71e575d37 30 #ifdef PICO_MEASURE_STACK
tass 6:e7d71e575d37 31 printf("******** Stack Statistics\n");
tass 6:e7d71e575d37 32 printf("Current free stack : %d bytes\n",stack_get_free_words()*sizeof(int));
tass 6:e7d71e575d37 33 printf("Total stack size : %d bytes\n",STACK_TOTAL_WORDS*sizeof(int));
tass 6:e7d71e575d37 34 printf("Used : %.2f %%\n",100.0*(float)(STACK_TOTAL_WORDS-stack_get_free_words())/STACK_TOTAL_WORDS);
tass 6:e7d71e575d37 35 #endif
tass 0:ebcbe44e53dd 36
tass 6:e7d71e575d37 37 #ifdef MEMORY_MEASURE
tass 6:e7d71e575d37 38 printf("******** Memory Statistics\n");
tass 6:e7d71e575d37 39 printf("Current used memory : %d bytes\n",cur_mem);
tass 6:e7d71e575d37 40 printf("Maximum used memory : %d bytes\n",max_mem);
tass 6:e7d71e575d37 41 #endif
tass 0:ebcbe44e53dd 42 }
tass 0:ebcbe44e53dd 43
tass 0:ebcbe44e53dd 44 int main() {
tass 0:ebcbe44e53dd 45
tass 0:ebcbe44e53dd 46 printf("Ethernet Interface memory test....\n");
tass 1:f014c48d0517 47
tass 0:ebcbe44e53dd 48 EthernetInterface eth;
tass 0:ebcbe44e53dd 49 int connections = 0;
tass 5:7a73a14f5e85 50 eth.init();
tass 6:e7d71e575d37 51 printf("Waiting for DHCP to give IP\n");
tass 6:e7d71e575d37 52 while(eth.connect() != 0);
tass 0:ebcbe44e53dd 53 printf("IP Address %s\n", eth.getIPAddress());
tass 0:ebcbe44e53dd 54
tass 0:ebcbe44e53dd 55 TCPSocketServer server;
tass 0:ebcbe44e53dd 56 server.bind(ECHO_SERVER_PORT);
tass 0:ebcbe44e53dd 57 server.listen();
tass 0:ebcbe44e53dd 58
tass 0:ebcbe44e53dd 59 while (true) {
tass 0:ebcbe44e53dd 60 printf("\nWait for new connection...\n");
tass 0:ebcbe44e53dd 61 printf("Client number %d\n",++connections);
tass 0:ebcbe44e53dd 62 TCPSocketConnection client;
tass 0:ebcbe44e53dd 63 server.accept(client);
tass 0:ebcbe44e53dd 64 client.set_blocking(false, 1500); // Timeout after (1.5)s
tass 0:ebcbe44e53dd 65 printf("Connection from: %s\n", client.get_address());
tass 0:ebcbe44e53dd 66 char buffer[1024];
tass 0:ebcbe44e53dd 67 while (true) {
tass 0:ebcbe44e53dd 68
tass 0:ebcbe44e53dd 69 int dataReceived = 0;
tass 0:ebcbe44e53dd 70 int dataSent = 0;
tass 0:ebcbe44e53dd 71
tass 0:ebcbe44e53dd 72 while(dataReceived < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 73 {
tass 0:ebcbe44e53dd 74 int n = client.receive(buffer, sizeof(buffer));
tass 0:ebcbe44e53dd 75 if (n <= 0) {
tass 0:ebcbe44e53dd 76 printf("Receive error\n");
tass 0:ebcbe44e53dd 77 break;
tass 0:ebcbe44e53dd 78 }
tass 0:ebcbe44e53dd 79 dataReceived += n;
tass 0:ebcbe44e53dd 80 }
tass 0:ebcbe44e53dd 81
tass 0:ebcbe44e53dd 82 if(dataReceived < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 83 {
tass 0:ebcbe44e53dd 84 printf("Receiving part of the test has failed. Exiting connection.\n");
tass 6:e7d71e575d37 85 printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY);
tass 0:ebcbe44e53dd 86 break;
tass 0:ebcbe44e53dd 87 }
tass 0:ebcbe44e53dd 88
tass 0:ebcbe44e53dd 89 while(dataSent < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 90 {
tass 0:ebcbe44e53dd 91 int n = client.send_all(buffer, sizeof(buffer));
tass 0:ebcbe44e53dd 92 if (n <= 0) {
tass 0:ebcbe44e53dd 93 printf("Send error\n");
tass 0:ebcbe44e53dd 94 break;
tass 0:ebcbe44e53dd 95 }
tass 0:ebcbe44e53dd 96 dataSent += n;
tass 0:ebcbe44e53dd 97 }
tass 0:ebcbe44e53dd 98
tass 0:ebcbe44e53dd 99 if(dataSent < BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 100 {
tass 0:ebcbe44e53dd 101 printf("Sending part of the test has failed. Exiting connection.\n");
tass 6:e7d71e575d37 102 printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY);
tass 6:e7d71e575d37 103
tass 0:ebcbe44e53dd 104 break;
tass 0:ebcbe44e53dd 105 }
tass 0:ebcbe44e53dd 106
tass 0:ebcbe44e53dd 107 dataReceived = dataSent = 0;
tass 0:ebcbe44e53dd 108 while((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 109 {
tass 0:ebcbe44e53dd 110 int n = client.receive(buffer, sizeof(buffer));
tass 0:ebcbe44e53dd 111 if (n <= 0) {
tass 0:ebcbe44e53dd 112 printf("Receive error\n");
tass 0:ebcbe44e53dd 113 break;
tass 0:ebcbe44e53dd 114 }
tass 0:ebcbe44e53dd 115 dataReceived += n;
tass 0:ebcbe44e53dd 116
tass 0:ebcbe44e53dd 117 n = client.send_all(buffer, n);
tass 0:ebcbe44e53dd 118 if (n <= 0) {
tass 0:ebcbe44e53dd 119 printf("Send error\n");
tass 0:ebcbe44e53dd 120 break;
tass 0:ebcbe44e53dd 121 }
tass 0:ebcbe44e53dd 122 dataSent += n;
tass 0:ebcbe44e53dd 123 }
tass 0:ebcbe44e53dd 124
tass 0:ebcbe44e53dd 125 if((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:ebcbe44e53dd 126 {
tass 6:e7d71e575d37 127 printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY);
tass 0:ebcbe44e53dd 128 printf("Echo test has failed.Exiting connection...\n");
tass 0:ebcbe44e53dd 129 }
tass 6:e7d71e575d37 130
tass 3:8892f28e2e58 131 break;
tass 0:ebcbe44e53dd 132 }
tass 6:e7d71e575d37 133
tass 0:ebcbe44e53dd 134 client.close();
tass 1:f014c48d0517 135 printf("Test was finished...\n");
tass 1:f014c48d0517 136 printMemoryStats();
tass 0:ebcbe44e53dd 137 }
tass 0:ebcbe44e53dd 138
tass 0:ebcbe44e53dd 139 return 0;
tass 0:ebcbe44e53dd 140 }