Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ppx_inline_test.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ depends: [
"ocaml" {>= "5.1.0"}
"base"
"sexplib0"
"time_now"
"dune" {>= "3.11.0"}
"ppxlib" {>= "0.33.0"}
depopts:[
"time_now"
]
available: arch != "arm32" & arch != "x86_32"
synopsis: "Syntax extension for writing in-line tests in ocaml code"
Expand Down
5 changes: 4 additions & 1 deletion runtime-lib/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(library
(name ppx_inline_test_lib)
(public_name ppx_inline_test.runtime-lib)
(libraries base inline_test_config sexplib0 time_now)
(libraries base inline_test_config sexplib0
(select timestamp.ml from
(time_now -> timestamp.time_now.ml)
( -> timestamp.sys.ml)))
(preprocess no_preprocessing))
11 changes: 2 additions & 9 deletions runtime-lib/ppx_inline_test_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -416,21 +416,14 @@ let testing =
else `Not_testing
;;

(* This function returns an int63 representing the number of nanos since
some (fixed) baseline. On unix, this baseline will be the unix epoch,
and in javascript, the baseline will be "program initialization time."
Regardless, it's always safe to subtract two values and use the diff,
which is all that ppx_inline_test_lib uses it for. *)
let timestamp_ns () = Time_now.nanosecond_counter_for_timing ()

let where_to_cut_backtrace =
lazy
(Base.String.Search_pattern.create
(__MODULE__ ^ "." ^ "time_without_resetting_random_seeds"))
;;

let time_without_resetting_random_seeds f =
let before_ns = timestamp_ns () in
let before = Timestamp.get () in
let res =
(* To avoid noise in backtraces, we do two things.

Expand All @@ -445,7 +438,7 @@ let time_without_resetting_random_seeds f =
try Ok (f ()) with
| exn -> Error (exn, Printexc.get_backtrace ())
in
time_sec := Base.Int63.(timestamp_ns () - before_ns |> to_float) /. 1e9;
time_sec := Timestamp.seconds_since before;
res
;;

Expand Down
4 changes: 4 additions & 0 deletions runtime-lib/timestamp.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type t

val get : unit -> t
val seconds_since : t -> float
4 changes: 4 additions & 0 deletions runtime-lib/timestamp.sys.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type t = float

let get () = Sys.time ()
let seconds_since before = get () -. before
9 changes: 9 additions & 0 deletions runtime-lib/timestamp.time_now.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type t = Base.Int63.t

(* This function returns an int63 representing the number of nanos since
some (fixed) baseline. On unix, this baseline will be the unix epoch,
and in javascript, the baseline will be "program initialization time."
Regardless, it's always safe to subtract two values and use the diff,
which is all that ppx_inline_test_lib uses it for. *)
let get () = Time_now.nanosecond_counter_for_timing ()
let seconds_since before = Base.Int63.(get () - before |> to_float) /. 1e9