起因
最近想基于共享内存加有名信号量实现一个进程间的共享栈,来取代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