Skip to content

Commit

Permalink
update bandit + mist
Browse files Browse the repository at this point in the history
  • Loading branch information
rawhat committed Apr 7, 2024
1 parent b2136ae commit 67c1635
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 63 deletions.
2 changes: 1 addition & 1 deletion glandit/lib/glandit/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Glandit.Application do
children = [
# Starts a worker by calling: Glandit.Worker.start_link(arg)
# {Glandit.Worker, arg}
{Bandit, plug: Glandit.Plug, scheme: :http, options: [port: 8080]}
{Bandit, plug: Glandit.Plug, scheme: :http, port: 8080}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
4 changes: 4 additions & 0 deletions glandit/lib/glandit/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defmodule Glandit.Plug do
send_resp(conn, 200, "")
end

post "/" do
send_resp(conn, 200, "")
end

get "/user/:id" do
send_resp(conn, 200, id)
end
Expand Down
4 changes: 2 additions & 2 deletions glandit/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule Glandit.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:bandit, "~> 0.5"},
{:plug, "~> 1.13"},
{:bandit, "~> 1.4"},
{:plug, "~> 1.13"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
Expand Down
15 changes: 8 additions & 7 deletions glandit/mix.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
%{
"bandit": {:hex, :bandit, "0.5.0", "188c1d3ade5760eded7a08798df9e231c8f07e406505a913629dd217b6fa96a7", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.12", [hex: :plug, repo: "hexpm", optional: false]}, {:thousand_island, "~> 0.5.7", [hex: :thousand_island, repo: "hexpm", optional: false]}], "hexpm", "2e062f7e9b72e1bfba7bceecfb7566700a8fbe71f91dd7add6c71f584e67d445"},
"hpax": {:hex, :hpax, "0.1.1", "2396c313683ada39e98c20a75a82911592b47e5c24391363343bde74f82396ca", [:mix], [], "hexpm", "0ae7d5a0b04a8a60caf7a39fcf3ec476f35cc2cc16c05abea730d3ce6ac6c826"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"plug": {:hex, :plug, "1.13.6", "187beb6b67c6cec50503e940f0434ea4692b19384d47e5fdfd701e93cadb4cc2", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "02b9c6b9955bce92c829f31d6284bf53c591ca63c4fb9ff81dfd0418667a34ff"},
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"thousand_island": {:hex, :thousand_island, "0.5.8", "0f54a5f18a3ce214a9cbbb465249cf48a9aaeeeb137912a46404076bf9420d37", [:mix], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2dab930618dcd61732b4aa2dfbdddd4febc10b7e57dcc596c258e23aa72748ea"},
"bandit": {:hex, :bandit, "1.4.2", "a1475c8dcbffd1f43002797f99487a64c8444753ff2b282b52409e279488e1f5", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "3db8bacea631bd926cc62ccad58edfee4252d1b4c5cccbbad9825df2722b884f"},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"plug": {:hex, :plug, "1.15.3", "712976f504418f6dff0a3e554c40d705a9bcf89a7ccef92fc6a5ef8f16a30a97", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cc4365a3c010a56af402e0809208873d113e9c38c401cabd88027ef4f5c01fd2"},
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"thousand_island": {:hex, :thousand_island, "1.3.5", "6022b6338f1635b3d32406ff98d68b843ba73b3aa95cfc27154223244f3a6ca5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2be6954916fdfe4756af3239fb6b6d75d0b8063b5df03ba76fd8a4c87849e180"},
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
}
14 changes: 7 additions & 7 deletions glist/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ version = "0.1.0"
# links = [{ title = "Website", href = "https://gleam.run" }]

[dependencies]
gleam_stdlib = "~> 0.20"
mist = "~> 0.4"
gleam_http = "~> 3.0"
gleam_erlang = "~> 0.9"
glisten = "~> 0.3"
gleam_otp = "~> 0.4"
gleam_stdlib = "~> 0.36"
gleam_http = "~> 3.6"
gleam_erlang = "~> 0.25"
gleam_otp = "~> 0.10"
# mist = "~> 1.0"
mist = { path = "../../../gleams/mist" }

[dev-dependencies]
gleeunit = "~> 0.6"
gleeunit = "~> 1.0"
32 changes: 18 additions & 14 deletions glist/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_erlang", version = "0.9.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0430CFF5DAFAB54C97D6F19AA98BDDAD5E7AA245AC240547F4C0DBC26CDAE85E" },
{ name = "gleam_http", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "02043655C5EEBBF533CC36BD95F881C94EE1B5959EA474CA3F597672716DB443" },
{ name = "gleam_otp", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "3F05090CEDFBA5B6DE2AEE20868F92E815D287E71807DAD5663DAA1B566953C2" },
{ name = "gleam_stdlib", version = "0.21.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "FB03CD50B477867DA65B1318252ABA02F76175754762D15BF6C792CF5B85BCC4" },
{ name = "gleeunit", version = "0.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "5BF486C3E135B7F5ED8C054925FC48E5B2C79016A39F416FD8CF2E860520EE55" },
{ name = "glisten", version = "0.3.2", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_erlang", "gleam_stdlib", "gleam_otp"], otp_app = "glisten", source = "hex", outer_checksum = "6FAB5F6A93FDE4A8A66A7F9B775B66276C0C42E2ECC1394D905E77BE5EF296E8" },
{ name = "mist", version = "0.4.5", build_tools = ["gleam"], requirements = ["glisten", "gleam_otp", "gleam_http", "gleam_stdlib", "gleam_erlang"], otp_app = "mist", source = "hex", outer_checksum = "C617C223F18EC80C5901DFE7FC5A75F94C243AF6F4BFC93DDD4800C728E5E4DD" },
{ name = "birl", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "976CFF85D34D50F7775896615A71745FBE0C325E50399787088F941B539A0497" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], source = "local", path = "../../../gleams/glisten" },
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
{ name = "logging", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "82C112ED9B6C30C1772A6FE2613B94B13F62EA35F5869A2630D13948D297BD39" },
{ name = "mist", version = "1.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "hpack_erl", "logging"], source = "local", path = "../../../gleams/mist" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "telemetry", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "DAD9CE9D8EFFC621708F99EAC538EF1CBE05D6A874DD741DE2E689C47FEAFED5" },
]

[requirements]
gleam_erlang = "~> 0.9"
gleam_http = "~> 3.0"
gleam_otp = "~> 0.4"
gleam_stdlib = "~> 0.20"
gleeunit = "~> 0.6"
glisten = "~> 0.3"
mist = "~> 0.4"
gleam_erlang = { version = "~> 0.25" }
gleam_http = { version = "~> 3.6" }
gleam_otp = { version = "~> 0.10" }
gleam_stdlib = { version = "~> 0.36" }
gleeunit = { version = "~> 1.0" }
mist = { path = "../../../gleams/mist" }
65 changes: 38 additions & 27 deletions glist/src/glist.gleam
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
import gleam/bit_builder
import gleam/erlang
import gleam/bytes_builder
import gleam/erlang/process
import gleam/http
import gleam/http/request.{Request}
import gleam/http/request.{type Request}
import gleam/http/response
import gleam/result
import mist

pub fn main() {
let empty_bytes = mist.Bytes(bytes_builder.new())
let empty_response =
response.new(200)
|> response.set_body(bit_builder.new())
|> response.set_body(empty_bytes)

let not_found =
response.new(404)
|> response.set_body(bit_builder.new())
|> response.set_body(empty_bytes)

assert Ok(_) =
mist.run_service(
8080,
fn(req: Request(BitString)) {
case req.method, request.path_segments(req) {
http.Get, [] -> empty_response
http.Get, ["user", id] ->
response.new(200)
|> response.set_body(bit_builder.from_bit_string(<<id:utf8>>))
http.Post, ["user"] -> {
let content_type =
req
|> request.get_header("content-type")
|> result.unwrap("application/octet-stream")
response.new(200)
|> response.set_body(bit_builder.from_bit_string(req.body))
|> response.prepend_header("content-type", content_type)
}
_, _ -> not_found
let too_large =
response.new(413)
|> response.set_body(empty_bytes)

let assert Ok(subj) =
fn(req: Request(BitArray)) {
case req.method, request.path_segments(req) {
http.Get, [] -> empty_response
http.Get, ["user", id] ->
response.new(200)
|> response.set_body(
mist.Bytes(bytes_builder.from_bit_array(<<id:utf8>>)),
)
http.Post, ["user"] -> {
let content_type =
req
|> request.get_header("content-type")
|> result.unwrap("application/octet-stream")
response.new(200)
|> response.set_body(
mist.Bytes(bytes_builder.from_bit_array(req.body)),
)
|> response.prepend_header("content-type", content_type)
}
},
)
erlang.sleep_forever()
_, _ -> not_found
}
}
|> mist.new
|> mist.read_request_body(40_000_000, too_large)
|> mist.port(8080)
|> mist.start_http

process.sleep_forever()
}
7 changes: 6 additions & 1 deletion glolang/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"net/http"
"strings"
)
Expand All @@ -19,5 +20,9 @@ func main() {
r.Write(w)
})

http.ListenAndServe(":8080", mux)
// err := http.ListenAndServeTLS(":3334", "/home/alex/gleams/mist/domain.crt", "/home/alex/gleams/mist/domain.key", mux)
err := http.ListenAndServe(":8080", mux)
if err != nil {
fmt.Printf("Failed to start: %w", err)
}
}
9 changes: 5 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ fi

mkdir -p "output/$1"

concurrencies=(1 2 4 6 8 12 16)
concurrencies=(1 2 4 6 8 12)
server="http://localhost:8080"

for concurrency in "${concurrencies[@]}"
do
echo "Running '/' with $concurrency concurrency..."
h2load --h1 --no-tls-proto=http/1.1 -D 30 http://localhost:8080/ -t $concurrency -c $concurrency > "output/$1/$1-$concurrency.txt"
h2load --h1 --no-tls-proto=http/1.1 -D 30 "$server/" -t $concurrency -c $concurrency > "output/$1/$1-$concurrency.txt"
done

for concurrency in "${concurrencies[@]}"
do
echo "Running '/user/:id' with $concurrency concurrency..."
random=$[$RANDOM % 2048 + 1024]
h2load --h1 --no-tls-proto=http/1.1 -D 30 "http://localhost:8080/user/$random" -t $concurrency -c $concurrency > "output/$1/$1-$concurrency-user-$random.txt"
h2load --h1 --no-tls-proto=http/1.1 -D 30 "$server/user/$random" -t $concurrency -c $concurrency > "output/$1/$1-$concurrency-user-$random.txt"
done

for concurrency in "${concurrencies[@]}"
Expand All @@ -29,6 +30,6 @@ do
echo $data > data.txt
echo "Running '/user' with $concurrency concurrency..."
random=$[$RANDOM % 2048 + 1024]
h2load --h1 --no-tls-proto=http/1.1 -D 30 -d data.txt "http://localhost:8080/user" -t $concurrency -c $concurrency > "output/$1/$1-$concurrency-post-$random.txt"
h2load --h1 --no-tls-proto=http/1.1 -D 30 -d data.txt "$server/user" -t $concurrency -c $concurrency > "output/$1/$1-$concurrency-post-$random.txt"
rm data.txt
done

0 comments on commit 67c1635

Please sign in to comment.