1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 07:25:44 +03:00
semantic/bench/Main.hs

75 lines
3.0 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DeriveAnyClass, DeriveGeneric, FlexibleInstances, StandaloneDeriving #-}
2016-06-06 22:32:16 +03:00
{-# OPTIONS_GHC -fno-warn-orphans #-}
2016-05-31 16:23:43 +03:00
module Main where
2016-05-31 16:27:00 +03:00
import Criterion.Main
2016-06-06 22:32:38 +03:00
import Data.Function
import Data.List (genericLength)
import Data.String
2016-06-06 22:32:16 +03:00
import Patch
2016-05-31 16:27:00 +03:00
import Prologue
2016-06-06 22:32:38 +03:00
import SES
import Test.QuickCheck hiding (Fixed)
2016-10-06 02:17:46 +03:00
import Arguments
import SemanticDiff (fetchDiffs)
import qualified Renderer as R
import qualified SemanticDiffPar
import System.Directory (makeAbsolute)
2016-05-31 16:27:00 +03:00
main :: IO ()
2016-05-31 17:53:23 +03:00
main = do
2016-06-07 00:17:37 +03:00
benchmarks <- sequenceA [ generativeBenchmarkWith "ses" 10 arbitrarySESInputs (uncurry ((*) `on` length)) (nf (uncurry benchmarkSES)) ]
2016-10-06 02:17:46 +03:00
defaultMain (syncAsyncBenchmark : benchmarks)
2016-06-07 00:17:37 +03:00
where arbitrarySESInputs = (,) <$> sized (`vectorOf` arbitrary) <*> sized (`vectorOf` arbitrary)
2016-06-07 00:16:52 +03:00
benchmarkSES :: [String] -> [String] -> [Either String (Patch String)]
2016-06-06 22:32:38 +03:00
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 . fmap genericLength)
2016-06-06 22:32:38 +03:00
2016-06-06 22:32:16 +03:00
instance NFData a => NFData (Patch a)
2016-05-31 18:47:56 +03:00
-- | 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 = generativeBenchmarkWith name n arbitrary metric benchmark
generativeBenchmarkWith :: (Show m, Ord m) => String -> Int -> Gen a -> (a -> m) -> (a -> Benchmarkable) -> IO Benchmark
generativeBenchmarkWith name n generator metric benchmark = do
benchmarks <- traverse measure (take n [0,(defaultSize `div` n)..defaultSize])
pure $! bgroup name (snd <$> (sortOn fst benchmarks))
where measure n = do
input <- generate (resize n generator)
let measurement = metric input
pure $! (measurement, bench (show measurement) (benchmark input))
defaultSize = 100
2016-10-06 02:17:46 +03:00
syncAsyncBenchmark :: Benchmark
syncAsyncBenchmark =
bgroup "async vs par" [
bench "async" . whnfIO $ SemanticDiff.fetchDiffs =<< theArgs,
bench "par" . whnfIO $ SemanticDiffPar.fetchDiffs =<< theArgs
]
theArgs :: IO Arguments
theArgs = do
jqueryPath <- makeAbsolute "test/repos/jquery"
pure $ args jqueryPath sha1 sha2 files R.Patch
where
sha1 = "70526981916945dc4093e116a3de61b1777d4718"
sha2 = "e5ffcb0838c894e26f4ff32dfec162cf624d8d7d"
files = [
"src/manipulation/getAll.js",
"src/manipulation/support.js",
"src/manipulation/wrapMap.js",
"src/offset.js",
"test/unit/css.js",
"test/unit/deferred.js",
"test/unit/deprecated.js",
"test/unit/effects.js",
"test/unit/event.js",
"test/unit/offset.js",
"test/unit/wrap.js"
]