2016-05-31 17:52:49 +03:00
|
|
|
|
{-# LANGUAGE DeriveAnyClass, FlexibleInstances, StandaloneDeriving #-}
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2016-05-31 16:23:43 +03:00
|
|
|
|
module Main where
|
2016-05-31 16:27:00 +03:00
|
|
|
|
|
2016-05-31 17:53:23 +03:00
|
|
|
|
import Alignment
|
2016-05-31 16:27:00 +03:00
|
|
|
|
import Criterion.Main
|
2016-05-31 17:52:49 +03:00
|
|
|
|
import Data.Bifunctor.Join
|
2016-05-31 17:37:34 +03:00
|
|
|
|
import Data.String
|
2016-05-31 17:52:49 +03:00
|
|
|
|
import Data.These
|
2016-05-31 16:27:00 +03:00
|
|
|
|
import Prologue
|
2016-05-31 17:37:34 +03:00
|
|
|
|
import Test.QuickCheck
|
2016-05-31 16:27:00 +03:00
|
|
|
|
|
|
|
|
|
main :: IO ()
|
2016-05-31 17:53:23 +03:00
|
|
|
|
main = do
|
|
|
|
|
benchmarks <- sequenceA [ generativeBenchmark "numberedRows" length (nf (numberedRows :: [Join These ()] -> [Join These (Int, ())])) ]
|
|
|
|
|
defaultMain benchmarks
|
2016-05-31 17:37:34 +03:00
|
|
|
|
|
2016-05-31 17:40:25 +03:00
|
|
|
|
generativeBenchmark :: (Arbitrary a, Show m) => String -> (a -> m) -> (a -> Benchmarkable) -> IO Benchmark
|
2016-05-31 17:38:19 +03:00
|
|
|
|
generativeBenchmark name metric benchmark = do
|
2016-05-31 17:37:34 +03:00
|
|
|
|
benchmarks <- traverse measure [0..100]
|
|
|
|
|
pure $! bgroup name benchmarks
|
|
|
|
|
where measure n = do
|
|
|
|
|
input <- generate (resize n arbitrary)
|
|
|
|
|
let measurement = metric input
|
2016-05-31 17:40:25 +03:00
|
|
|
|
pure $! bench (show measurement) (benchmark input)
|
2016-05-31 17:52:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Instances
|
|
|
|
|
|
|
|
|
|
deriving instance (NFData a, NFData b) => NFData (These a b)
|
|
|
|
|
deriving instance NFData a => NFData (Join These a)
|
2016-05-31 17:53:00 +03:00
|
|
|
|
|
|
|
|
|
instance (Arbitrary a, Arbitrary b) => Arbitrary (These a b) where
|
|
|
|
|
arbitrary = oneof [ This <$> arbitrary
|
|
|
|
|
, That <$> arbitrary
|
|
|
|
|
, These <$> arbitrary <*> arbitrary ]
|
|
|
|
|
shrink = these (fmap This . shrink) (fmap That . shrink) (\ a b -> (This <$> shrink a) ++ (That <$> shrink b) ++ (These <$> shrink a <*> shrink b))
|
|
|
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (Join These a) where
|
|
|
|
|
arbitrary = Join <$> arbitrary
|
|
|
|
|
shrink (Join a) = Join <$> shrink a
|