is OpenBSD 10x faster than Linux?
Here’s a little benchmark complements of Jann Horn. It’s unexpectedly slow on Linux.
OpenBSD is so fast, I had to modify the program slightly to measure itself, as the time utility is missing sufficient precision to even record nonzero.
All it does is create one extra thread, then both existing threads create 256 sockets. What’s so hard about that?
#include <pthread.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/socket.h>
static void open_sockets(void) {
for (int i=0; i<256; i++) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
err(1, "socket");
}
}
static void *thread_fn(void *dummy) {
open_sockets();
return NULL;
}
int main(int argc) {
struct timeval one, two;
gettimeofday(&one, NULL);
if (argc > 1)
dup2(0, 666);
pthread_t thread;
if (pthread_create(&thread, NULL, thread_fn, NULL))
errx(1, "pthread_create");
open_sockets();
if (pthread_join(thread, NULL))
errx(1, "pthread_join");
gettimeofday(&two, NULL);
timersub(&two, &one, &one);
printf("elapsed: %lld.%06lds\n", one.tv_sec, one.tv_usec);
return 0;
}
On Linux, I get results approximately as so:
tedu@penguin:~$ ./a.out
elapsed: 0.017770s
tedu@penguin:~$ ./a.out
elapsed: 0.026309s
tedu@penguin:~$ ./a.out
elapsed: 0.018414s
On OpenBSD, here we go, choo choo:
ox$ ./a.out
a.out: a.out: socketsocket: : Too many open files
Too many open files
ox$ ulimit -n 1024
ox$ ./a.out
elapsed: 0.006096s
ox$ ./a.out
elapsed: 0.002508s
ox$ ./a.out
elapsed: 0.002326s
These aren’t identical machines, but roughly comparable.
There’s a hint in the code (nothing to do with networking code, if that was your first guess), with more explanation in the linked thread, which is worth reading and some thinking. I’d love to see the system and benchmark where Linux outperforms here.
Really, I just found it a little funny. Usually it’s the weirdo benchmark that shows OpenBSD being 10x slower, so this one is definitely going in the collection.
Tagged: openbsd