Created
January 23, 2013 05:52
-
-
Save methane/4602406 to your computer and use it in GitHub Desktop.
micro benchmark for accepting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -O2 -o client -pthread uhello_client.c */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <pthread.h> | |
struct sockaddr_un remote; | |
void* attack(void* unused) | |
{ | |
int i, err; | |
char buf[128]; | |
for (i=0; i<10000; ++i) { | |
int s = socket(AF_UNIX, SOCK_STREAM, 0); | |
err = connect(s, (struct sockaddr *)&remote, sizeof(remote)); | |
if (err < 0) { | |
perror("connect"); | |
exit(1); | |
} | |
recv(s, buf, 100, 0); | |
close(s); | |
} | |
return NULL; | |
} | |
int main() | |
{ | |
const int C = 20; | |
int i; | |
remote.sun_family = AF_UNIX; | |
strcpy(remote.sun_path, "/tmp/uhello.sock"); | |
pthread_t infos[C]; | |
for (i = 0; i < C; ++i) { | |
pthread_create(&infos[i], NULL, attack, NULL); | |
} | |
for (i = 0; i < C; ++i) { | |
pthread_join(infos[i], NULL); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"os" | |
) | |
func handler(con net.Conn) { | |
defer con.Close() | |
con.Write([]byte("hello")) | |
} | |
func main() { | |
os.Remove("/tmp/uhello.sock") | |
ln, _ := net.Listen("unix", "/tmp/uhello.sock") | |
for { | |
con, err := ln.Accept() | |
if err != nil { | |
panic("accept error.") | |
} | |
go handler(con) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment