pnet benchmarking

20 views (last 30 days)
Priya Bhat
Priya Bhat on 28 Mar 2011
Hello everyone,
I am trying to benchmark pnet toolbox for sending and receiving both tcp/udp messages between the machines. My goal is to measure the throughput and latency for different pay-load sizes.
I wanted some advice on getting the basic loop of the script running. I wrote the following code for the listener:
sPort = '1234';
if ((sPort > 1024))
tcpCon = pnet('tcpsocket', sPort); % only create this socket once
tcpCon = -1;
serv = pnet(tcpCon,'tcplisten');
pnet(tcpCon, 'close');
catch exception
retCode = 1;
But I get the error:
??? Error using ==> pnet
Invalid socket for LISTEN, Already open, or UDP?...
Error in ==> test_pnet at 24
serv = pnet(tcpCon,'tcplisten');
Can someone please guide me to what could be the problem. If anyone has a simple working sender/receiver script that I could develop on that would be great!

Answers (5)

Ian on 3 Apr 2011
Hi, perhaps you have another application that has opened that port? Try a higher port number (9876 for example), or check if that port is open (sudo lsof -i TCP -P in os x/linux, use sysinternals tcpview in windows).
I use pnet for both UDP and TCP communication, wrapping them in an oop object. My ghetto code needs refactoring, but here it is warts and all:
To open a server:
sconn=dataConnection(struct('lPort', 8765, 'protocol', 'tcp', 'autoOpen', 1, 'type', 'server'));
To open a client:
cconn=dataConnection(struct('rPort', 8765, 'rAddress', '', 'protocol', 'tcp', 'autoOpen', 1));
Check connection:
sconn.checkClient; %server needs to check if a client has connected
sconn.write('Ping'); %write data to the connection % (ans='Ping')
EDIT: as Walter points out, the port number needs to be a number for pnet not a string; the address should be a string.

Walter Roberson
Walter Roberson on 3 Apr 2011
Your code
sPort = '1234';
sets sPort to be a string. Then in the next line, you try to compare the string to an integer. The result is to compare each character of the string to the integer, and to find the "if" to be true only if all of the comparisons are true. None of the comparisons will be true, though, as '1' is only about value 48...

