Skip to content

Commit 7b54729

Browse files
committed
update for 1.0
1 parent 2e4a946 commit 7b54729

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

src/glychee/benchmark.gleam

+20-50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//// Contains custom types `Function` and `Data` and a runner function to run
22
//// a list of these benchmark functions against a list of benchmark data.
3+
////
4+
5+
import glychee/helpers as h
36

47
/// Function pairs a `label` with a function returning a callable.
58
///
@@ -34,58 +37,25 @@ pub fn run(
3437
function_list: List(Function(test_data, any)),
3538
data_list: List(Data(test_data)),
3639
) -> Nil {
37-
gleam_stdlib_each(
38-
data_list,
39-
fn(data) {
40-
erlang_io_put_chars("\n\n")
41-
erlang_io_put_chars(separator_line <> "\n")
42-
erlang_io_put_chars("==== data set: " <> data.label)
43-
erlang_io_put_chars("\n")
44-
erlang_io_put_chars(separator_line <> "\n")
45-
erlang_io_put_chars("\n")
46-
47-
function_list
48-
|> erlang_lists_map(
49-
fn(function: Function(test_data, any)) {
50-
#(function.label, function.callable(data.data))
51-
},
52-
_,
53-
)
54-
|> benchee_wrapper_run
55-
},
56-
)
57-
}
58-
59-
/// Copy of stdlib's implementation.
60-
/// Copied here, so that there are no deps on Glychee.
61-
///
62-
fn gleam_stdlib_each(list: List(a), f: fn(a) -> b) -> Nil {
63-
case list {
64-
[] -> Nil
65-
[x, ..xs] -> {
66-
f(x)
67-
gleam_stdlib_each(xs, f)
68-
}
69-
}
40+
h.gleam_list_each(data_list, fn(data) {
41+
h.gleam_io_println("\n\n")
42+
h.gleam_io_println(separator_line <> "\n")
43+
h.gleam_io_println("==== data set: " <> data.label)
44+
h.gleam_io_println("\n")
45+
h.gleam_io_println(separator_line <> "\n")
46+
h.gleam_io_println("\n")
47+
48+
function_list
49+
|> h.gleam_list_map(fn(function: Function(test_data, any)) {
50+
#(function.label, function.callable(data.data))
51+
})
52+
|> benchee_wrapper_run
53+
})
7054
}
7155

72-
/// Replaces stdlib's io.println
73-
/// so that there are no deps on Glychee.
74-
///
75-
@external(erlang, "io", "put_chars")
76-
fn erlang_io_put_chars(text text: String) -> Nil
77-
78-
/// Replaces stdlib's list.map
79-
/// so that there are no deps on Glychee.
80-
///
81-
@external(erlang, "lists", "map")
82-
fn erlang_lists_map(callable callable: fn(a) -> b, list list: List(a)) -> List(
83-
b,
84-
)
85-
8656
/// Wrapper for Elixir's Benchee
8757
///
8858
@external(erlang, "Elixir.GlycheeBenchee", "run")
89-
fn benchee_wrapper_run(list_of_function_tuples list_of_function_tuples: List(
90-
#(String, any),
91-
)) -> any
59+
fn benchee_wrapper_run(
60+
list_of_function_tuples list_of_function_tuples: List(#(String, any)),
61+
) -> any

src/glychee/helpers.gleam

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// Contains helper functions copied from Gleam's stlib,
2+
//// so that there are no deps on Glychee.
3+
////
4+
5+
/// Replaces stdlib's `list.each`.
6+
///
7+
pub fn gleam_list_each(list: List(a), f: fn(a) -> b) -> Nil {
8+
case list {
9+
[] -> Nil
10+
[x, ..xs] -> {
11+
f(x)
12+
gleam_list_each(xs, f)
13+
}
14+
}
15+
}
16+
17+
/// Replaces stdlib's `io.println`
18+
///
19+
@external(erlang, "io", "put_chars")
20+
pub fn gleam_io_println(text text: String) -> Nil
21+
22+
/// Replaces stdlib's `list.map`
23+
///
24+
pub fn gleam_list_map(
25+
list list: List(a),
26+
callable callable: fn(a) -> b,
27+
) -> List(b) {
28+
do_erlang_lists_map(callable, list)
29+
}
30+
31+
@external(erlang, "lists", "map")
32+
fn do_erlang_lists_map(
33+
callable callable: fn(a) -> b,
34+
list list: List(a),
35+
) -> List(b)

src/glychee_benchee.ex

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
defmodule GlycheeBenchee do
22
@moduledoc """
3-
Allows running of Benchee from Gleam
3+
Allows running of Benchee from Gleam.
44
"""
55

66
@doc """
77
Runs Benchee with some standard settings.
88
9-
Takes a list of function tuples, for example:
9+
Takes a list of function tuples.
1010
11-
list_of_function_tuples [
12-
{"flat_map", fn -> Enum.flat_map(test_data, fn i -> [i, i * i]) end}
13-
]
14-
"""
15-
def run(list_of_function_tuples) do
16-
map_of_function_tuples = list_of_function_tuples |> Enum.into(%{})
11+
## Example
1712
18-
Benchee.run(
19-
map_of_function_tuples,
20-
warmup: 4,
21-
time: 8,
22-
memory_time: 4,
23-
reduction_time: 4
24-
)
25-
end
13+
```elixir
14+
list_of_function_tuples = [
15+
{"flat_map", fn -> Enum.flat_map(test_data, fn i -> [i, i * i]) end}
16+
]
17+
```
18+
"""
19+
@spec run(list({String.t(), function()})) :: Benchee.Suite.t()
20+
def run(list_of_function_tuples),
21+
do:
22+
list_of_function_tuples
23+
|> Enum.into(%{})
24+
|> Benchee.run(warmup: 4, time: 8, memory_time: 4, reduction_time: 4)
2625
end

0 commit comments

Comments
 (0)