Skip to content

Instantly share code, notes, and snippets.

@methane
Created January 23, 2013 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/4602406 to your computer and use it in GitHub Desktop.
Save methane/4602406 to your computer and use it in GitHub Desktop.
micro benchmark for accepting.
/* 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);
}
}
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