/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. % create N processes, wait for them to send a msg and die.
  4. % bench repeats this experiments a few times
  5. bench(N) -> bench(10, N).
  6. % spawn N processes that block. Measure time taken to spawn. Not
  7. % a good round-trip test.
  8. % bbench repeats this experiments a few times
  9. bbench(N) -> bbench(10, N).
  10. %======================================================================
  11. bench(0,_) -> done;
  12. bench(M,N) ->
  13. start(N),
  14. % Wait a little for possible background cleanup to occur
  15. receive
  16. after 1000
  17. -> done
  18. end,
  19. bench(M-1, N).
  20. bbench(0, _) -> done;
  21. bbench(M, N) ->
  22. start(N, block),
  23. receive
  24. after 1000 -> done
  25. end,
  26. bbench(M-1, N).
  27. start(N) ->
  28. statistics(runtime),
  29. %io:format("spawning ~p procs~n", [N]),
  30. spawnN(N, self()),
  31. %io:format("waiting for them to finish~n"),
  32. wait(N, N).
  33. start(N, block) ->
  34. statistics(runtime),
  35. spawnN(N, nil),
  36. {_, T} = statistics(runtime),
  37. if T == 0 ->
  38. %io:format("Elapsed: ~p ms ~n", [T]);
  39. io:format("~p~n", [T]);
  40. true ->
  41. %io:format("Elapsed: ~p ms, ~p tasks/ms ~n", [T, N/T])
  42. io:format("~p~n", [T])
  43. end.
  44. wait(0, _) -> %wait(0, Total) ->
  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, Total/T])
  51. io:format("~p~n", [T])
  52. end;
  53. wait(N, Total) ->
  54. receive
  55. done ->
  56. wait(N-1, Total)
  57. end.
  58. spawnN(0, _) -> done;
  59. spawnN(N, Main) ->
  60. % if (N rem 50000 == 0) ->
  61. % io:format("#Procs: ~p ~n", [N]);
  62. % true -> true
  63. % end,
  64. spawn(numprocs, recv, [Main]),
  65. spawnN(N-1, Main).
  66. recv(Main) ->
  67. if is_pid(Main) ->
  68. Main ! done;
  69. true ->
  70. receive
  71. hello -> recv(Main)
  72. end
  73. end.