Skip to content

Commit

Permalink
Code for step 9
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jul 29, 2020
1 parent 2d25f31 commit 51ef58d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions uniform_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
:rand.seed(:exrop, {1, 2, 3})
:inets.start()
:ssl.start()

total_requests = 2_000
concurrency = 20

book_ids =
BookStore.Books.Book
|> BookStore.Repo.all()
|> Enum.map(fn %BookStore.Books.Book{id: id} -> id end)

request_book_ids =
1..ceil(total_requests / length(book_ids))
|> Enum.reduce([], fn _, acc ->
acc ++ book_ids
end)
|> Enum.shuffle()
|> Enum.take(total_requests)
|> Enum.zip(1..total_requests)

request_book_ids
|> Task.async_stream(
fn {book_id, count} ->
case rem(count, 3) do
0 ->
url = 'http://localhost:4000/api/books'
method = :get
:httpc.request(method, {url, []}, [], [])

1 ->
url = String.to_charlist("http://localhost:4000/api/books/#{book_id}")
method = :get
:httpc.request(method, {url, []}, [], [])

2 ->
url = String.to_charlist("http://localhost:4000/api/books/#{book_id}/order")
method = :post
:httpc.request(method, {url, [], 'application/json', '{}'}, [], [])
end
end,
max_concurrency: concurrency
)
|> Stream.run()

0 comments on commit 51ef58d

Please sign in to comment.