一个简单的基于epoll的web server,性能还不错


我根据一个epoll的模型改了一个http server出来。只有129行,还可以精简不少,呵呵。
小测了一下,一秒钟处理了一万了请求。当然这里只是把现成的东西输出。没考虑到发送数据处理。和请求的解析。
注意了,epoll只基于linux 2.6内核的。其他平台不能用。





/*-------------------------------------------------------------------------------------------------
gcc -o httpd httpd.c -lpthread
author: wyezl
2006.4.28
---------------------------------------------------------------------------------------------------*/

#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

#define PORT 8888
#define MAXFDS 5000
#define EVENTSIZE 100

#define BUFFER "HTTP/1.1 200 OK/r/nContent-Length: 5/r/nConnection: close/r/nContent-Type: text/html/r/n/r/nHello"

int epfd;
void *serv_epoll(void *p);
void setnonblocking(int fd)
{
    int opts;
    opts=fcntl(fd, F_GETFL);
    if (opts < 0)
    {
          fprintf(stderr, "fcntl failed/n");
          return;
    }
    opts = opts | O_NONBLOCK;
    if(fcntl(fd, F_SETFL, opts) < 0)
    {
          fprintf(stderr, "fcntl failed/n");
          return;
    }
    return;
}

int main(int argc, char *argv[])
{
    int fd, cfd,opt=1;
    struct epoll_event ev;
    struct sockaddr_in sin, cin;
    socklen_t sin_len = sizeof(struct sockaddr_in);
    pthread_t tid;
    pthread_attr_t attr;

    epfd = epoll_create(MAXFDS);
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
    {
          fprintf(stderr, "socket failed/n");
          return -1;
    }
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void*)&opt, sizeof(opt));

    memset(&sin, 0, sizeof(struct sockaddr_in));
    sin.sin_family = AF_INET;
    sin.sin_port = htons((short)(PORT));
    sin.sin_addr.s_addr = INADDR_ANY;
    if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) != 0)
    {
          fprintf(stderr, "bind failed/n");
          return -1;
    }
    if (listen(fd, 32) != 0)
    {
          fprintf(stderr, "listen failed/n");
          return -1;
    }

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    if (pthread_create(&tid, &attr, serv_epoll, NULL) != 0)
    {
          fprintf(stderr, "pthread_create failed/n");
          return -1;
    }

    while ((cfd = accept(fd, (struct sockaddr *)&cin, &sin_len)) > 0)
    {
          setnonblocking(cfd);
          ev.data.fd = cfd;
          ev.events = EPOLLIN | EPOLLET;
          epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);
          //printf("connect from %s/n",inet_ntoa(cin.sin_addr));
          //printf("cfd=%d/n",cfd);
    }

    if (fd > 0)
          close(fd);
    return 0;
}

void *serv_epoll(void *p)
{
    int i, ret, cfd, nfds;;
    struct epoll_event ev,events[EVENTSIZE];
    char buffer[512];

    while (1)
    {
          nfds = epoll_wait(epfd, events, EVENTSIZE , -1);
          //printf("nfds ........... %d/n",nfds);
          for (i=0; i<nfds; i++)
          {
                if(events[i].events & EPOLLIN)
                {
                    cfd = events[i].data.fd;
                    ret = recv(cfd, buffer, sizeof(buffer),0);
                    //printf("read ret..........= %d/n",ret);

                    ev.data.fd = cfd;
                    ev.events = EPOLLOUT | EPOLLET;
                    epoll_ctl(epfd, EPOLL_CTL_MOD, cfd, &ev);
                }
                else if(events[i].events & EPOLLOUT)
                {
                    cfd = events[i].data.fd;
                    ret = send(cfd, BUFFER, strlen(BUFFER), 0);
                    //printf("send ret...........= %d/n", ret);

                    ev.data.fd = cfd;
                    epoll_ctl(epfd, EPOLL_CTL_DEL, cfd, &ev);
                    //shutdown(cfd, 1);
                    close(cfd);

                }
          }
    }
    return NULL;
}




下面是测试结果:

[yangjian2@localhost bin]$ ./ab -c 50 -n 10000 [url]http://202.108.xxx.xxx:8888/[/url]
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, [url]http://www.zeustech.net/[/url]
Copyright (c) 1998-2002 The Apache Software Foundation, [url]http://www.apache.org/[/url]

Benchmarking 202.108.xxx.xxx (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software:    
Server Hostname:     202.108.xxx.xxx
Server Port:         8888

Document Path:       /
Document Length:     5 bytes

Concurrency Level:     50
Time taken for tests:   0.921732 seconds
Complete requests:     10000
Failed requests:     0
Write errors:       0
Total transferred:     872088 bytes
HTML transferred:     50120 bytes
Requests per second:   10849.14 [#/sec] (mean)
Time per request:     4.609 [ms] (mean)
Time per request:     0.092 [ms] (mean, across all concurrent requests)
Transfer rate:       923.26 [Kbytes/sec] received

Connection Times (ms)
        min mean[+/-sd] median   max
Connect:     1   1   0.9     2     3
Processing:   1   2   0.5     2     4
Waiting:     0   1   0.3     1     3
Total:       4   4   0.2     4     5
WARNING: The median and mean for the initial connection time are not within a normal deviation
    These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
50%     4
66%     4
75%     4
80%     4
90%     4
95%     4
98%     5
99%     5
100%     5 (longest request)



我的blog:
http://blog.sina.com.cn/u/1181509184
[0楼] | IP:已记录| Posted:2006-04-29 5:31 PM|
Logo

更多推荐