PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/bench/erlang/numprocs.erl

http://github.com/kilim/kilim
Erlang | 86 lines | 56 code | 13 blank | 17 comment | 5 complexity | 4c7f5d14aefbc41ed2852c6341539b47 MD5 | raw file
 1-module(numprocs).
 2-export([bbench/1, bench/1, bench/2, start/1, start/2, recv/1]).
 3
 4
 5% create N processes, wait for them to send a msg and die. 
 6% bench repeats this experiments a few times
 7bench(N) ->  bench(10, N).
 8
 9
10% spawn N processes that block. Measure time taken to spawn. Not
11% a good round-trip test.
12% bbench repeats this experiments a few times
13bbench(N) ->  bbench(10, N).
14
15%======================================================================
16
17bench(0,_) -> done;
18bench(M,N) ->
19    start(N),
20    % Wait a little for possible background cleanup to occur
21    receive
22	after 1000
23	      -> done
24	end,
25    bench(M-1, N).
26
27bbench(0, _) -> done;
28bbench(M, N) -> 
29    start(N, block),
30    receive 
31	after 1000 -> done
32	end,
33    bbench(M-1, N).
34
35start(N) ->
36    statistics(runtime),
37    %io:format("spawning ~p procs~n", [N]),
38    spawnN(N, self()),
39    %io:format("waiting for them to finish~n"),
40    wait(N, N).
41
42start(N, block) ->
43    statistics(runtime),
44    spawnN(N, nil),
45    {_, T} = statistics(runtime),
46    if T == 0 ->
47	    %io:format("Elapsed: ~p ms ~n", [T]);
48	    io:format("~p~n", [T]);
49       true -> 
50	    %io:format("Elapsed: ~p ms, ~p tasks/ms ~n", [T, N/T])
51	    io:format("~p~n", [T])
52    end.
53
54wait(0, _)  ->    %wait(0, Total) ->
55    {_, T} = statistics(runtime),
56    if T == 0 ->
57	    %io:format("Elapsed: ~p ms ~n", [T]);
58	    io:format("~p~n", [T]);
59       true -> 
60	    %io:format("Elapsed: ~p ms, ~p tasks/ms ~n", [T, Total/T])
61	    io:format("~p~n", [T])
62    end;
63wait(N, Total) ->
64    receive 
65	done -> 
66	     wait(N-1, Total)
67    end.
68
69
70spawnN(0, _) -> done;
71spawnN(N, Main) -> 
72%    if (N rem 50000 == 0) ->
73%	    io:format("#Procs: ~p ~n", [N]);
74%       true -> true
75%    end,
76    spawn(numprocs, recv, [Main]),
77    spawnN(N-1, Main).
78
79recv(Main) ->
80    if is_pid(Main) -> 
81	    Main ! done;
82       true ->
83	    receive
84		hello -> recv(Main)
85	    end
86    end.