[C/C++]使用iperf3压测网口吞吐量是否达标

什么是iperf3

iperf是一个主动测量IP网络上最大可实现带宽的工具。它支持调优与定时、协议和缓冲区相关的各种参数。对于每个测试,它都会报告测量的吞吐量/比特率、损耗和其他参数。
这个版本,有时被称为iperf3,是对NLANR/ ast开发的原始版本的重新设计。Iperf3是一个从头开始的新实现,其目标是更小、更简单的代码库,以及可以在其他程序中使用的功能的库版本。Iperf3还具有许多其他工具(如nutcp和netperf)中存在的特性,但这些特性在最初的iperf中是没有的。例如,这些包括零复制模式和可选的JSON输出。请注意,iperf3与原始iperf不向后兼容。
iperf3的主要开发是在Ubuntu Linux、FreeBSD和macOS上进行的。目前,这些是唯一得到官方支持的平台,不过也有一些关于OpenBSD、NetBSD、Android、Solaris和其他Linux发行版的成功报告。
iperf3主要由ESnet /劳伦斯伯克利国家实验室开发。它是在包含三个条款的BSD许可证下发布的。
github:https://github.com/esnet/iperf

iperf3服务端

通过iperf3 --hep可以查看service端和客户端的使用方法

Server specific:
  -p, --port      #         server port to listen on/connect to
  -s, --server              run in server mode

运行服务端

PS C:\Users\shuai.yin\Desktop\IPERF3 测试工具> .\iperf3.exe  -s -p 19991
-----------------------------------------------------------
Server listening on 19991
-----------------------------------------------------------
Accepted connection from 172.16.40.104, port 42524
[  5] local 172.16.40.21 port 19991 connected to 172.16.40.104 port 42534
[ ID] Interval           Transfer     Bandwidth
[  5]   0.00-1.00   sec  10.9 MBytes  91.5 Mbits/sec
[  5]   1.00-2.00   sec  11.3 MBytes  94.4 Mbits/sec
[  5]   2.00-3.00   sec  11.3 MBytes  94.9 Mbits/sec
[  5]   3.00-4.00   sec  11.3 MBytes  94.9 Mbits/sec
[  5]   4.00-5.00   sec  11.3 MBytes  94.9 Mbits/sec
[  5]   5.00-6.00   sec  11.3 MBytes  94.9 Mbits/sec

iperf3客户端

客户端运行iperf3

  -c, --client    <host>    run in client mode, connecting to <host>
  -t, --time      #         time in seconds to transmit for (default 10 secs)
  -i, --interval  #         seconds between periodic throughput reports

运行iperf3客户端

root@rk3588-buildroot:/# iperf3 -c 172.16.40.21 -p 19991  -i 10 -t 30 > test1
root@rk3588-buildroot:/# cat test1
Connecting to host 172.16.40.21, port 19991
[  5] local 172.16.40.104 port 47372 connected to 172.16.40.21 port 19991
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-10.00  sec   113 MBytes  95.2 Mbits/sec    0    184 KBytes
[  5]  10.00-20.00  sec   113 MBytes  94.9 Mbits/sec    0    184 KBytes
[  5]  20.00-30.00  sec   113 MBytes  94.8 Mbits/sec    0    275 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-30.00  sec   340 MBytes  95.0 Mbits/sec    0             sender
[  5]   0.00-30.00  sec   339 MBytes  94.8 Mbits/sec                  receiver

[C/C++]输出结果提取

windows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 256
#define BITRATE_COLUMN_INDEX 6 // 根据你的数据格式,Bitrate在第四列
typedef struct {
    double bitrate; // 使用double类型存储Bitrate的数值部分
} BandwidthData;
char* next_token1 = NULL;

void parseLine(char* line, BandwidthData* bd) {
    char* token;
    int column = 0;
    token = strtok_s(line, " ", &next_token1); // 使用空格分割
    bd->bitrate = -1;
    while (token != NULL && column < BITRATE_COLUMN_INDEX + 1) {
        if (column == BITRATE_COLUMN_INDEX) {
            // 提取速率的数字部分,单位固定为 'Mbits/sec'
            char* endptr;
            double rate = strtod(token, &endptr); // 将字符串转换为浮点数
            if (*endptr == '\0' || *endptr == ' ') {
                // 如果结束位置是空字符或空格,说明成功提取了数字
                bd->bitrate = rate;
            }
            break;
        }
        if(token != NULL)
            token = strtok_s(NULL, " ", &next_token1);
        column++;
    }
}

int main() {
    FILE* file;
    fopen_s(&file,"test1.txt", "r"); // 替换为你的文件路径
    if (file == NULL) {
        perror("Failed to open file");
        return EXIT_FAILURE;
    }
    char line[MAX_LINE_LENGTH];
    BandwidthData bd;
    while (fgets(line, MAX_LINE_LENGTH, file)) {
        parseLine(line, &bd);
        if (bd.bitrate != -1)
            printf("Bitrate: %.2f Mbits/sec\n", bd.bitrate);
    }
    fclose(file);
	return 0;
}

linux:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 256
#define BITRATE_COLUMN_INDEX 6 // 根据你的数据格式,Bitrate在第四列
typedef struct {
    double bitrate; // 使用double类型存储Bitrate的数值部分
} BandwidthData;

void parseLine(char* line, BandwidthData* bd) {
    char* token;
    int column = 0;
    token = strtok(line, " "); // 使用空格分割
    bd->bitrate = -1;
    while (token != NULL && column < BITRATE_COLUMN_INDEX + 1) {
        if (column == BITRATE_COLUMN_INDEX) {
            // 提取速率的数字部分,单位固定为 'Mbits/sec'
            char* endptr;
            double rate = strtod(token, &endptr); // 将字符串转换为浮点数
            if (*endptr == '\0' || *endptr == ' ') {
                // 如果结束位置是空字符或空格,说明成功提取了数字
                bd->bitrate = rate;
            }
            break;
        }
        if(token != NULL)
            token = strtok_s(NULL, " ");
        column++;
    }
}

int main() {
    FILE* file;
    file = fopen("test1.txt", "r"); // 替换为你的文件路径
    if (file == NULL) {
        perror("Failed to open file");
        return EXIT_FAILURE;
    }
    char line[MAX_LINE_LENGTH];
    BandwidthData bd;
    while (fgets(line, MAX_LINE_LENGTH, file)) {
        parseLine(line, &bd);
        if (bd.bitrate != -1)
            printf("Bitrate: %.2f Mbits/sec\n", bd.bitrate);
    }
    fclose(file);
	return 0;
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值