Skip to content

Commit

Permalink
Rewrite benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck committed May 5, 2020
1 parent 8bad7c9 commit 8681711
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 485 deletions.
434 changes: 259 additions & 175 deletions bench/README.md

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion bench/benchee_helper.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
alias BencheeDsl.Benchmark
alias TimeZoneInfo.{DataStore, IanaParser, Transformer}

Application.ensure_all_started(:tzdata)

Code.require_file("bench/data.exs")
Code.require_file("test/support/time_zone_info/data_store/server.exs")

Application.ensure_all_started(:tzdata)
path = "test/fixtures/iana/2019c"
files = ~w(africa antarctica asia australasia etcetera europe northamerica southamerica)

time_zone_info_data =
with {:ok, data} <- IanaParser.parse(path, files) do
Transformer.transform(data, "2019c", lookahead: 5)
end

DataStore.PersistentTerm.put(time_zone_info_data)
DataStore.ErlangTermStorage.put(time_zone_info_data)
DataStore.Server.put(time_zone_info_data)

BencheeDsl.config(
before_each_benchmark: fn benchmark ->
Expand Down
48 changes: 48 additions & 0 deletions bench/data.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Data do
@moduledoc false

alias TimeZoneInfo.DataStore

def inputs do
data = Code.eval_file("bench/data/333_ok_gap_ambiguous.exs") |> elem(0)

%{
world_ok: Map.get(data, :ok),
world_gap: Map.get(data, :gap),
world_ambiguous: Map.get(data, :ambiguous),
world_last_year: last_year(),
berlin_gap_2020: berlin_2020(:gap),
berlin_ambiguous_2020: berlin_2020(:ambiguous),
berlin_ok_2020: berlin_2020(:ok)
}
end

defp last_year do
Application.put_env(:time_zone_info, :data_store, DataStore.ErlangTermStorage)
seconds_per_year = 31_622_400
now = NaiveDateTime.utc_now()

Enum.map(0..333, fn _ ->
time_zone = Enum.random(TimeZoneInfo.time_zones())
datetime = NaiveDateTime.add(now, :rand.uniform(seconds_per_year) * -1)
{datetime, time_zone}
end)
end

defp berlin_2020(mode) do
datetime =
case mode do
:gap ->
~N[2020-03-29 02:00:01]

:ambiguous ->
~N[2020-10-25 02:00:01]
:ok ->
~N[2020-06-25 00:00:00]
end

Enum.map(0..333, fn index ->
{NaiveDateTime.add(datetime, index), "Europe/Berlin"}
end)
end
end
89 changes: 9 additions & 80 deletions bench/stores_bench.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule StoresBench do
use BencheeDsl.Benchmark

alias TimeZoneInfo.{DataStore, IanaParser, Transformer}
alias TimeZoneInfo.DataStore

@title "Benchmark: TimeZoneDatabase"

Expand All @@ -20,13 +20,13 @@ defmodule StoresBench do
`GenServer` version is otherwise only used in the tests.
The inputs for every benchmark run:
- **ok:** 333 `(datetime, time_zone)` arguments that are resulting in a
- **world_ok:** 333 `(datetime, time_zone)` arguments that are resulting in a
`:ok` return value.
- **gap:** 333 `(datetime, time_zone)` arguments that are resulting in a
- **world_gap:** 333 `(datetime, time_zone)` arguments that are resulting in a
`:gap` return tuple.
- **ambiguous:** 333 `(datetime, time_zone)` arguments that are resulting in
- **world_ambiguous:** 333 `(datetime, time_zone)` arguments that are resulting in
a `:ambiguous` return tuple.
- **last_year:** 333 `(datetime, time_zone)` arguments with random time zone
- **world_last_year:** 333 `(datetime, time_zone)` arguments with random time zone
and a date time from now to one year in the past. The data is calculated
once for all test candidates.
- **berlin_gap_2020**: 333 gaps in the time zone `Europe/Berlin` in 2020.
Expand All @@ -37,100 +37,29 @@ defmodule StoresBench do
and date times between 1900 and 2050.
"""

setup do
path = "test/fixtures/iana/2019c"
files = ~w(africa antarctica asia australasia etcetera europe northamerica southamerica)

time_zone_info_data =
case IanaParser.parse(path, files) do
{:ok, data} ->
Transformer.transform(data, "2019c", lookahead: 5)

{:error, _} ->
raise "Can not parse '#{path}'!"
end

DataStore.PersistentTerm.put(time_zone_info_data)
DataStore.ErlangTermStorage.put(time_zone_info_data)
DataStore.Server.put(time_zone_info_data)
end

inputs do
data = Code.eval_file("bench/data/333_ok_gap_ambiguous.exs") |> elem(0)

%{
ok: Map.get(data, :ok),
gap: Map.get(data, :gap),
ambiguous: Map.get(data, :ambiguous),
berlin_gap_2020: berlin_2020(:gap),
berlin_ambiguous_2020: berlin_2020(:ambiguous),
last_year: last_year()
}
end
inputs Data.inputs()

job time_zone_info_ets(data) do
Application.put_env(:time_zone_info, :data_store, DataStore.ErlangTermStorage)

Enum.each(data, fn {datetime, time_zone} ->
datetime
|> TimeZoneInfo.TimeZoneDatabase.time_zone_periods_from_wall_datetime(time_zone)
|> validate()
TimeZoneInfo.TimeZoneDatabase.time_zone_periods_from_wall_datetime(datetime, time_zone)
end)
end

job time_zone_info_pst(data) do
Application.put_env(:time_zone_info, :data_store, DataStore.PersistentTerm)

Enum.each(data, fn {datetime, time_zone} ->
datetime
|> TimeZoneInfo.TimeZoneDatabase.time_zone_periods_from_wall_datetime(time_zone)
|> validate()
TimeZoneInfo.TimeZoneDatabase.time_zone_periods_from_wall_datetime(datetime, time_zone)
end)
end

job time_zone_info_map(data) do
Application.put_env(:time_zone_info, :data_store, DataStore.Server)

Enum.each(data, fn {datetime, time_zone} ->
datetime
|> TimeZoneInfo.TimeZoneDatabase.time_zone_periods_from_wall_datetime(time_zone)
|> validate()
end)
end

defp validate(result) do
case elem(result, 0) do
:ok -> :ok
:gap -> :ok
:ambiguous -> :ok
_ -> raise "fail: #{inspect(result)}"
end
end

defp last_year do
Application.put_env(:time_zone_info, :data_store, DataStore.ErlangTermStorage)
seconds_per_year = 31_622_400
now = NaiveDateTime.utc_now()

Enum.map(0..333, fn _ ->
time_zone = Enum.random(TimeZoneInfo.time_zones())
datetime = NaiveDateTime.add(now, :rand.uniform(seconds_per_year) * -1)
{datetime, time_zone}
end)
end

defp berlin_2020(mode) do
datetime =
case mode do
:gap ->
~N[2020-03-29 02:00:01]

:ambiguous ->
~N[2020-10-25 02:00:01]
end

Enum.map(0..333, fn index ->
{NaiveDateTime.add(datetime, index), "Europe/Berlin"}
TimeZoneInfo.TimeZoneDatabase.time_zone_periods_from_wall_datetime(datetime, time_zone)
end)
end
end
Loading

0 comments on commit 8681711

Please sign in to comment.