- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
wrktcp安装 码云地址:https://gitee.com/icesky1stm/wrktcp 直接下载,cd wrktcp-master && make,会生成wrktcp,就ok了,很简单 。
wrktcp使用 压测首先需要一个服务,写了一个epoll+边沿触发的服务,业务是判断ip是在国内还是国外,rq:00000015CHECKIP1.0.4.0,rs:000000010,写的有些就简陋兑付看吧,主要为了压测和分析性能瓶颈.
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <thread>
#include <netinet/in.h>
#include <netdb.h>
#include <cstring>
#include <map>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <syslog.h>
std::map<unsigned long, unsigned long> g_ip_list; // 存储 IP 范围
bool init_ip_list(const char* file_name, std::map<unsigned long, unsigned long> &ip_list)
{
FILE *fp = nullptr;
if ((fp = fopen(file_name, "r")) == nullptr)
{
return false;
}
int i = 0;
int total_count = 0;
char buf[64] = {0};
while (fgets(buf, sizeof(buf), fp))
{
i++;
if (buf[0] == '#')
continue;
char *pout = nullptr;
char *pbuf = buf;
char *pc[10];
int j = 0;
while ((pc[j] = strtok_r(pbuf, "|", &pout)) != nullptr)
{
j++;
pbuf = nullptr;
if (j > 7)
break;
}
if (j != 7)
{
syslog(LOG_ERR, "%s:%d, unknown format the line is %d", __FILE__, __LINE__, i);
continue;
}
if (strcmp(pc[2], "ipv4") == 0 && strcmp(pc[1], "CN") == 0)
{
unsigned long ip_begin = inet_addr(pc[3]);
if (ip_begin == INADDR_NONE)
{
syslog(LOG_ERR, "%s:%d, ip is unknown, the line is %d, the ip is %s", __FILE__, __LINE__, i, pc[3]);
continue;
}
int count = atoi(pc[4]);
ip_begin = ntohl(ip_begin);
unsigned long ip_end = ip_begin + count - 1;
ip_list.insert(std::make_pair(ip_end, ip_begin));
total_count++;
}
}
syslog(LOG_INFO, "%s:%d, init_ip_list, total count is %d", __FILE__, __LINE__, total_count);
fclose(fp);
return true;
}
void extract_ip(char *buf, char *ip) {
// 假设协议字符串格式总是 "00000015CHECKIPx.x.x.x"
// 找到IP地址的起始位置
char *start = strstr(buf, "CHECKIP");
if (start == NULL) {
fprintf(stderr, "Invalid protocol string\n");
return;
}
// 跳过"CHECKIP"
start += 7;
// 复制IP地址到ip变量,注意检查边界
strncpy(ip, start, 15); // IP地址最多15个字符,包括'\0'
ip[15] = '\0'; // 确保字符串以'\0'结尾
}
// server
int main(int argc, const char* argv[])
{
const char* file_name = "ip_list.txt";
if (!init_ip_list(file_name, g_ip_list)) {
std::cerr << "Failed to initialize IP list." << std::endl;
return 1;
}
// 创建监听的套接字
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1)
{
perror("socket error");
exit(1);
}
// 绑定
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9999);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 本地多有的IP
// 127.0.0.1
// inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr.s_addr);
// 设置端口复用
int opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// 绑定端口
int ret = bind(lfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(ret == -1)
{
perror("bind error");
exit(1);
}
// 监听
ret = listen(lfd, 64);
if(ret == -1)
{
perror("listen error");
exit(1);
}
// 现在只有监听的文件描述符
// 所有的文件描述符对应读写缓冲区状态都是委托内核进行检测的epoll
// 创建一个epoll模型
int epfd = epoll_create(100);
if(epfd == -1)
{
perror("epoll_create");
exit(0);
}
// 往epoll实例中添加需要检测的节点, 现在只有监听的文件描述符
struct epoll_event ev;
ev.events = EPOLLIN; // 检测lfd读读缓冲区是否有数据
ev.data.fd = lfd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &ev);
if(ret == -1)
{
perror("epoll_ctl");
exit(0);
}
struct epoll_event evs[1024];
int size = sizeof(evs) / sizeof(struct epoll_event);
// 持续检测
while(1)
{
// 调用一次, 检测一次
int num = epoll_wait(epfd, evs, size, -1);
printf("==== num: %d\n", num);
for(int i=0; i<num; ++i)
{
// 取出当前的文件描述符
int curfd = evs[i].data.fd;
// 判断这个文件描述符是不是用于监听的
if(curfd == lfd)
{
// 建立新的连接
int cfd = accept(curfd, NULL, NULL);
// 将文件描述符设置为非阻塞
// 得到文件描述符的属性
int flag = fcntl(cfd, F_GETFL);
flag |= O_NONBLOCK;
fcntl(cfd, F_SETFL, flag);
// 新得到的文件描述符添加到epoll模型中, 下一轮循环的时候就可以被检测了
// 通信的文件描述符检测读缓冲区数据的时候设置为边沿模式
ev.events = EPOLLIN | EPOLLET; // 读缓冲区是否有数据
ev.data.fd = cfd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);
if(ret == -1)
{
perror("epoll_ctl-accept");
exit(0);
}
}
else
{
// 处理通信的文件描述符
// 接收数据
char buf[128];
memset(buf, 0, sizeof(buf));
// 循环读数据
while(1)
{
int len = recv(curfd, buf, sizeof(buf)-1, 0);
if(len == 0)
{
// 非阻塞模式下和阻塞模式是一样的 => 判断对方是否断开连接
printf("客户端断开了连接...\n");
// 将这个文件描述符从epoll模型中删除
epoll_ctl(epfd, EPOLL_CTL_DEL, curfd, NULL);
close(curfd);
break;
}
else if(len > 0)
{
// 通信
// 接收的数据打印到终端
write(STDOUT_FILENO, buf, len);
char ip[16]; // 存储IP地址
extract_ip(buf, ip);
printf("Received IP: %s\n", ip);
// 发送数据
//send(curfd, buf, len, 0);
// 验证 IP 地址
struct in_addr address;
int result = inet_pton(AF_INET, ip, &address); // 检查 IP 地址的有效性
if (result < 0) {
std::cout << "Invalid IP address: " << result << " " << ip << std::endl;
send(curfd, "-Err\n", 5, 0);
continue;
}
unsigned long ip_num = ntohl(address.s_addr);
auto it = g_ip_list.lower_bound(ip_num);
if (it != g_ip_list.end() && it->first >= ip_num && it->second <= ip_num) {
send(curfd, "000000010", 9, 0); // 国内
} else {
send(curfd, "000000011", 9, 0); // 国外
}
}
else
{
// len == -1
if(errno == EAGAIN)
{
printf("数据读完了...\n");
close(curfd);
break;
}
else
{
perror("recv");
exit(0);
}
}
}
}
}
}
return 0;
}
编译g++ epoll_test.cpp -o epoll_test,直接执行./epoll_test,监听0的9999端口 。
[common]
# ip & port
host = 127.0.0.1
port = 9999
[request]
req_body = CHECKIP1.0.4.0
[response]
rsp_code_location = head
说下其中的坑,req_body就是要发的协议,但是wrktcp会在前面加长度固定8位:00000015;默认成功成功响应码是000000,设置rsp_code_location这个会让wrktcp从返回协议(000000010)头开始找成功响应码 上面那些说明:wrktcp的README有一些说明,但解释的不太全,需要自己去试和看源码 。
-t, --threads: 使用线程总数,一般推荐使用CPU核数的2倍-1
-c, --connections: 连接总数,与线程无关。每个线程的连接数为connections/threads
-d, --duration: 压力测试时间, 可以写 2s, 2m, 2h
--latency: 打印延迟分布情况
--timeout: 指定超时时间,默认是5000毫秒,越长占用统计内存越大。
--trace: 打印出分布图
--html: 将压测的结果数据,输出到html文件中。
--test: 每个连接只会执行一次,一般用于测试配置是否正确。
-v --version: 打印版本信息
测试了两遍,TPS能维持在1600左右 。
Running 2m loadtest @ 127.0.0.1:9999 using sample_tiny.ini
15 threads and 15 connections
Time:100s TPS:1644.64/0.00 Latency:7.69ms BPS:14.45KB Error:0
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.66ms 14.17ms 318.09ms 98.89%
Req/Sec 113.66 233.09 1.69k 94.95%
Latency Distribution
50% 823.00us
75% 8.17ms
90% 9.15ms
99% 23.08ms
164554 requests in 1.67m, 1.41MB read
Requests/sec: 1643.21 (Success:1643.21/Failure:0.00)
Transfer/sec: 14.44KB
会在当前目录生成perf.data文件,执行perf report,会看到printf和write占用的CPU比较高,删除上面服务的printf和write函数,重新压测 重新压测之后,TPS能维持在3W+ 。
Running 2m loadtest @ 127.0.0.1:9999 using sample_tiny.ini
15 threads and 15 connections
Time:100s TPS:32748.45/0.00 Latency:438.00us BPS:287.83KB Error:0
Thread Stats Avg Stdev Max +/- Stdev
Latency 519.35us 1.24ms 63.18ms 97.47%
Req/Sec 2.19k 536.83 4.83k 76.97%
Latency Distribution
50% 349.00us
75% 426.00us
90% 507.00us
99% 5.12ms
3275261 requests in 1.67m, 28.11MB read
Requests/sec: 32716.39 (Success:32716.39/Failure:0.00)
Transfer/sec: 287.55KB
最后此篇关于网络服务性能优化:Wrktcp与Perf工具详解的文章就讲到这里了,如果你想了解更多关于网络服务性能优化:Wrktcp与Perf工具详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我们正在创建一个 n 层 Silverlight LOB 应用程序,并且正在考虑使用 .NET RIA 服务。我们不清楚这与我们当前的 WCF 服务 API 的关系在哪里。我们当前的架构是: 银光
上下文:我在celery + rabbitmq堆栈上有一个主工作系统。 系统已docker化(此处未提供worker服务) version: '2' services: rabbit:
我是 Windows Azure 新手,我正在尝试将我的 Web 应用程序部署到 Windows Azure。在我的应用程序中,我使用了一些 Web 服务,现在我想知道如何在 Windows Azur
因此,根据我对服务的了解,自定义对象似乎是写入服务以返回数据的方式。如果我正在编写将用于 1) 填充数据库或 2) 为网站提供信息的服务,是否有返回数据集/数据表而不是包含所有这些的自定义对象列表的用
我在 google 和 stackoverflow 上都找过答案,但似乎找不到。我正在尝试将 azure 实验的输出获取到应用程序。我使用 ibuildapp 和谷歌表单制作了该应用程序。如何使用 g
我不小心删除了 kubernetes svc: service "kubernetes" deleted 使用: kubectl delete svc --all 我该怎么办?我只是想删除服务,以便
我正在努力确定解决网络服务问题的最有效方法。 我的情况:我正在开发一个 Android 应用程序,它通过 Web 服务从 mysql 数据库(在我自己的服务器 PC 上)存储和检索数据。用户按下提交按
我一直在翻阅 Android 文档,我很好奇。什么时候绑定(bind)服务而不是不绑定(bind)服务?它提供了哪些优点/限制? 最佳答案 When would you bind a service
我试图从架构的角度理解 hive,我指的是 Tom White 关于 Hadoop 的书。 我遇到了以下关于配置单元的术语:Hive Services、hiveserver2、metastore 等。
我的问题:安装服务后我无法导航到基地址,因为服务不会继续运行(立即停止)。我需要在服务器或我的机器上做些什么才能使 baseAddress 有效吗? 背景:我正在尝试学习如何使用 Windows 服务
我正在努力就 Web 服务的正确组织做出决定。我应该有多个 ASMX 来代表 Web 服务中的不同功能,还是应该有一个 ASMX? 如果我有多个 ASMX,这不构成多个 Web 服务吗? 如果我只有一
我正在从事一个在 azure 平台上提供休息服务的项目。该服务由 iPhone 客户端使用,这是选择其余方法的重要原因之一。 我们希望通过 AccessControlService(ACS) 并使用
我是 Ionic 新手,正在使用 Ionic 3.9.2 我有几个终端命令来为我的 ionic 应用程序提供服务,但是,我没有发现这两个命令之间有任何区别。 ionic serve 和 ionic s
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
作为项目的一部分,我期待着问这个问题。我过去有开发和使用 Web 服务的经验,并且非常熟悉这些服务。但是,有人告诉我,作为下一个项目的一部分,我将需要使用“安全”的 Web 服务。您能否提供一些见解,
我浏览了很多关于这个问题的信息,但找不到解决方案。这里的问题是,我想使用 Apache Cordova 和 Visual Studio 连接到 wcf。因此,如果有人找到合适的工作解决方案,请发布链接
我在 Windows 服务中托管了一个 WCF(从 MS 网站示例中选取),我可以使用 SOAP UI 访问和调用方法。但是,当我尝试使用 jquery 从 Web 应用程序调用相同的方法时,我不断收
我们构建了一个 Android 应用程序,它从 Android 向我的 PHP 服务器发送 HTTP 请求。作为响应,Web 服务将 JSON 对象发送到 Android 应用程序以显示结果。 就像其
我想在 android 应用程序中调用 soap web 服务,它需要一个枚举值作为参数,它是一个标志枚举。如何从 Android 应用程序将一些值作为标志枚举传递给此 Web 服务方法? 我使用 K
我尝试在模拟器上安装 Google Play。我已按照 Google Dev Site 中的说明进行操作. 使用 ADV 管理器似乎没问题,设备的目标是 Google API 版本 22,但是当我运行
我是一名优秀的程序员,十分优秀!