1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 01:47:01 +03:00
semantic/bench/Main.hs
2016-06-06 15:32:38 -04:00

37 lines
1.5 KiB
Haskell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{-# LANGUAGE DeriveAnyClass, DeriveGeneric, FlexibleInstances, StandaloneDeriving #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main where
import Criterion.Main
import Data.Function
import Data.String
import Patch
import Prologue
import SES
import Test.QuickCheck hiding (Fixed)
main :: IO ()
main = do
benchmarks <- sequenceA [ generativeBenchmark "ses" 10 (uncurry ((*) `on` length)) (nf (uncurry benchmarkSES)) ]
defaultMain benchmarks
benchmarkSES :: [Int] -> [Int] -> [Either Int (Patch Int)]
benchmarkSES as bs = ses compare cost as bs
where compare a b = if a == b then Just (Left a) else Nothing
cost = either (const 0) (sum . (1 <$))
instance NFData a => NFData (Patch a)
-- | Defines a named group of n benchmarks over inputs generated by an `Arbitrary` instance.
-- |
-- | The inputs sizes at each iteration are measured by a metric function, which gives the name of the benchmark. This makes it convenient to correlate a benchmark of some function over lists with e.g. input `length`.
generativeBenchmark :: (Arbitrary a, Show m, Ord m) => String -> Int -> (a -> m) -> (a -> Benchmarkable) -> IO Benchmark
generativeBenchmark name n metric benchmark = do
benchmarks <- traverse measure (replicate n defaultSize)
pure $! bgroup name (snd <$> (sortOn fst benchmarks))
where measure n = do
input <- generate (resize n arbitrary)
let measurement = metric input
pure $! (measurement, bench (show measurement) (benchmark input))
defaultSize = 100