Skip to content

Commit

Permalink
update for 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Feb 27, 2024
1 parent 2e4a946 commit 7b54729
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 66 deletions.
70 changes: 20 additions & 50 deletions src/glychee/benchmark.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//// Contains custom types `Function` and `Data` and a runner function to run
//// a list of these benchmark functions against a list of benchmark data.
////

import glychee/helpers as h

/// Function pairs a `label` with a function returning a callable.
///
Expand Down Expand Up @@ -34,58 +37,25 @@ pub fn run(
function_list: List(Function(test_data, any)),
data_list: List(Data(test_data)),
) -> Nil {
gleam_stdlib_each(
data_list,
fn(data) {
erlang_io_put_chars("\n\n")
erlang_io_put_chars(separator_line <> "\n")
erlang_io_put_chars("==== data set: " <> data.label)
erlang_io_put_chars("\n")
erlang_io_put_chars(separator_line <> "\n")
erlang_io_put_chars("\n")

function_list
|> erlang_lists_map(
fn(function: Function(test_data, any)) {
#(function.label, function.callable(data.data))
},
_,
)
|> benchee_wrapper_run
},
)
}

/// Copy of stdlib's implementation.
/// Copied here, so that there are no deps on Glychee.
///
fn gleam_stdlib_each(list: List(a), f: fn(a) -> b) -> Nil {
case list {
[] -> Nil
[x, ..xs] -> {
f(x)
gleam_stdlib_each(xs, f)
}
}
h.gleam_list_each(data_list, fn(data) {
h.gleam_io_println("\n\n")
h.gleam_io_println(separator_line <> "\n")
h.gleam_io_println("==== data set: " <> data.label)
h.gleam_io_println("\n")
h.gleam_io_println(separator_line <> "\n")
h.gleam_io_println("\n")

function_list
|> h.gleam_list_map(fn(function: Function(test_data, any)) {
#(function.label, function.callable(data.data))
})
|> benchee_wrapper_run
})
}

/// Replaces stdlib's io.println
/// so that there are no deps on Glychee.
///
@external(erlang, "io", "put_chars")
fn erlang_io_put_chars(text text: String) -> Nil

/// Replaces stdlib's list.map
/// so that there are no deps on Glychee.
///
@external(erlang, "lists", "map")
fn erlang_lists_map(callable callable: fn(a) -> b, list list: List(a)) -> List(
b,
)

/// Wrapper for Elixir's Benchee
///
@external(erlang, "Elixir.GlycheeBenchee", "run")
fn benchee_wrapper_run(list_of_function_tuples list_of_function_tuples: List(
#(String, any),
)) -> any
fn benchee_wrapper_run(
list_of_function_tuples list_of_function_tuples: List(#(String, any)),
) -> any
35 changes: 35 additions & 0 deletions src/glychee/helpers.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// Contains helper functions copied from Gleam's stlib,
//// so that there are no deps on Glychee.
////

/// Replaces stdlib's `list.each`.
///
pub fn gleam_list_each(list: List(a), f: fn(a) -> b) -> Nil {
case list {
[] -> Nil
[x, ..xs] -> {
f(x)
gleam_list_each(xs, f)
}
}
}

/// Replaces stdlib's `io.println`
///
@external(erlang, "io", "put_chars")
pub fn gleam_io_println(text text: String) -> Nil

/// Replaces stdlib's `list.map`
///
pub fn gleam_list_map(
list list: List(a),
callable callable: fn(a) -> b,
) -> List(b) {
do_erlang_lists_map(callable, list)
}

@external(erlang, "lists", "map")
fn do_erlang_lists_map(
callable callable: fn(a) -> b,
list list: List(a),
) -> List(b)
31 changes: 15 additions & 16 deletions src/glychee_benchee.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
defmodule GlycheeBenchee do
@moduledoc """
Allows running of Benchee from Gleam
Allows running of Benchee from Gleam.
"""

@doc """
Runs Benchee with some standard settings.
Takes a list of function tuples, for example:
Takes a list of function tuples.
list_of_function_tuples [
{"flat_map", fn -> Enum.flat_map(test_data, fn i -> [i, i * i]) end}
]
"""
def run(list_of_function_tuples) do
map_of_function_tuples = list_of_function_tuples |> Enum.into(%{})
## Example
Benchee.run(
map_of_function_tuples,
warmup: 4,
time: 8,
memory_time: 4,
reduction_time: 4
)
end
```elixir
list_of_function_tuples = [
{"flat_map", fn -> Enum.flat_map(test_data, fn i -> [i, i * i]) end}
]
```
"""
@spec run(list({String.t(), function()})) :: Benchee.Suite.t()
def run(list_of_function_tuples),
do:
list_of_function_tuples
|> Enum.into(%{})
|> Benchee.run(warmup: 4, time: 8, memory_time: 4, reduction_time: 4)
end

0 comments on commit 7b54729

Please sign in to comment.