Skip to content

Commit

Permalink
优化性能
Browse files Browse the repository at this point in the history
1. memset() -> bzero()
2. 增加benchmark.md
3. 完善c++接口
4. 更新libevlite版本v9.6.1

Signed-off-by: spriteray <spriteray@gmail.com>
  • Loading branch information
spriteray committed Aug 21, 2018
1 parent c15623d commit 14e8321
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 181 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
OS = $(shell uname)

APP = libevlite
VERSION = 9.6.0
VERSION = 9.6.1
PREFIX = /usr/local

# 主版本号
Expand All @@ -21,8 +21,8 @@ endif

# 默认选项
LFLAGS = -ggdb -lpthread
CFLAGS = -Wall -Wformat=0 -Iinclude/ -Isrc/ -Itest/ -ggdb -fPIC -O2 -DNDEBUG -D__EVENT_VERSION__=\"$(REALNAME)\" #-DUSE_REUSEPORT #-DUSE_ATOMIC
CXXFLAGS = -Wall -Wformat=0 -Iinclude/ -Isrc/ -Itest/ -ggdb -fPIC -O2 -DNDEBUG -D__EVENT_VERSION__=\"$(REALNAME)\" #-DUSE_REUSEPORT #-DUSE_ATOMIC
CFLAGS = -Wall -Wformat=0 -Iinclude/ -Isrc/ -Itest/ -ggdb -fPIC -O2 -DNDEBUG -D__EVENT_VERSION__=\"$(REALNAME)\" -DUSE_ATOMIC #-DUSE_REUSEPORT
CXXFLAGS = -Wall -Wformat=0 -Iinclude/ -Isrc/ -Itest/ -ggdb -fPIC -O2 -DNDEBUG -D__EVENT_VERSION__=\"$(REALNAME)\" -DUSE_ATOMIC #-DUSE_REUSEPORT

# 动态库编译选项
ifeq ($(OS),Darwin)
Expand Down Expand Up @@ -130,6 +130,7 @@ chatroom_client: io.o chatroom_client.o $(OBJS)
clean :
rm -rf *.o
rm -rf *.log
rm -rf core
rm -rf *.core
rm -rf core.*
rm -rf vgcore.*
Expand Down
11 changes: 7 additions & 4 deletions docs/README
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Makefile.* - Makefile
1000 113MBytes/s 41MBytes/s
1000 110MBytes/s 43MBytes/s
10000 119MBytes/s 20MBytes/s
10000 110MBytes/s 37MBytes/s
10000 110MBytes/s 37MBytes/s

2. ���ֵ����⣺
1) 1000������ʱ����������л��ﵽ50K, ��20000�����ӵ��л���300��;[�ѽ��, vmstatͳ������]
Expand All @@ -39,8 +39,8 @@ Makefile.* - Makefile
6000 9000,[330~38000] 130MBytes/s
7000 6000,[300~19000] 131MBytes/s
8000 1000,[300~18000] 127MBytes/s
10000 350 130MBytes/s
10000 350 130MBytes/s

2) ͬ��1000�����ӵ�ʱ��, IO�̵߳ȴ�ʱ��Ϊ0ʱ(IO�߳�������̯ѹ��), �������л��������ͣ������������;

�ġ�Accept-Lockģ�͵Ļ��Է�����
Expand All @@ -51,6 +51,9 @@ Makefile.* - Makefile
2. ���Խ����
1) accept�¼�������һ���߳��з���, ��û��ԭ����Ƶ�Ч������ѹ�����ԵĽű������������Ľ��Ҳ�ǿ�������ġ�
�Ͼ��������Ӷ��ɹ��󣬲ſ�ʼ�������ݵģ�����һ��ʼ���̻߳���û��ʲôѹ����

�塢ͬ���Ŀ��������У�centos6.0��freebsd8.2���˽�30%��

��������������Ƚ�(muduo, Ubuntu 14.04.5, Intel(R) Xeon(R) CPU E5606 @ 2.13GHz, 8��8G)
moduo 1000�����ӣ�4��IO�̣߳�4K�����ݰ���197.515234375 MiB/s throughput
libevlite 1000�����ӣ�4��IO�̣߳�4K�����ݰ���245.58203125 MiB/s throughput
29 changes: 29 additions & 0 deletions docs/benchmark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# libevlite性能测试



