Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chasers committed May 19, 2020
1 parent ddbd53a commit 86584bf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Mix.Config
config :logger, :console, format: "$time $metadata[$level] $levelpad$message\n"

config :loadfest,
logflare_source_prod: "b03decb3-2cf9-4835-a3df-3d8502eade75",
logflare_source_prod: "a7394972-dd59-49e9-bbc3-2e8f3f6c1811",
logflare_source_dev: "d6b815d4-4596-4d88-be83-46f1a2e8d165",
logflare_source_stag: "e1474a2f-bdc1-46a8-a5b4-4816f59dcac3",
# logflare_endpoint_prod: "https://logflare.app/api/logs",
Expand Down
31 changes: 31 additions & 0 deletions lib/json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule LoadFest.Json do
import Enum

def event() do
%{
"manufacturer" => manu(),
"temp" => temp(),
"customer" => %{
"id" => "Qsx78Xfd",
"location" => %{
"address" => "123 W Main St",
"city" => "Phoenix",
"state" => "AZ",
"zip" => 85016
}
},
"tags" => tags(),
"type" => "fridge",
"door_status" => door()
}
end

defp manu(), do: random(["Zenith", "Sub Zero", "Wolf", "Bosch", "Kitchen Aid"])
defp temp(), do: random(1..100)
defp tags(), do: take(all_tags(), 3)

defp all_tags(),
do: shuffle(["stand-alone", "commercial", "restaurant", "glass-doors"])

defp door(), do: random(["open", "closed"])
end
61 changes: 60 additions & 1 deletion lib/loadfest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ defmodule LoadFest do
end
end

def post_async_json(count, env) do
for line <- 1..count do
Task.Supervisor.start_child(LoadFest.TaskSupervisor, fn ->
post_json("prod")
end)
end
end

@doc """
POSTs async to a logflare source with more options.
Expand All @@ -31,7 +39,7 @@ defmodule LoadFest do

for line <- 1..count do
Task.Supervisor.start_child(LoadFest.TaskSupervisor, fn ->
post("#{line}", env)
post_json(env)
end)
end
end
Expand All @@ -55,6 +63,18 @@ defmodule LoadFest do
end
end

def post_async_batch_json(its, sleep, count, env) do
for _a <- 1..its do
Process.sleep(sleep)

for line <- 1..count do
Task.Supervisor.start_child(LoadFest.TaskSupervisor, fn ->
post_json(env)
end)
end
end
end

@doc """
GETs a url a lot.
Expand Down Expand Up @@ -149,6 +169,45 @@ defmodule LoadFest do
)
end

def post_json(env) do
key = String.to_atom("logflare_api_key" <> "_" <> env)
source_key = String.to_atom("logflare_source" <> "_" <> env)
endpoint = String.to_atom("logflare_endpoint" <> "_" <> env)

api_key = Application.get_env(:loadfest, key)
source = Application.get_env(:loadfest, source_key)
url = Application.get_env(:loadfest, endpoint) <> "/json"
user_agent = "Loadfest"

headers = [
{"Content-type", "application/json"}
]

params = %{"source_id" => source, "api_key" => api_key}

batch =
0..4
|> Enum.map(fn _x -> LoadFest.Json.event() end)
|> Jason.encode!()

### Should pull metrics from HTTPoison to do this correctly.

prev = System.monotonic_time()

request =
HTTPoison.post!(url, batch, headers, params: params, hackney: [pool: :loadfest_pool])

next = System.monotonic_time()
diff = next - prev
response_headers = Enum.into(request.headers, %{})

Logger.info(
"#{request.status_code} | #{response_headers["x-rate-limit-source_remaining"]} | #{
diff / 1_000_000
}ms"
)
end

defp post_batch(line, env) do
key = String.to_atom("logflare_api_key" <> "_" <> env)
source_key = String.to_atom("logflare_source" <> "_" <> env)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule LoadFest.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:httpoison, "~> 1.4"},
{:httpoison, "~> 1.6.2"},
{:jason, "~> 1.0"},
{:credo, "~> 1.0.0", only: [:dev, :test], runtime: false}
]
Expand Down
24 changes: 12 additions & 12 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "1.0.5", "fdea745579f8845315fe6a3b43e2f9f8866839cfbc8562bb72778e9fdaa94214", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.15.1", "9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.5.1", "0f55b5b673b03c5c327dac7015a67cb571b99b631acc0bc1b0b98dcd6b9f2104", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"credo": {:hex, :credo, "1.0.5", "fdea745579f8845315fe6a3b43e2f9f8866839cfbc8562bb72778e9fdaa94214", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "16105fac37c5c4b3f6e1f70ba0784511fec4275cd8bb979386e3c739cf4e6455"},
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"},
"httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "aa2c74bd271af34239a3948779612f87df2422c2fdcfdbcec28d9c105f0773fe"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}

0 comments on commit 86584bf

Please sign in to comment.