Stampede.HTTP (stampede v0.2.0)

Copy Markdown View Source

An HTTP client for load tests.

{:ok, %{status: 200, body: body}} = Stampede.HTTP.get("http://localhost:4000/api/posts")
{:ok, %{status: 201}} = Stampede.HTTP.post("http://localhost:4000/api/posts", json: %{title: "Hi"})

Responses are maps with the :status, the :headers, with lowercase names, and the :body.

Each VU keeps a connection open to every server it talks to and sends its next request on it, as a browser does. When the server closed it in the meantime, a new one is opened, and a request that is safe to repeat (GET, HEAD, PUT, DELETE or OPTIONS) is sent again on it.

Metrics

Inside a run, requests record:

  • "http.request" - how long each response took, whatever its status
  • "http.request.errors" - requests that failed, or whose response had a status of 400 or more

Summary

Functions

Sends a DELETE request. See request/3.

Sends a GET request. See request/3.

Sends a PATCH request. See request/3.

Sends a POST request. See request/3.

Sends a PUT request. See request/3.

Sends a method request to url and waits for the whole response.

Types

response()

@type response() :: %{
  status: non_neg_integer(),
  headers: [{String.t(), String.t()}],
  body: binary()
}

Functions

delete(url, opts \\ [])

@spec delete(String.t(), keyword()) :: {:ok, response()} | {:error, term()}

Sends a DELETE request. See request/3.

get(url, opts \\ [])

@spec get(String.t(), keyword()) :: {:ok, response()} | {:error, term()}

Sends a GET request. See request/3.

patch(url, opts \\ [])

@spec patch(String.t(), keyword()) :: {:ok, response()} | {:error, term()}

Sends a PATCH request. See request/3.

post(url, opts \\ [])

@spec post(String.t(), keyword()) :: {:ok, response()} | {:error, term()}

Sends a POST request. See request/3.

put(url, opts \\ [])

@spec put(String.t(), keyword()) :: {:ok, response()} | {:error, term()}

Sends a PUT request. See request/3.

request(method, url, opts \\ [])

@spec request(atom(), String.t(), keyword()) :: {:ok, response()} | {:error, term()}

Sends a method request to url and waits for the whole response.

Returns {:ok, response} whatever the status, and {:error, reason} when there is no response, such as a Mint.TransportError.

Options

  • :headers - a list of {name, value} headers
  • :body - the body, as iodata
  • :json - a term to send as JSON instead of a :body, with a content-type of application/json unless one is given
  • :timeout - how long to wait for the response, in milliseconds. Defaults to 5_000.
  • :metric - the timing to record the request as, or nil to record nothing. Defaults to "http.request".
  • :transport_opts - options for :gen_tcp or :ssl, given to Mint.HTTP.connect/4