### 一、吞吐量测试结果

##### 1. 硬件配置

2CPU4Core

CPU型号 Intel(R) Xeon(R) CPU E5606 @ 2.13GHz

8G内存

Ubuntu 14.04.5

##### 2. 4K数据包,4个IO线程

| 连接数 | 1000 | 2000 | 10000 |
| --------- | ------- | ------- | ------- |
| muduo | 197.515 | 232.825 | 244.675 |
| libevlite | 245.582 | 238.293 | 244.478 |

##### 3. 16K数据包,4个IO线程

| 连接数 | 1000 | 2000 | 10000 |
| --------- | ------- | ------- | ------- |
| muduo | 596.692 | 598.942 | 588.075 |
| libevlite | 630.339 | 598.021 | 571.337 |
4 changes: 2 additions & 2 deletions libevlite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@
isa = XCBuildConfiguration;
buildSettings = {
DYLIB_COMPATIBILITY_VERSION = 9;
DYLIB_CURRENT_VERSION = 9.5.2;
DYLIB_CURRENT_VERSION = 9.6.1;
EXECUTABLE_PREFIX = "";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand All @@ -599,7 +599,7 @@
isa = XCBuildConfiguration;
buildSettings = {
DYLIB_COMPATIBILITY_VERSION = 9;
DYLIB_CURRENT_VERSION = 9.5.2;
DYLIB_CURRENT_VERSION = 9.6.1;
EXECUTABLE_PREFIX = "";
PRODUCT_NAME = "$(TARGET_NAME)";
};
Expand Down
1 change: 0 additions & 1 deletion src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ int32_t _transmit( struct session * session )

int32_t iov_size = 0;
struct iovec iov_array[iov_max];
memset( iov_array, 0, sizeof(iov_array) );

for ( i = 0; i < session_sendqueue_count(session) && iov_size < iov_max; ++i )
{
Expand Down
11 changes: 10 additions & 1 deletion src/iolayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,12 @@ int32_t iolayer_send( iolayer_t self, sid_t id, const char * buf, uint32_t nbyte

int32_t iolayer_broadcast( iolayer_t self, sid_t * ids, uint32_t count, const char * buf, uint32_t nbytes )
{
uint8_t i = 0;
if ( unlikely( ids == NULL || count == 0 ) )
{
return 0;
}

uint8_t i = 0;
pthread_t threadid = pthread_self();
struct iolayer * layer = (struct iolayer *)self;

Expand Down Expand Up @@ -594,6 +598,11 @@ int32_t iolayer_shutdown( iolayer_t self, sid_t id )

int32_t iolayer_shutdowns( iolayer_t self, sid_t * ids, uint32_t count )
{
if ( unlikely( ids == NULL || count == 0 ) )
{
return 0;
}

uint8_t i = 0;
struct iolayer * layer = (struct iolayer *)self;

Expand Down
5 changes: 3 additions & 2 deletions src/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include <sys/types.h>
#include <sys/event.h>
Expand Down Expand Up @@ -127,7 +128,7 @@ int32_t kqueue_add( void * arg, struct event * ev )
struct kevent kev;
struct kqueuer * poller = (struct kqueuer *)arg;

memset( &kev, 0, sizeof(kev) );
bzero( &kev, sizeof(kev) );

if ( ev->events & EV_READ )
{
Expand Down Expand Up @@ -186,7 +187,7 @@ int32_t kqueue_del( void * arg, struct event * ev )
return 0;
}

memset( &kev, 0, sizeof(kev) );
bzero( &kev, sizeof(kev) );

if ( ev->events & EV_READ )
{
Expand Down
14 changes: 3 additions & 11 deletions src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ struct hashnode * _find_table( struct hashtable * table, sid_t id, int32_t flag
struct hashnode * node = NULL;
struct hashnode * entries = table->entries + bucket;

do
for ( ; entries != NULL; entries = entries->next )
{
if ( entries->session != NULL )
{
Expand All @@ -568,15 +568,7 @@ struct hashnode * _find_table( struct hashtable * table, sid_t id, int32_t flag
{
node = entries;
}

if ( entries->next == NULL )
{
break;
}

entries = entries->next;
}
while ( 1 );

if ( node == NULL && flag != 0 )
{
Expand Down Expand Up @@ -623,7 +615,7 @@ struct session * _find_session( struct hashtable * table, sid_t id )
{
return NULL;
}
if ( node->session == NULL )
else if ( node->session == NULL )
{
return NULL;
}
Expand All @@ -640,7 +632,7 @@ int32_t _remove_session( struct hashtable * table, struct session * s )
{
return -1;
}
if ( node->session == NULL )
else if ( node->session == NULL )
{
return -2;
}
Expand Down
8 changes: 5 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <assert.h>
#include <strings.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
Expand Down Expand Up @@ -105,7 +106,7 @@ int32_t tcp_accept( int32_t fd, char * remotehost, uint16_t * remoteport )
*remoteport = 0;
remotehost[0] = 0;

memset( &in_addr, 0, sizeof(in_addr) );
bzero( &in_addr, sizeof(in_addr) );

cfd = accept( fd, (struct sockaddr *)&in_addr, &len );
if ( cfd != -1 )
Expand All @@ -131,7 +132,7 @@ int32_t tcp_listen( const char * host, uint16_t port, void (*options)(int32_t) )
// 对描述符的选项操作
options( fd );

memset( &addr, 0, sizeof(addr) );
bzero( &addr, sizeof(addr) );
addr.sin_family = AF_INET;
addr.sin_port = htons( port );
if ( host == NULL
Expand Down Expand Up @@ -184,7 +185,7 @@ int32_t tcp_connect( const char * host, uint16_t port, void (*options)(int32_t)
// 对描述符的选项操作
options( fd );

memset( &addr, 0, sizeof(addr) );
bzero( &addr, sizeof(addr) );
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, host, (void *)&(addr.sin_addr.s_addr));
Expand Down Expand Up @@ -371,6 +372,7 @@ struct msgqueue * msgqueue_create( uint32_t size )
int32_t rc = -1;
int32_t fds[2] = { -1, -1 };

// TODO: linux2.6直接使用eventfd
rc = pipe( fds );
//rc = socketpair( AF_UNIX, SOCK_STREAM, 0, fds );
if ( rc == -1 )
Expand Down
18 changes: 11 additions & 7 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ int32_t tcp_connect( const char * host, uint16_t port, void (*options)(int32_t)
uint32_t getpower( uint32_t size );
uint32_t nextpow2( uint32_t size );

/*
* sidlist
*/
//
// sidlist
//
struct sidlist
{
uint32_t count;
Expand All @@ -78,6 +78,10 @@ int32_t sidlist_adds( struct sidlist * self, sid_t * ids, uint32_t count );
sid_t sidlist_del( struct sidlist * self, int32_t index );
void sidlist_destroy( struct sidlist * self );

//
// 消息队列
//

// 任务类型
enum
{
Expand Down Expand Up @@ -108,10 +112,10 @@ struct task
QUEUE_PADDING_HEAD(taskqueue, struct task);
QUEUE_PROTOTYPE(taskqueue, struct task)

/*
* 消息队列
* 线程安全的消息队列, 有通知的功能
*/
//
// 消息队列
// 线程安全的消息队列, 有通知的功能
//
struct msgqueue
{
struct taskqueue queue;
Expand Down
9 changes: 0 additions & 9 deletions test/chatroom_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,6 @@ void CChatRoomService::run()
std::swap( swapqueue, m_TaskQueue );
pthread_mutex_unlock( &m_TaskLock );

if ( !m_Perform )
{
TASK * t = new TASK();
t->data = 1000;
perform( t, t->clone, t->perform );
m_Perform = true;
}

for ( std::deque<Task>::iterator it = swapqueue.begin(); it != swapqueue.end(); ++it )
{
Task * task = &(*it);
Expand All @@ -237,7 +229,6 @@ void CChatRoomService::run()
case 0 :
{
m_SessionMap.push_back( task->sid );
perform( task->sid, 1, (void *)(++m_UniqueID) );
}
break;

Expand Down
2 changes: 2 additions & 0 deletions test/echoserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ int main()
return -1;
}

service->start();

if ( !service->listen( "127.0.0.1", 9029 ) )
{
printf("service start failed \n");
Expand Down
Loading

0 comments on commit 14e8321

Please sign in to comment.