Bodigrim / tasty-bench

Featherlight benchmark framework, drop-in replacement for criterion and gauge.
https://hackage.haskell.org/package/tasty-bench
MIT License
80 stars 11 forks source link

Unhandled resource exception on pure IO action #52

Closed bolt12 closed 11 months ago

bolt12 commented 11 months ago

I have the following code:

myBenchEncode :: ( forall (st :: ps) (st' :: ps). NFData (Message ps st st')
                 , forall (st :: ps) pr. NFData (PeerHasAgency pr st)
                 )
              => String
              -> (NodeToNodeVersion -> Codec ps DeserialiseFailure IO ByteString)
              -> NodeToNodeVersion
              -> AnyMessageAndAgency ps
              -> Benchmark
myBenchEncode title getCodec ntnVersion (AnyMessageAndAgency a m) =
  let Codec { encode } = getCodec ntnVersion
   in env (evaluate (force (a, m))) $ \(agency, message) ->
        bench title $ nf (encode agency) message

ignoring the details specific to my code, this is a function that generates benchmarks. I want to force evaluate some of the inputs which are pure values, an as you can see, the name of the benchmarks is not dependent on the IO action. Also the benchmark hierarchy is not dependent on it as well. What's weirder is that if I only evaluate on element of the tuple (i.e. either a or m) the code works, but as soon as I tuple them it doesn't work giving bench: Unhandled resource. Probably a bug in the runner you're using. is this a bug?

Bodigrim commented 11 months ago

You don't need env (evaluate (force (a, m)), a simple env (pure (a, m)) would suffice, because env forces the resource internally: https://github.com/Bodigrim/tasty-bench/blob/53d59f97b272152e6bba3fbd466a68377cba8956/src/Test/Tasty/Bench.hs#L1621-L1624

(It seems the documentation of env is misleading, it can use a fix)

tasty-bench defers resource management to tasty. Is it possible to reproduce the issue with tasty itself?

bolt12 commented 11 months ago

Thank you for your prompt reply! I changed the code to use just pure but the same issue, remains.

tasty-bench defers resource management to tasty. Is it possible to reproduce the issue with tasty itself?

What do you mean, tasty does not have any env function, only withResource which has a very different type signature. How can I try to reproduce the issue with tasty, given that I believe the issue is in env ?

Bodigrim commented 11 months ago

env is a thin wrapper over withResource. If you inline env and underlying envWithCleanup, you'll obtain a reproducer via withResource. You can further simplify your example replacing bench with a simpler test ingredient, say testCase.

bolt12 commented 11 months ago

Thank you! Yes I tried that and still get the same problem. I inlined everything up to withResource and used something other than bench, but no success

Bodigrim commented 11 months ago

So if it's not tasty-bench specific, could you forward the issue to tasty issue tracker?

bolt12 commented 11 months ago

Please see this comment: https://github.com/UnkindPartition/tasty/issues/401#issuecomment-1818764402