Skip to content

Commit c557a2f

Browse files
committed
Init
1 parent db33501 commit c557a2f

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
dist
2+
dist-*
3+
cabal-dev
4+
*.o
5+
*.hi
6+
*.chi
7+
*.chs.h
8+
*.dyn_o
9+
*.dyn_hi
10+
.hpc
11+
.hsenv
12+
.cabal-sandbox/
13+
cabal.sandbox.config
14+
*.prof
15+
*.aux
16+
*.hp
17+
*.eventlog
18+
.stack-work/
19+
cabal.project.local
20+
cabal.project.local~
21+
.HTF/
22+
.ghc.environment.*

ChangeLog.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for bench-graph
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.

LICENSE

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2018, Alexandre Moine
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Alexandre Moine nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

bench-graph.cabal

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Initial bench-graph.cabal generated by cabal init. For further
2+
-- documentation, see http://haskell.org/cabal/users-guide/
3+
4+
name: bench-graph
5+
version: 0.1.0.0
6+
synopsis: Bench Graph
7+
-- description:
8+
license: BSD3
9+
license-file: LICENSE
10+
author: Alexandre Moine
11+
maintainer: [email protected]
12+
-- copyright:
13+
-- category:
14+
build-type: Simple
15+
extra-source-files: ChangeLog.md, README.md
16+
cabal-version: >=1.10
17+
18+
library
19+
exposed-modules: BenchGraph
20+
-- other-modules:
21+
other-extensions: ExistentialQuantification
22+
build-depends: base >=4.10 && <4.11, criterion >=1.4 && <1.5, deepseq >=1.4 && <1.5
23+
hs-source-dirs: src
24+
default-language: Haskell2010
25+
26+
benchmark benchmark-alga
27+
hs-source-dirs: bench
28+
type: exitcode-stdio-1.0
29+
main-is: Bench.hs
30+
build-depends: algebraic-graphs,
31+
base >= 4.7 && < 5,
32+
base-compat >= 0.9.1 && < 0.10,
33+
containers >= 0.5.5.1 && < 0.8,
34+
criterion >= 1.1,
35+
bench-graph
36+
default-language: Haskell2010
37+
GHC-options: -O2
38+
-Wall
39+
-fno-warn-name-shadowing
40+
if impl(ghc >= 8.0)
41+
GHC-options: -Wcompat
42+
-Wincomplete-record-updates
43+
-Wincomplete-uni-patterns
44+
-Wredundant-constraints
45+
default-extensions: FlexibleContexts
46+
TypeFamilies
47+
ScopedTypeVariables

bench/Bench.hs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
3+
import Criterion.Main
4+
import BenchGraph
5+
import Algebra.Graph
6+
7+
-- For example with alga
8+
instance GraphImpl (Graph Int) where
9+
mkGraph = edges
10+
11+
-- A simple consummer
12+
isEmpty' :: ToFuncToBench (Graph Int)
13+
isEmpty' = const $ Consummer "IsEmpty" isEmpty
14+
15+
--A simple function
16+
pathHasEdge :: ToFuncToBench (Graph Int)
17+
pathHasEdge = FuncWithArg "hasEdge" (uncurry hasEdge) show . take 5 .edgesNotInPath
18+
19+
tenPowers :: [Int]
20+
tenPowers = 1: map (10*) tenPowers
21+
22+
main :: IO ()
23+
main = defaultMain $ benchFunc isEmpty' $ map mkPath $ take 5 tenPowers

src/BenchGraph.hs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{-# LANGUAGE ExistentialQuantification #-}
2+
3+
module BenchGraph (
4+
Edges,
5+
ToFuncToBench,
6+
FuncToBench (..),
7+
GraphImpl,
8+
mkGraph,
9+
benchFunc,
10+
mkPath,
11+
edgesNotInPath
12+
) where
13+
14+
import Criterion.Main
15+
import Control.DeepSeq (NFData(..))
16+
17+
-- Generic graph
18+
type Edges = [(Int,Int)]
19+
20+
-- We want to pass the generic graph to create an according function to test
21+
type ToFuncToBench a = Edges -> FuncToBench a
22+
23+
-- Type used to group different types of functions
24+
data FuncToBench a = forall b. NFData b => Consummer String (a -> b)
25+
| forall b c. NFData c => FuncWithArg String (b -> a -> c) (b -> String) [b]
26+
27+
-- An interface between our generic graphs and others
28+
class GraphImpl g where
29+
mkGraph :: Edges -> g
30+
31+
-- Main function
32+
-- Will be cooler if its return a single benchmark with bgroup
33+
benchFunc :: GraphImpl g => ToFuncToBench g -> [Edges] -> [Benchmark]
34+
benchFunc tofunc = map (\e -> benchFunc' (tofunc e) e)
35+
36+
-- Here we bench a single function over a single graph
37+
benchFunc' :: GraphImpl g => FuncToBench g -> Edges -> Benchmark
38+
benchFunc' (Consummer name fun) edges = bench name $ nf fun $! mkGraph edges
39+
benchFunc' (FuncWithArg name fun showArg args ) edges = bgroup name $ map (\arg -> bench (showArg arg) $ nf (fun arg) $! mkGraph edges) args
40+
41+
---------
42+
43+
-- Generic graphs
44+
mkPath :: Int -> Edges
45+
mkPath n = take n $ iterate ((\(x,y) -> (x+1,y+1)) :: (Int,Int) -> (Int,Int)) (0,1)
46+
47+
edgesNotInPath :: Edges -> Edges
48+
edgesNotInPath = map (\(x,y)-> (x-1,y+1))

0 commit comments

Comments
 (0)