Skip to content

Commit 25a3cf0

Browse files
committed
allow benchee configuration
1 parent c46647c commit 25a3cf0

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

CHANGELOG.md

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
# Changelog
22

3+
## 1.1.0 - 2024-02-28
4+
5+
- Allow optional configuration:
6+
7+
```gleam
8+
import glychee/configuration
9+
import glychee/benchmark
10+
11+
pub fn main() {
12+
// Configuration is optional
13+
configuration.initialize()
14+
configuration.set_pair(configuration.Warmup, 2)
15+
configuration.set_pair(configuration.Parallel, 2)
16+
17+
// Run the benchmark
18+
benchmark.run(...)
19+
}
20+
```
21+
22+
As of now, all values are positive integers, and supported keys are:
23+
24+
- MemoryTime
25+
- Parallel
26+
- ReductionTime
27+
- Time
28+
- Warmup
29+
330
## 1.0.2 - 2024-02-27
431

532
- Fix hiding of internals.

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,15 @@ To add and run a demo of **Glychee**:
5454
import gleam/int
5555
import gleam/list
5656
import glychee/benchmark
57+
import glychee/configuration
5758
5859
pub fn main() {
60+
// Configuration is optional
61+
configuration.initialize()
62+
configuration.set_pair(configuration.Warmup, 2)
63+
configuration.set_pair(configuration.Parallel, 2)
64+
65+
// Run the benchmarks
5966
benchmark.run(
6067
[
6168
benchmark.Function(label: "list.sort()", callable: fn(test_data) {

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "glychee"
2-
version = "1.0.2"
2+
version = "1.1.0"
33
gleam = "~> 1.0"
44
licences = ["Apache-2.0"]
55
description = "Glychee: Easy access to Elixir's Benchee from Gleam!"

src/glychee/configuration.gleam

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub type BencheeConfigurationKey {
2+
MemoryTime
3+
Parallel
4+
ReductionTime
5+
Time
6+
Warmup
7+
}
8+
9+
pub type BencheeConfigurationValue =
10+
Int
11+
12+
@external(erlang, "Elixir.GlycheeBenchee", "initialize_configuration")
13+
pub fn initialize() -> Nil
14+
15+
@external(erlang, "Elixir.GlycheeBenchee", "set_configuration_pair")
16+
pub fn set_pair(
17+
key configuration_key: BencheeConfigurationKey,
18+
value configuration_value: BencheeConfigurationValue,
19+
) -> Bool

src/glychee_benchee.ex

+48-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ defmodule GlycheeBenchee do
33
Allows running of Benchee from Gleam.
44
"""
55

6+
@type configuration_key_t() :: :warmup | :time | :memory_time | :reduction_time
7+
8+
@default_warmup 4
9+
@default_time 4
10+
@default_memory_time 8
11+
@default_reduction_time 4
12+
@default_parallel 1
13+
614
@doc """
715
Runs Benchee with some standard settings.
816
@@ -21,5 +29,44 @@ defmodule GlycheeBenchee do
2129
do:
2230
list_of_function_tuples
2331
|> Enum.into(%{})
24-
|> Benchee.run(warmup: 4, time: 8, memory_time: 4, reduction_time: 4)
32+
|> Benchee.run(get_configurations())
33+
34+
@doc """
35+
Initializes ETS based global configuration.
36+
"""
37+
@spec initialize_configuration() :: atom()
38+
def initialize_configuration,
39+
do: :ets.new(:glychee_configuration, [:set, :protected, :named_table])
40+
41+
@doc """
42+
Sets an ETS based global configuration pair.
43+
"""
44+
@spec set_configuration_pair(configuration_key_t(), pos_integer()) :: boolean()
45+
def set_configuration_pair(key, value) when is_atom(key),
46+
do: :ets.insert(:glychee_configuration, {key, value})
47+
48+
@spec get_configurations() :: keyword()
49+
defp get_configurations do
50+
[
51+
get_configuration(:warmup, @default_warmup),
52+
get_configuration(:time, @default_time),
53+
get_configuration(:memory_time, @default_memory_time),
54+
get_configuration(:reduction_time, @default_reduction_time),
55+
get_configuration(:parallel, @default_parallel)
56+
]
57+
end
58+
59+
@spec get_configuration(configuration_key_t(), pos_integer()) :: {atom(), pos_integer()}
60+
defp get_configuration(key, default)
61+
when is_atom(key) and is_integer(default) and default > 0 do
62+
# Checks if the `:glychee_configuration` ETS table exists.
63+
if :lists.member(:glychee_configuration, :ets.all()) do
64+
case :ets.lookup(:glychee_configuration, key) do
65+
[] -> {key, default}
66+
[{key, value} | _tail] -> {key, value}
67+
end
68+
else
69+
{key, default}
70+
end
71+
end
2572
end

0 commit comments

Comments
 (0)