起因
最近想基于共享内存加有名信号量实现一个进程间的共享栈,来取代thrift, 优化下ipc的延迟。
测试
首先测试了下thrift消息传递的延迟,分别在client调用前和server(client server位于同一台机器)收到消息打印微秒
start end us
776912 777005 93
777152 777228 76
777340 777422 82
777502 777572 70
测试结果表明thrift一次通信大概需要80us的延迟。
如果我们改为信号量来通知server读取共享栈的参数,那么我们有必要测试下信号量在阻塞的情况下,被sem_post唤醒的性能。
贴下测试使用的client 和server代码
client
#include <semaphore.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
int main()
{
sem_t *lock = sem_open("lock_test", O_CREAT | O_RDWR, 0777, 0);
int id = 0;
while(1) {
sleep(1);
struct timeval tv;
gettimeofday(&tv, NULL);
sem_post(lock);
printf("id=%d, usec=%d\n", id, tv.tv_usec);
id++;
}
}
server
#include <semaphore.h>
#include <fcntl.h>
#include <stdio.h>
#inclu

本文探讨了使用Linux有名信号量作为进程间通信手段的可能性,以替代Thrift以降低IPC延迟。作者进行了测试,发现Thrift通信延迟约为80us。接着测试信号量在阻塞和被唤醒的情况,得出约20-40us的唤醒时间,但考虑到自定义协议栈解析和并发问题,最终认为改造的必要性不高。
最低0.47元/天 解锁文章
129

被折叠的 条评论
为什么被折叠?



