SHARE
TWEET

Untitled

a guest Jul 17th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. defmodule Scenario.WS.PingPong do
  2.   use Chaperon.Scenario
  3.  
  4.   def init(session) do
  5.     # We can add custom session setup logic in an `init/1` function, if we need to.
  6.     # Returns {:ok, session} or {:error, reason} (see `Chaperon.Session` module)
  7.  
  8.     session
  9.     |> ws_connect("/socket.io/?EIO=3&transport=websocket")
  10.     |> ok
  11.   end
  12.  
  13.   def run(session) do
  14.     # Accessing config values using the `Session.config/2` helper function.
  15.     # Alternatively we could have just accessed `session.config.ping_pong.iterations`
  16.     # but using the helper function as we do here gives us better error messages
  17.     # in case we didn't define the config value for this session.
  18.  
  19.     iterations = session |> config([:ping_pong, :iterations])
  20.  
  21.     # This will call `ping_pong/1` repeatedly for `iterations` amount of times
  22.     # and record the duration of calling it in a histogram
  23.  
  24.     :timer.sleep(5000);
  25.  
  26.     session
  27.     |> repeat_traced(:ping_pong, iterations)
  28.     |> log_info("PingPong finished after #{iterations} iterations")
  29.   end
  30.  
  31.   def ping_pong(session) do
  32.     session
  33.     |> ws_send("42[\"xxx\",\"true\"]")
  34.     |> ws_await_recv(~r/^(.*)zzz(.*)$/)
  35.   end
  36.  
  37.   def teardown(session) do
  38.     # We can also define a `teardown/1` function which then gets called with our
  39.     # session after we successfully ran our `run/1` defined above.
  40.     # This is useful for cleaning up resources or performing other logic after
  41.     # we've run our load test scenario.
  42.     # Note that any actions in this code will not be traced and no metrics for
  43.     # them will be recorded in the final metrics histogram output.
  44.  
  45.     session
  46.     |> ws_close
  47.   end
  48. end
  49.  
  50. defmodule LoadTest.PingPong do
  51.   use Chaperon.LoadTest
  52.  
  53.   def default_config, do: %{
  54.     base_url: "ws://127.0.0.1:3456"
  55.   }
  56.  
  57.   # run 100 PingPong sessions (each with 10 iterations) across the cluster
  58.   # `scenario/0` is expected to return a list of 2-tuples (`{scenario_module, config}`)
  59.   # for each load test.
  60.   # `scenario_module` can be `{concurrency, scenario_module}` if running multiple
  61.   # concurrent instances of the scenario is needed
  62.   def scenarios, do: [
  63.     {{1, Scenario.WS.PingPong}, %{
  64.       ping_pong: %{
  65.         iterations: 10
  66.       }
  67.     }}
  68.   ]
  69. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top