Skip to content

Instantly share code, notes, and snippets.

@xjia1
Last active December 10, 2015 22:08
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 xjia1/4499544 to your computer and use it in GitHub Desktop.
Save xjia1/4499544 to your computer and use it in GitHub Desktop.
The idea is to create M x N threads, and measure the time cost for every N threads created. After a thread is created, it sends a message to let the main process continue, and finishes execution.
-module(tcbenchnb).
-export([start/2, init/2, thread/1]).
start(Total_Rounds, Step) ->
spawn(?MODULE, init, [Total_Rounds, Step]).
init(Total_Rounds, Step) ->
Start_Time = os:timestamp(),
create_threads(0, 0, Total_Rounds, Step, Start_Time).
create_threads(Total_Rounds, _, Total_Rounds, _, _) ->
init:stop();
create_threads(Round, Step, Total_Rounds, Step, Last_Time) ->
wait_threads(Round, 0, Total_Rounds, Step, Last_Time);
create_threads(Round, Created, Total_Rounds, Step, Last_Time) ->
spawn(?MODULE, thread, [self()]),
create_threads(Round, Created+1, Total_Rounds, Step, Last_Time).
wait_threads(Round, Step, Total_Rounds, Step, Last_Time) ->
Time = os:timestamp(),
Diff = timer:now_diff(Time, Last_Time),
io:format("~p~n", [Diff div 1000]),
create_threads(Round+1, 0, Total_Rounds, Step, Time);
wait_threads(Round, Started, Total_Rounds, Step, Last_Time) ->
receive
started ->
wait_threads(Round, Started+1, Total_Rounds, Step, Last_Time)
end.
thread(Parent) ->
Parent ! started.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment