Skip to content

Commit

Permalink
nock: add jet-instrumentation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
juped authored and mariari committed Dec 18, 2023
1 parent 98ca829 commit 484bb00
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/nock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,40 @@ defmodule Nock do
end
end

# skip a jet once (must be called with a 9 formula for this to work)
def nock(subject, formula, :unjetted_once) do
naive_nock(subject, formula, :jetted)
end

# nock with jet-instrumentation mode.
def nock(subject, formula, instrumentation)
when instrumentation == :instrument or instrumentation == :instrument_once do
test_jettedness =
case instrumentation do
:instrument -> :unjetted
:instrument_once -> :unjetted_once
end

{jetted_usecs, jetted_result} = :timer.tc(fn -> nock(subject, formula, :jetted) end)

{unjetted_usecs, unjetted_result} =
:timer.tc(fn -> nock(subject, formula, test_jettedness) end)

validity = jetted_result === unjetted_result

%{
jetted_usecs: jetted_usecs,
unjetted_usecs: unjetted_usecs,
result: unjetted_result,
valid:
if validity do
validity
else
{validity, jetted_result}
end
}
end

# generic case: use naive nock to reduce once.
def nock(subject, formula, jettedness) do
naive_nock(subject, formula, jettedness)
Expand Down

0 comments on commit 484bb00

Please sign in to comment.