Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Exercise 2

Measuring the throughput of a Wi-Fi connection

In this exercise, we will measure the throughput of the Wi-Fi connection between your device and an iPerf server that we will create in the exercise. .

The device will connect to the iPerf server running on a test PC, and will send and receive packets during the test duration. When the test is finished, the result of the throughput test will be printed to the terminal.

Exercise steps

In the GitHub repository for this course, go to the base code for this exercise, found in lesson3/wififund_less3_exer2.

1. Set the necessary networking configurations

1.1 Enable zperf

Enable zperf shell utility through CONFIG_NET_ZPERF in the prj.conf file.

1.2 Configure the relevant network configurations.

Enable network settings through CONFIG_NET_CONFIG_SETTINGS and set the networking timeout to 0 through CONFIG_NET_CONFIG_INIT_TIMEOUT.

Set the peer IPv4 address using CONFIG_NET_CONFIG_PEER_IPV4_ADDR. This is the IPv4 address of the computer you are running the zperf server on. You can find the IPv4 of your computer by typing “ipconfig” in your command prompt on Windows or “ifconfig” in your terminal on Linux and macOS.

1.3 Increase the heap memory pool size.

Depending on the board you are using, open either boards/nrf7002dk_nrf5340_cpuapp.conf or boards/nrf5340dk_nrf5340_cpuapp.conf, and increase the heap memory pool size by adding the following Kconfig line.

2. Include the header files for the zperf API and nrfx clock.

#include <zephyr/net/zperf.h>
#include <nrfx_clock.h>

3. Define the zperf server and test parameters

3.1 Define the port for the zperf server

#define PEER_PORT 5001

3.2 Define packet size, rate and test duration

#define WIFI_ZPERF_PKT_SIZE 1024
#define WIFI_ZPERF_RATE 10000
#define WIFI_TEST_DURATION 20000

4. Create a socket address struct for the server address

static struct sockaddr_in in4_addr_my = {
	.sin_family = AF_INET,
	.sin_port = htons(PEER_PORT),
};

5. Fill in the zperf upload parameters for the zperf client

5.1 Initialize a struct for storing the zperf upload parameters.

struct zperf_upload_params params;

5.2 Configure packet size, rate and duration from the defines created earlier.

params.packet_size = WIFI_ZPERF_PKT_SIZE;
params.rate_kbps = WIFI_ZPERF_RATE;
params.duration_ms = WIFI_TEST_DURATION;

5.3 Convert the server address from a string to IP address.

ret = net_addr_pton(AF_INET, CONFIG_NET_CONFIG_PEER_IPV4_ADDR, &in4_addr_my.sin_addr);
if (ret < 0) {
	LOG_ERR("Invalid IPv4 address %s\n", CONFIG_NET_CONFIG_PEER_IPV4_ADDR);
	return -EINVAL;
}

5.4 Add the zperf server address to the zperf_upload_params struct.

memcpy(&params.peer_addr, &in4_addr_my, sizeof(in4_addr_my));

6. Call zperf_udp_upload_async() to start the asynchronous UDP upload.

ret = zperf_udp_upload_async(&params, udp_upload_results_cb, NULL);
if (ret != 0) {
	LOG_ERR("Failed to start Wi-Fi benchmark: %d\n", ret);
	return ret;
}

7. Retrieve and print the results from the throughput test.

Handle the three zperf session statuses: started, finished, and error inside the zperf_status switch.

7.1 Inform the user that the UDP session has started

LOG_INF("New UDP session started");

7.2 If client_time_in_us is not zero, calculate the throughput rate in kilobit per second. Otherwise, set it to zero.

if (result->client_time_in_us != 0U) {
	client_rate_in_kbps = (uint32_t)
		(((uint64_t)result->nb_packets_sent *
		  (uint64_t)result->packet_size * (uint64_t)8 *
		  (uint64_t)USEC_PER_SEC) /
		 ((uint64_t)result->client_time_in_us * 1024U));
} else {
	client_rate_in_kbps = 0U;
}

7.3 Print the results of the throughput test

LOG_INF("Upload results:");
LOG_INF("%u bytes in %u ms",
		(result->nb_packets_sent * result->packet_size),
		(result->client_time_in_us / USEC_PER_MSEC));
LOG_INF("%u packets sent", result->nb_packets_sent);
LOG_INF("%u packets lost", result->nb_packets_lost);
LOG_INF("%u packets received", result->nb_packets_rcvd);
LOG_INF("%u kbps throughput",client_rate_in_kbps);

7.4 Inform the user that there is an error with the UDP session

LOG_ERR("UDP session error");

8. Install iPerf

Running this example requires the Wi-Fi iPerf application. Download and install iPerf by following the guide for your operating system.

Windows

8.1 Install chocolatey.

8.2 Add chocolatey to path.

8.3 Open a Windows Command Prompt window as Administrator. To do so, press the Windows key, type “cmd”, right-click the result, and choose Run as Administrator.

8.4 Use choco to install iPerf. Make sure to install version 2.0.8.

Linux

8.1 Install iPerf using apt.

9. Start the iPerf server

Important

This exercise assumes a local connection. Therefore, you must ensure your Nordic device and the device you are running the iPerf server from are connected to the same network.
Windows

9.1 Run the following command in the command prompt to start the iPerf server in UDP mode.

Allow iperf2 to communicate on public and private network if you get a pop up from Windows Defender Firewall.

Linux

9.1 Run the following command in a terminal to start the iPerf server in UDP mode.

10. With the iPerf server running, build and flash the application to your board.

Due to the memory requirements of the exercise, you need to build the application without TF-M.

BoardBuild without TF-M
nRF7002 DKnrf7002dk_nrf5340_cpuapp
nRF5340 DK + nRF7002 EKnrf5340dk_nrf5340_cpuapp

If the application does not connect to the network, initiate a Wi-Fi connection by issuing the following commands

Please note it can take a minute while messages are printed on the terminal.

11. The results of the throughput test will be printed after the test has finished

You should see output similar to this from the iPerf server

Note

The Wi-Fi performance is affected by various factors, such as the distance to the access point and its placement, the Wi-Fi adapter on the PC running the iPerf server, frequency band, noise/interference, and more. Therefore, results will vary from one testing environment to the other.
Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.