recv peek和内存copy性能对比
文章目录
recv peek和内存copy性能对比
场景
对比每秒程序对Unix Socket执行peek recv操作的次数以及程序每秒copy内存的次数。
copy内存
以copy 256KB字节的内存为例,代码如下:
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <stdint.h>
#include <iostream>
#define BUFFER_SIZE 256 * 1024
uint64_t get_current_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int main(int argc, char const *argv[])
{
long count = 1000000;
uint64_t start = get_current_ms();
uint64_t end, diff, ret;
char *src = (char *)malloc(BUFFER_SIZE);
char *dest = (char *)malloc(BUFFER_SIZE);
for (size_t i = 0; i < count; i++)
{
memcpy(dest, src, BUFFER_SIZE);
}
diff = get_current_ms() - start;
ret = count / diff * 1000;
std::cout << "ret: " << ret << "次/秒" << std::endl;
return 0;
}
recv peek
我们在同一个进程里面模拟Swoole客户端从Unix Socket发送swEventData,以及Swoole服务器从Unix Socket调用recv来peek出swEventData里面的swDataHead。
代码如下:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <unistd.h>
#include <errno.h>
#define SW_IPC_MAX_SIZE 8192 // for IPC, dgram and message-queue max size
#define SW_IPC_BUFFER_SIZE (SW_IPC_MAX_SIZE - sizeof(struct _swDataHead))
typedef struct _swDataHead
{
int fd;
uint32_t len;
int16_t reactor_id;
uint8_t type;
uint8_t flags;
uint16_t server_fd;
uint16_t ext_info;
} swDataHead;
typedef struct
{
swDataHead info;
char data[SW_IPC_BUFFER_SIZE];
} swEventData;
uint64_t get_current_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int main(int argc, char const *argv[])
{
long count = 1000000;
int pipes[2];
int &read_fd = pipes[0];
int &write_fd = pipes[1];
uint64_t start, end, diff, ret;
swEventData send_data;
swDataHead head;
socketpair(AF_UNIX, SOCK_DGRAM, 0, pipes);
send_data.info.reactor_id = 1;
if (send(write_fd, (void *)&send_data, sizeof(send_data), 0) < 0)
{
std::cout << "send error: " << strerror(errno) << std::endl;
exit(0);
}
sleep(1);
start = get_current_ms();
for (size_t i = 0; i < count; i++)
{
if (recv(read_fd, (void *)&head, sizeof(head), MSG_PEEK) < 0)
{
std::cout << "send error: " << strerror(errno) << std::endl;
exit(0);
}
}
diff = get_current_ms() - start;
ret = count / diff * 1000;
std::cout << "ret: " << ret << "次/秒" << std::endl;
return 0;
}
压测结果
实际测试的时候,编译器开启O2优化和不开启优化结果差别不大
copy
| 内存大小 | 速率 |
|---|---|
| 8KB | 4950000次/秒 |
| 64KB | 620000次/秒 |
| 128KB | 283000次/秒 |
| 256KB | 129000次/秒 |
peek
| 速率 |
|---|
| 2793000次/秒 |
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论