Skip to content

Commit 51ef58d

Browse files
committed
Code for step 9
1 parent 2d25f31 commit 51ef58d

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

uniform_test.exs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
:rand.seed(:exrop, {1, 2, 3})
2+
:inets.start()
3+
:ssl.start()
4+
5+
total_requests = 2_000
6+
concurrency = 20
7+
8+
book_ids =
9+
BookStore.Books.Book
10+
|> BookStore.Repo.all()
11+
|> Enum.map(fn %BookStore.Books.Book{id: id} -> id end)
12+
13+
request_book_ids =
14+
1..ceil(total_requests / length(book_ids))
15+
|> Enum.reduce([], fn _, acc ->
16+
acc ++ book_ids
17+
end)
18+
|> Enum.shuffle()
19+
|> Enum.take(total_requests)
20+
|> Enum.zip(1..total_requests)
21+
22+
request_book_ids
23+
|> Task.async_stream(
24+
fn {book_id, count} ->
25+
case rem(count, 3) do
26+
0 ->
27+
url = 'http://localhost:4000/api/books'
28+
method = :get
29+
:httpc.request(method, {url, []}, [], [])
30+
31+
1 ->
32+
url = String.to_charlist("http://localhost:4000/api/books/#{book_id}")
33+
method = :get
34+
:httpc.request(method, {url, []}, [], [])
35+
36+
2 ->
37+
url = String.to_charlist("http://localhost:4000/api/books/#{book_id}/order")
38+
method = :post
39+
:httpc.request(method, {url, [], 'application/json', '{}'}, [], [])
40+
end
41+
end,
42+
max_concurrency: concurrency
43+
)
44+
|> Stream.run()

0 commit comments

Comments
 (0)