-module(tester).
-include("common.hrl").

-export([
   test_online_num/1,
   test_scene_sign/0,
   test_lists_and_proplists/0,
   test_lists_and_proplists/3
]).

%% Apis ------------------------------
test_online_num(Repeat) ->
   F1 = fun(_) -> lib_world_boss:online_num() end,
   F2 = fun(_) -> lib_world_boss:online_num2() end,
   Tester = [
      {"lib_world_boss:online_num", F1},
      {"lib_world_boss:online_num2", F2}
   ],
   test:time_compare(Repeat, Tester).

test_lists_and_proplists() ->
   test_lists_and_proplists(10000, 10, 50).

test_lists_and_proplists(Repeat, ListSize, FindSize) ->
   {ToFindKeys, MaterialList} = gen_list_material(ListSize, FindSize),
   F1 = fun(_) -> [lists:keyfind(K, 1, MaterialList) || K <- ToFindKeys] end,
   F2 = fun(_) -> [proplists:get_value(K, MaterialList) || K <- ToFindKeys] end,
   Tester = [
      {"lists:keyfind/3", F1},
      {"proplists:get_value/2", F2}
   ],
   test:time_compare(Repeat, Tester).

%% @doc 测试地图区域
test_scene_sign() ->
   case conf_scene_list:get_id_list() of
      SceneList when SceneList =/= [] ->
         SceneID = util:list_rand(SceneList),
         test_scene_sign(10000, SceneID, 100);
      _ ->
         ignore
   end.

test_scene_sign(Repeat, SceneID, FindSize) ->
   {MaxX, MaxY} = lib_scene_sign:get_max_xy(SceneID),
   PosList = [{util:rand(1, MaxX), util:rand(1, MaxY)} || _ <- lists:seq(1, FindSize)],
   %% 从s3取svr_scene_sign模块，并将svr_scene_sign:load_sign/1改为call
   svr_scene_sign:ensure_scene_sign(SceneID),
   F1 = fun(_) ->
      [svr_scene_sign:get_scene_poses({SceneID, X, Y}) || {X, Y} <- PosList]
        end,
   Ets = conf_scene:get_ets(SceneID),
   F2 = fun(_) ->
      [lib_scene_sign:is_pos_blocked(Ets, {SceneID, X, Y}) || {X, Y} <- PosList]
        end,
   F3 = fun(_) ->
      [lib_scene_sign:is_pos_blocked({SceneID, X, Y}) || {X, Y} <- PosList]
        end,
   Tester = [
      {"scene_sign+gen_server", F1},
      {"scene_sign+ets", F2},
      {"scene_sign+ets", F3}
   ],
   test:time_compare(Repeat, Tester).


%% Privates --------------------------

gen_list_material(ListSize, FindSize) ->
   Keys = [list_to_atom(lists:concat([k, I])) || I <- lists:seq(1, ListSize)],
   Values = [list_to_atom(lists:concat([v, I])) || I <- lists:seq(1, ListSize)],
   List = lists:zip(Keys, Values),
   ToFind = util:gen_n(FindSize, Keys),
   {ToFind, List}.
