shithub: bench9

ref: 42e3ee2d9508932863c789041c71b7ae6c813f49
dir: /main.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include "b.h"

static void
bmallocfree(int out)
{
	Bgr g;
	B *bm, *bf;
	int sz[] = {
		{0},
		{1},
		{16},
		{64},
		{256},
		{1*Kb},
		{4*Kb},
		{64*Kb},
		{1*Mb},
		{2*Mb},
		{4*Mb},
	};
	int i;
	void *ptr;
	char t[32];

	benchinitgr(&g, "malloc & free");
	for(i = 0; i < nelem(sz); i++){
		snprint(t, sizeof(t), "malloc/%d", sz[i]);
		bm = benchadd(&g, t);
		snprint(t, sizeof(t), "free/%d", sz[i]);
		bf = benchadd(&g, t);

		for(; bm->n > 0 || bf->n > 0;){
			benchin(bm);
			ptr = malloc(sz[i]);
			benchout(bm);

			benchin(bf);
			free(ptr);
			benchout(bf);
		}
	}

	benchprintgr(&g, out);
	benchfreegr(&g);
}

static void
bzeronull(int out)
{
	Bgr g;
	B *br, *bw;
	int sz[] = {
		{0},
		{1},
		{64*Kb},
		{1*Mb},
		{4*Mb},
		{16*Mb},
		{64*Mb},
		{256*Mb},
	};
	int i, zero, null;
	void *ptr;
	char t[32];

	ptr = malloc(sz[nelem(sz)-1]);
	if((zero = open("/dev/zero", OREAD)) < 0)
		sysfatal("%r");
	if((null = open("/dev/null", OWRITE)) < 0)
		sysfatal("%r");
	benchinitgr(&g, "/dev/zero → /dev/null");
	for(i = 0; i < nelem(sz); i++){
		snprint(t, sizeof(t), "read/%d", sz[i]);
		br = benchadd(&g, t);
		snprint(t, sizeof(t), "write/%d", sz[i]);
		bw = benchadd(&g, t);

		for(; br->n > 0 || bw->n > 0;){
			benchin(br);
			if(readn(zero, ptr, sz[i]) != sz[i])
				sysfatal("/dev/zero: readn");
			benchout(br);

			benchin(bw);
			if(write(null, ptr, sz[i]) != sz[i])
				sysfatal("/dev/null: write");
			benchout(bw);
		}
	}

	benchprintgr(&g, out);
	benchfreegr(&g);

	close(zero);
	close(null);
	free(ptr);
}

void
threadmain(int argc, char **argv)
{
	ARGBEGIN{
	}ARGEND

	if(benchwire(0) != 0)
		fprint(2, "failed to wire: %r\n");

	bmallocfree(1);
	bseparator(1);
	bzeronull(1);

	threadexitsall(nil);
}