Skip to content

Commit

Permalink
Add benchmarks (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Oct 19, 2016
1 parent 2583890 commit 1281554
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Identicon 0.2.0

* Added benchmarks.

## Identicon 0.1.0

* Initial release.
115 changes: 115 additions & 0 deletions bench/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
--
-- Benchmarks for the ‘identicon’ package.
--
-- Copyright © 2016 Mark Karpov <markkarpov@openmailbox.org>
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are
-- met:
--
-- * Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
--
-- * Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
--
-- * Neither the name Mark Karpov nor the names of contributors may be used
-- to endorse or promote products derived from this software without
-- specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS “AS IS” AND ANY
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Main (main) where

import Codec.Picture
import Criterion.Main
import Data.ByteString (ByteString)
import Data.Proxy
import Data.Word (Word8)
import Graphics.Identicon
import Graphics.Identicon.Primitive
import System.Random
import System.Random.TF.Init
import qualified Data.ByteString as B

main :: IO ()
main = defaultMain
[ bgen 0 0 gen0
, bgen 4 1 gen1
, bgen 8 2 gen2
, bgen 12 3 gen3 ]

-- | Run an identicon benchmark given its name and rendering function.

bgen
:: Int -- ^ Number of bytes the generator expects
-> Int -- ^ Number of layers the generator has
-> (Int -> Int -> ByteString -> Maybe (Image PixelRGB8)) -- ^ Generator
-> Benchmark -- ^ Benchmark
bgen bytes layers gen = bgroup groupName (f <$> testSizes)
where
groupName = show bytes ++ " bytes/" ++ show layers ++ " layers"
f n =
let n' = show n
in env (getBS bytes) (bench (n' ++ " × " ++ n') . nf (gen n n))

-- | Obtain a quite random 'ByteString' of specified length.

getBS :: Int -> IO ByteString
getBS n = do
gen <- initTFGen
(return . B.pack . take n . randoms) gen

-- | We render a series of rectangular icons in 'bgen', this list contains
-- size of a side of icon in pixels.

testSizes :: [Int]
testSizes = [16,32,64,128,256,512,1024]

----------------------------------------------------------------------------
-- Identicon generators

type Gen0 = Identicon 0

gen0 :: Int -> Int -> ByteString -> Maybe (Image PixelRGB8)
gen0 = renderIdenticon (Proxy :: Proxy Gen0) Identicon

type Gen1 = Identicon 4 :+ Consumer 4

gen1 :: Int -> Int -> ByteString -> Maybe (Image PixelRGB8)
gen1 = renderIdenticon (Proxy :: Proxy Gen1) i
where
i = Identicon :+ stdLayer

type Gen2 = Identicon 8 :+ Consumer 4 :+ Consumer 4

gen2 :: Int -> Int -> ByteString -> Maybe (Image PixelRGB8)
gen2 = renderIdenticon (Proxy :: Proxy Gen2) i
where
i = Identicon :+ stdLayer :+ stdLayer

type Gen3 = Identicon 12 :+ Consumer 4 :+ Consumer 4 :+ Consumer 4

gen3 :: Int -> Int -> ByteString -> Maybe (Image PixelRGB8)
gen3 = renderIdenticon (Proxy :: Proxy Gen3) i
where
i = Identicon :+ stdLayer :+ stdLayer :+ stdLayer

stdLayer :: Pixel8 -> Pixel8 -> Pixel8 -> Word8 -> Layer
stdLayer r g b n = rsym $ onGrid 4 4 n $
circle $ gradientLR (edge . mid) black (PixelRGB8 r g b)
17 changes: 17 additions & 0 deletions identicon.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,20 @@ test-suite tests
else
ghc-options: -O2 -Wall
default-language: Haskell2010

benchmark bench
main-is: Main.hs
hs-source-dirs: bench
type: exitcode-stdio-1.0
build-depends: base >= 4.7 && < 5.0
, JuicyPixels >= 3.2.6.5 && < 4.0
, bytestring >= 0.10.6 && < 0.13
, criterion >= 0.6.2.1 && < 1.2
, identicon >= 0.1.0
, random >= 1.1 && < 1.2
, tf-random >= 0.4 && < 0.6
if flag(dev)
ghc-options: -02 -Wall -Werror
else
ghc-options: -O2 -Wall
default-language: Haskell2010

0 comments on commit 1281554

Please sign in to comment.