PageRenderTime 171ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/grinderl/test/test.erl

http://grinderl.googlecode.com/
Erlang | 158 lines | 113 code | 12 blank | 33 comment | 0 complexity | aaeb4269c5606473df5ecc050bc5a09e MD5 | raw file
Possible License(s): GPL-2.0
  1. -module(test).
  2. -include("include/grinderl.hrl").
  3. -export([
  4. init/0,
  5. test_seq1/0,
  6. test_con1/0,
  7. test_seq2/0,
  8. test_con2/0,
  9. test_seq3/0,
  10. test_con3/0
  11. ]).
  12. init() ->
  13. % add some nodes to grinderl
  14. grinderl:add_nodes([node(), node()]).
  15. test_seq1() ->
  16. % create task1 and run it on each node 3 times, sleeping 1s each time
  17. Test = create_test1({sequence, 1000}, 3),
  18. grinderl:run_test(Test).
  19. test_con1() ->
  20. % create task1 and run it on each node 3 times concurrently
  21. Test = create_test1(concurrent, 3),
  22. grinderl:run_test(Test).
  23. create_test1(Mode, Repeat) ->
  24. % Function to evaluate for the task:
  25. % Write a silly message on DEBUG stream about its arguments.
  26. % got 2 arguments:
  27. % * a 'writer' name
  28. % * a 'writen' value
  29. % return 2 values which will be manage by grinderl:
  30. % * the time of execution,
  31. % * a 2-tuple of its 2 arguments
  32. TaskFun = fun(Writer, WritenValue) ->
  33. FWrite = fun() -> ?DEBUG(
  34. "task1's function got two arguments: ~s, ~w",
  35. [Writer, WritenValue]
  36. ) end,
  37. {WriteTime, _Res} = grd_util:timeit(FWrite),
  38. {ok, self(), [WriteTime, {Writer, WritenValue}]}
  39. end,
  40. % Test description
  41. #test{
  42. nick = 'Test 1',
  43. task = #task{
  44. callable = TaskFun,
  45. args_spec = [
  46. % 1st argument of the task will be taken in sequence
  47. {seq, ["bea", "pouf", "paf"]},
  48. % 2nd argument of the task will be choosen at random
  49. {choice, [0, 1, 12, 42, 77]}
  50. ],
  51. result_spec = [ % list of task results
  52. % 1st result of the task will be memorized as a list of values
  53. {mean, writetime},
  54. % 2nd result of the task will be a count per return value
  55. {count, writer_val}
  56. ]
  57. },
  58. mode = Mode,
  59. repeat_n = Repeat
  60. }.
  61. test_seq2() ->
  62. % create task1 and run it on each node 3 times, sleeping 1s each time
  63. Test = create_test2({sequence, 1000}, 3),
  64. grinderl:run_test(Test).
  65. test_con2() ->
  66. % create task1 and run it on each node 3 times concurrently
  67. Test = create_test2(concurrent, 3),
  68. grinderl:run_test(Test).
  69. create_test2(Mode, Repeat) ->
  70. % use 'ping' command line to retrieve its raw output (text)
  71. #test{
  72. nick = 'Textual ping by command line',
  73. task = #task{
  74. callable = grd_extcmd:command_2_taskfun(
  75. % use "ping" command line as a function
  76. "ping -n -c 1",
  77. % how many argument on command line? =>
  78. 1
  79. ),
  80. args_spec = [
  81. % choose at random a host to ping
  82. {choice, ["127.0.0.1", "localhost"]}
  83. ],
  84. result_spec = [
  85. {acc, ping_raw_output}
  86. ]
  87. },
  88. mode = Mode,
  89. repeat_n = Repeat
  90. }.
  91. test_seq3() ->
  92. % create task1 and run it on each node 3 times, sleeping 1s each time
  93. Test = create_test3({sequence, 1000}, 3),
  94. grinderl:run_test(Test).
  95. test_con3() ->
  96. % create task1 and run it on each node 3 times concurrently
  97. Test = create_test3(concurrent, 3),
  98. grinderl:run_test(Test).
  99. create_test3(Mode, Repeat) ->
  100. % use 'ping' command line, retrieve and parse its output
  101. #test{
  102. nick = 'Textual ping by command line',
  103. task = #task{
  104. callable = grd_extcmd:command_2_taskfun(
  105. % use "ping" command line as a function
  106. "ping -n -c 1",
  107. % how many argument on command line? =>
  108. 1,
  109. % how to interpret output of this command
  110. fun ping_output_parser/1
  111. ),
  112. args_spec = [
  113. % choose at random a host to ping
  114. {choice, ["127.0.0.1", "localhost"]}
  115. ],
  116. result_spec = [
  117. {acc, nicer_output}
  118. ]
  119. },
  120. mode = Mode,
  121. repeat_n = Repeat
  122. }.
  123. ping_output_parser({0, Data}) ->
  124. % Data is the rw output of the command
  125. % ... let's extract some information
  126. % WARNING: no test at all! may failed if the output of ping is slightly
  127. % different of what I had during my tests...
  128. Str = lists:flatten(Data),
  129. {match, IPStart, IPLength} = regexp:match(
  130. Str,
  131. "\([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\)"
  132. ),
  133. IPStr = string:substr(Str, IPStart + 1, IPLength - 2),
  134. {match, TimesStart, TimesLength} = regexp:match(
  135. Str,
  136. "([0-9]+\.[0-9]+)/([0-9]+\.[0-9]+)/([0-9]+\.[0-9]+)/([0-9]+\.[0-9]+)"
  137. ),
  138. TimesStr = string:substr(Str, TimesStart, TimesLength),
  139. {ok, TimeStrings} = regexp:split(TimesStr, "/"),
  140. Times = lists:map(
  141. fun(FStr) -> {F,_R} = string:to_float(FStr), F end,
  142. TimeStrings
  143. ),
  144. {ok, self(), [{IPStr, Times}]};
  145. ping_output_parser({Status, Data}) ->
  146. {error, self(), [Status, Data]}.