Priya Bhat
Priya Bhat on 8 Apr 2011
Hi Guys!
Thanks for your responses. I have created a preliminary version of the benchmarking code. I have uploaded it here in case any one would like to have a look. Any feedback would be greatly appreciated.
The basic idea of the code is to have two sessions (one server and one receiver) running and transmit packets of different sizes for several iterations and measure the time elapsed.
I compared the results of throughput with those obtained with netperf on the same set of machines and it seems (from the surface) that pnet is very slow:
Results from netperf:
pbhat@BMIUbuntu64:~$ netperf -H STREAM TEST from ( port 0 AF_INET to ( port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
87380 16384 16384 10.00 4302.63
Output of pnet_benchmarking is:
Send Throughput MeanLatency
Size 10^6 10^(-3)
bytes bits/sec sec
100 4.435 0.181
500 21.845 0.185
1000 44.114 0.183
1500 65.640 0.184
2000 86.150 0.187
5000 193.530 0.208
16384 460.342 0.285
In this case the better term for Throughput would probably be "Average Bandwidth". As you can see there is order of magnitude difference between the two results. I am wondering if this is something I am doing or if pnet is little inefficient.
Also copypasting my code here for easier refernce:
Server Code
function server(iterNumber, repeat)
% This function tests how to send and receive packets using pnet over the
% network.
if (nargin < 1) iterNumber = 1000; end
if (nargin < 2) repeat = 1000; end
%Set path
if isunix
pnetPath ='/home/pbhat/ModifiedBmiModels/TestCwFolder/pnetLib';
pnetPath = 'C:\Users\Administrator\Documents\ModifiedBmiModels\TestCwFolder\pnetLib';
%Set up Connection
sPort = '1677';
sockcon = pnet('tcpsocket', sPort);
ByteSize = [100, 500, 1000, 1500, 2000,5000, 16384];
byte_sizes = length(ByteSize);
data_collected = 0;
%Receive and send data
for i = 1: byte_sizes
byteSize = ByteSize(i);
for j = 1: iterNumber
for r = 1: repeat
element = pnet(tcpCon,'read',byteSize,'uint8');
% pnet(tcpCon, 'write' , element, 'uint8');
pnet(tcpCon, 'write' , element, 'uint8');
data_collected = data_collected + length(element);
%Store the final result
str = ['Test completed. Number of uint8 elements received: ' num2str(data_collected)];
pnet(tcpCon, 'close');
catch exception
retCode = 1;
Client File 2: client_test.m
function TimeElapsed = client_test(con, byteSize, iterNumber, repeat)
if (nargin < 1) connectionType = 'tcpconnect'; end
if (nargin < 2) byteSize = 100; end
if (nargin < 3) iterNumber = 1000; end
if (nargin < 4) repeat = 1000; end
payload = uint8(rand(byteSize,1));
TimeElapsed = zeros(iterNumber,1);
%Carry out experiment
for i = 1:iterNumber
tStart = tic;
for r = 1: repeat
pnet(con,'write',payload, 'uint8');
element = pnet (con, 'read', byteSize, 'uint8');
TimeElapsed(i) = (toc(tStart)) / repeat;
Client function 1: benchmarking_pnet code
function [Results_thoughput, Results_latency] = benchmarking_pnet(host, remote, iterNumber, repeat, draw_plot)
%How to run
% Start this function after startigng the server function in another matlab session. 'host and remote values have been associated with hardcoded addresses for this test bench
if (nargin < 1) host = 'local'; end
if (nargin < 2) remote = 'local'; end
if (nargin < 3) iterNumber = 1000; end
if (nargin < 4) repeat = 1000; end
if (nargin < 5) draw_ plot = 1; end
%Add pnet to path
if isunix
pnetPath ='../pnet_benchmarking/pnetLib';
pnetPath = '..\pnet_benchmarking\pnetLib';
%Hardcoded IP addresses of machines on which test are performed
if strcmp(remote,'Win32')
remoteHost = '';
elseif strcmp(remote,'Win64')
remoteHost = '';
elseif strcmp(remote,'Lin64')
remoteHost = '';
elseif strcmp(remote,'Lin32')
remoteHost = '';
remoteHost = '';
ByteSize = [100, 500, 1000, 1500, 2000, 5000, 16384];
byte_sizes = length(ByteSize);
TimeElapsed = zeros(iterNumber, byte_sizes);
Results = zeros(byte_sizes,7);
%Set up connection
connectionType = 'tcpconnect';
con = pnet(connectionType, remoteHost, 1677);
if con == -1
Error = ['connection could not be established, Error:' num2str(con)];
%Begin Testing
for i = 1: byte_sizes
byteSize = ByteSize(i);
TimeElapsed(:, i) = client_test(con, byteSize, iterNumber, repeat);
%Compute the Latency.
% if (i > 1)
% TimeElapsed(:,i) = TimeElapsed(:, i) - byteSize .* (TimeElapsed(:,1);
% end
[muhat,sigmahat,muci,sigmaci] = normfit(TimeElapsed(:, i));
Results(i, 1) = 8* mean( byteSize ./ TimeElapsed(:, i) );
Results(i, 2) = muhat;
Results(i, 3) = sigmahat;
Results(i, 4) = muci(1);
Results(i, 5) = muci(2);
Results(i, 6) = sigmaci(1);
Results(i, 7) = sigmaci(2);
%Store Results and Display plots
str = ['Total uint8 items sent from the client program:' num2str(sum(ByteSize) * iterNumber)];
filename = ['Host-' host 'Remote-' remote];
result_filename = ['Results/' filename 'Results.txt'];
dlmwrite(result_filename, Results, 'delimiter', '\t', 'precision', 6);
%print output on console
fprintf('%15s%15s%15s \n', 'Send','Throughput','MeanLatency');
fprintf('%15s%15s%15s \n', 'Size', '10^6','10^(-3)');
fprintf('%15s%15s%15s \n\n', 'bytes', 'bits/sec','sec');
for i = 1: byte_sizes
fprintf('%15d%15.3f%15.3f\n',ByteSize(i),Results(i,1)/(10^6), Results(i,2)*(10^3));
% plotting the Elapsed Time graph
if (draw_plot == 1)
iteration = 1: iterNumber;
figure_filename = ['Figures/' filename 'Plot.png'];
handle = figure('Visible', 'off');
plot(iteration, TimeElapsed, '--s', 'LineWidth',1,'MarkerSize',5);
xlabel('Iteration Number');
ylabel('Latency (seconds)');
legend('100', '500', '1000', '1500', '2000', '5000');
title(['Latency Plot for' filename]);
print(handle, '-dpng' , figure_filename);
%End of figurediaplay
clear con;
Priya Bhat
Priya Bhat on 8 Apr 2011
Well thanks anyway. And do let me know if you have any thoughts about this.

Sign in to comment.

Priya Bhat
Priya Bhat on 8 Apr 2011
Does anyone have an idea about how much longer it takes to send a very small (20 bytes) packet using pnet as compared to ping.
ping <machine_address> -s 50
I am getting an order of magnitude difference and I want to know if someone can corroborate that. On my machine the ping time is 0.021 ms while the pnet round trip time is ~.50 ms

Erik Flister
Erik Flister on 11 Jul 2011


Find more on Perform Sensitivity Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!