Skip to content

Commit

Permalink
Compare json to json embedded in wasm byte sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Jan 15, 2024
1 parent 0ed7a53 commit b85226a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
20 changes: 19 additions & 1 deletion lib/components_guide/wasm/examples/examples_magic_numbers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,26 @@ defmodule ComponentsGuide.Wasm.Examples.MagicNumbers do
fast_3g_download: 180_000,
fast_3g_upload: 84_375
)
end

defmodule MobileThrottlingJSON do
use Orb

Memory.pages(1)

def json do
Jason.encode!(%{
slow_3g_latency_ms: 2000,
slow_3g_download: 50_000,
slow_3g_upload: 50_000,
fast_3g_latency_ms: 563,
fast_3g_download: 180_000,
fast_3g_upload: 84_375
})
end

wasm do
defw application_json, I32.String do
json()
end
end
end
22 changes: 7 additions & 15 deletions test/components_guide/wasm/examples/magic_numbers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@ defmodule ComponentsGuide.Wasm.Examples.MagicNumbers.Test do

describe "MobileThrottling" do
alias MagicNumbers.MobileThrottling
alias MagicNumbers.MobileThrottlingJSON

test "byte size vs json" do
assert byte_size(Wasm.to_wasm(MobileThrottling)) == 168

assert byte_size(
Jason.encode!(%{
slow_3g_latency_ms: 2000,
slow_3g_download: 50_000,
slow_3g_upload: 50_000,
fast_3g_latency_ms: 563,
fast_3g_download: 180_000,
fast_3g_upload: 84_375
})
) == 149
end
test "json encoded size", do: assert(149 = byte_size(MobileThrottlingJSON.json()))
test "wasm with globals size", do: assert(168 = byte_size(Wasm.to_wasm(MobileThrottling)))

test "wasm with embedded json size",
do: assert(224 = byte_size(Wasm.to_wasm(MobileThrottlingJSON)))

@tag :skip
test "opt" do
path_wasm = Path.join(__DIR__, "mobile_throttling.wasm")
path_wat = Path.join(__DIR__, "mobile_throttling.wat")
path_opt_wasm = Path.join(__DIR__, "mobile_throttling_OPT.wasm")
path_opt_wat = Path.join(__DIR__, "mobile_throttling_OPT.wat")
wasm = Wasm.to_wasm(MobileThrottling)
wasm = Wasm.to_wasm(MobileThrottlingJSON)
File.write!(path_wasm, wasm)
System.cmd("wasm-opt", [path_wasm, "-o", path_opt_wasm, "-O"])

Expand Down

0 comments on commit b85226a

Please sign in to comment.