In networking and telecommunications, it is important to simulate and analyze network traffic patterns to understand system performance and capacity requirements.
This blog provides a MATLAB code to create a simple traffic generator based on the Poisson distribution, allowing users to model realistic traffic patterns in a network simulation.
The code consists of two main components: the data_generator function and the traffic simulation. Let's start by examining the data_generator function.
The data_generator function takes three input parameters: meanSize, maxTraffic, and stdPercentage. These parameters allow users to define the mean packet size, maximum packet size, and the standard deviation percentage from the mean. If any of the parameters are not provided, default values are used (10 megabits for meanSize, 50 megabits for maxTraffic, and 20% for stdPercentage).
Inside the function, the standard deviation stdSize is calculated based on the meanSize and stdPercentage. Then, a packet size is generated using the formula
max(0, randn * stdSize + meanSize), ensuring that the packet size is non-negative and within the maximum traffic limit. The function returns the generated packet size.
The code is as below:
% This data generator creates data based on normal distribution with the
% mean and max allowed traffic and a standard deviation around that mean.
function packetSize = data_generator(meanSize, maxTraffic, stdPercentage)
% meanSize = meanSize of the data-packet size
% maxTraffic = Max size of the data-packet size
% stdPercentage = Percentage deviation of packet size from mean
% Check if var1_1 is not provided
if nargin < 1
meanSize = 10 * 10^6; % Mean packet size in bits (10 Mega bits)
end
% Check if var2 is not provided
if nargin < 2
maxTraffic = 50 * 10^6; % Maximum traffic in bits (50 Mega bits)
end
% Check if var3 is not provided
if nargin < 3
stdPercentage = 0.2; % Standard deviation percentage from the mean (20%)
end
stdSize = meanSize * stdPercentage;
% packetSize = max(0, normrnd(meanSize, stdSize));
packetSize = max(0, randn * stdSize + meanSize);
packetSize = min(packetSize, maxTraffic);
end
Moving on to the traffic simulation, various parameters are defined, such as totalTime (simulation duration), onDuration (duration of ON state), offDuration (duration of OFF state), trafficRateOn (traffic rate during ON state), and trafficRateOff (traffic rate during OFF state).
The simulation is performed by generating an ON-OFF state vector. The state vector represents the network state at each time step, with 1 indicating the ON state and 0 indicating the OFF state. The state vector is generated by iterating through the time steps and setting segments of the vector to 1 for the ON duration and 0 for the OFF duration.
Next, the traffic vector is generated based on the state vector. For each time step, if the state is ON (value is 1), the traffic rate during the ON state is multiplied by calling the data_generator function without any parameters. This generates a packet size based on the Poisson distribution. If the state is OFF (value is 0), the traffic rate during the OFF state is assigned.
% Parameters
totalTime = 10; % Total simulation time (in seconds)
onDuration = 0.5; % Duration of ON state (in seconds)
offDuration = 0.5; % Duration of OFF state (in seconds)
trafficRateOn = 10; % Traffic rate during ON state (in packets/second)
trafficRateOff = 0; % Traffic rate during OFF state (in packets/second)
% Simulation
time = 0:0.01:totalTime; % Time vector with small time steps
state = zeros(size(time)); % Initialize state vector (0 = OFF, 1 = ON)
% Generate ON-OFF state vector
i = 1;
while i <= length(time)
state(i:i+onDuration*100) = 1; % ON state
i = i + onDuration*100 + offDuration*100;
end
% Generate traffic based on state vector
traffic = zeros(size(time)); % Initialize traffic vector
for i = 1:length(time)
if state(i) == 1
traffic(i) = trafficRateOn*data_generator();
else
traffic(i) = trafficRateOff;
end
end
Finally, the traffic is plotted over time using the stem function, creating a stem plot that visualizes the network traffic pattern. The x-axis represents time in seconds, and the y-axis represents the traffic rate in packets per second.
By using this code, network researchers and engineers can generate realistic traffic patterns for network simulations. It allows them to study the effects of different traffic rates, ON-OFF durations, and packet sizes on network performance and design efficient systems to handle varying traffic loads.
% Plotting
figure;
% plot(time, traffic);
stem(time, traffic)
xlabel('Time (s)');
ylabel('Traffic Rate (packets/second)');
title('Network Traffic (ON-OFF)');