Skip to content

Commit

Permalink
allow benchee configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Feb 28, 2024
1 parent c46647c commit 25a3cf0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## 1.1.0 - 2024-02-28

- Allow optional configuration:

```gleam
import glychee/configuration
import glychee/benchmark
pub fn main() {
// Configuration is optional
configuration.initialize()
configuration.set_pair(configuration.Warmup, 2)
configuration.set_pair(configuration.Parallel, 2)
// Run the benchmark
benchmark.run(...)
}
```

As of now, all values are positive integers, and supported keys are:

- MemoryTime
- Parallel
- ReductionTime
- Time
- Warmup

## 1.0.2 - 2024-02-27

- Fix hiding of internals.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ To add and run a demo of **Glychee**:
import gleam/int
import gleam/list
import glychee/benchmark
import glychee/configuration
pub fn main() {
// Configuration is optional
configuration.initialize()
configuration.set_pair(configuration.Warmup, 2)
configuration.set_pair(configuration.Parallel, 2)
// Run the benchmarks
benchmark.run(
[
benchmark.Function(label: "list.sort()", callable: fn(test_data) {
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "glychee"
version = "1.0.2"
version = "1.1.0"
gleam = "~> 1.0"
licences = ["Apache-2.0"]
description = "Glychee: Easy access to Elixir's Benchee from Gleam!"
Expand Down
19 changes: 19 additions & 0 deletions src/glychee/configuration.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub type BencheeConfigurationKey {
MemoryTime
Parallel
ReductionTime
Time
Warmup
}

pub type BencheeConfigurationValue =
Int

@external(erlang, "Elixir.GlycheeBenchee", "initialize_configuration")
pub fn initialize() -> Nil

@external(erlang, "Elixir.GlycheeBenchee", "set_configuration_pair")
pub fn set_pair(
key configuration_key: BencheeConfigurationKey,
value configuration_value: BencheeConfigurationValue,
) -> Bool
49 changes: 48 additions & 1 deletion src/glychee_benchee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ defmodule GlycheeBenchee do
Allows running of Benchee from Gleam.
"""

@type configuration_key_t() :: :warmup | :time | :memory_time | :reduction_time

@default_warmup 4
@default_time 4
@default_memory_time 8
@default_reduction_time 4
@default_parallel 1

@doc """
Runs Benchee with some standard settings.
Expand All @@ -21,5 +29,44 @@ defmodule GlycheeBenchee do
do:
list_of_function_tuples
|> Enum.into(%{})
|> Benchee.run(warmup: 4, time: 8, memory_time: 4, reduction_time: 4)
|> Benchee.run(get_configurations())

@doc """
Initializes ETS based global configuration.
"""
@spec initialize_configuration() :: atom()
def initialize_configuration,
do: :ets.new(:glychee_configuration, [:set, :protected, :named_table])

@doc """
Sets an ETS based global configuration pair.
"""
@spec set_configuration_pair(configuration_key_t(), pos_integer()) :: boolean()
def set_configuration_pair(key, value) when is_atom(key),
do: :ets.insert(:glychee_configuration, {key, value})

@spec get_configurations() :: keyword()
defp get_configurations do
[
get_configuration(:warmup, @default_warmup),
get_configuration(:time, @default_time),
get_configuration(:memory_time, @default_memory_time),
get_configuration(:reduction_time, @default_reduction_time),
get_configuration(:parallel, @default_parallel)
]
end

@spec get_configuration(configuration_key_t(), pos_integer()) :: {atom(), pos_integer()}
defp get_configuration(key, default)
when is_atom(key) and is_integer(default) and default > 0 do
# Checks if the `:glychee_configuration` ETS table exists.
if :lists.member(:glychee_configuration, :ets.all()) do
case :ets.lookup(:glychee_configuration, key) do
[] -> {key, default}
[{key, value} | _tail] -> {key, value}
end
else
{key, default}
end
end
end

0 comments on commit 25a3cf0

Please sign in to comment.