-module(write_test).
-export([start/1]).
-record(table_1, {id, key, value}).

%% For test run this function
%% example:
%% write_test:start(1000000).
start(N)->
    io:format("Mnesia memory leakage test~n"),
    %%erlang:system_flag(fullsweep_after, 0),
    init_db(),
    io:format("Starting test~n"),
    run(N).


init_db()->
    io:format("Stopping mnesia~n"),
    R1 = mnesia:stop(),
    R2 = mnesia:delete_schema([node()]),
    R3 = mnesia:create_schema([node()]),
    timer:sleep(1000),
    io:format("Starting mnesia~n"),
    R4 = mnesia:start(),
    R5 = mnesia:create_table(table_1,
			     [{attributes,
			       record_info(fields,table_1)},
			      {type, bag},{ram_copies, [node()]}]),
    io:format("Mnesia stop,delete_schema,create_schema,~n"
	      "start,create_schema:~n~p~n", [{R1,R2,R3,R4,R5}]).

set_value(ID, Key, Value)->
    TableParam = #table_1{id = ID, key = Key, value = Value},
    Fun = fun() ->
		  mnesia:write(TableParam)
	  end,
    mnesia:transaction(Fun).

run(0) ->
    io:format("Memory usage: ~p~n", [erlang:memory()]),
    ok;
run(N) ->
    A = "aaaaaaaaaa",
    set_value("Index", some_atom, A),
    %%io:format("Step: ~w~n", [N]),
    %%timer:sleep(5),
    run(N-1).

