summaryrefslogtreecommitdiff
path: root/bench/Main.hs
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-03-31 15:49:46 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-03-31 15:49:46 +0400
commitdc19b708c56a168b4c9dcf5d1bf7c2cae0a04c71 (patch)
tree3c30782e6a9122071e622c5b6433e48a9a3a064f /bench/Main.hs
parenta6b54241f2a5c56ec9b5103f34688bd26e69f810 (diff)
add bench
Diffstat (limited to 'bench/Main.hs')
-rw-r--r--bench/Main.hs67
1 files changed, 67 insertions, 0 deletions
diff --git a/bench/Main.hs b/bench/Main.hs
new file mode 100644
index 0000000..279842f
--- /dev/null
+++ b/bench/Main.hs
@@ -0,0 +1,67 @@
1{-# LANGUAGE PackageImports #-}
2module Main (main) where
3
4import Control.DeepSeq
5import Data.Maybe
6import Data.Attoparsec.ByteString as Atto
7import Data.ByteString as B
8import Data.ByteString.Lazy as BL
9import Criterion.Main
10import System.Environment
11
12import "bencode" Data.BEncode as A
13import Data.AttoBencode as B
14import Data.AttoBencode.Parser as B
15import "bencoding" Data.BEncode as C
16
17instance NFData A.BEncode where
18 rnf (A.BInt i) = rnf i
19 rnf (A.BString s) = rnf s
20 rnf (A.BList l) = rnf l
21 rnf (A.BDict m) = rnf m
22
23instance NFData B.BValue where
24 rnf (B.BInt i) = rnf i
25 rnf (B.BString s) = rnf s
26 rnf (B.BList l) = rnf l
27 rnf (B.BDict d) = rnf d
28
29instance NFData C.BEncode where
30 rnf (C.BInteger i) = rnf i
31 rnf (C.BString s) = rnf s
32 rnf (C.BList l) = rnf l
33 rnf (C.BDict d) = rnf d
34
35getRight :: Either String a -> a
36getRight (Left e) = error e
37getRight (Right x) = x
38
39main :: IO ()
40main = do
41 (path : args) <- getArgs
42 torrentFile <- B.readFile path
43 let lazyTorrentFile = fromChunks [torrentFile]
44
45 case rnf (torrentFile, lazyTorrentFile) of
46 () -> return ()
47
48 withArgs args $
49 defaultMain
50 [ bench "decode/bencode" $ nf A.bRead lazyTorrentFile
51 , bench "decode/AttoBencode" $ nf (getRight . Atto.parseOnly bValue) torrentFile
52 , bench "decode/b-encode" $ nf (getRight . C.decode) torrentFile
53
54 , let Just v = A.bRead lazyTorrentFile in
55 bench "encode/bencode" $ nf A.bPack v
56 , let Right v = Atto.parseOnly bValue torrentFile in
57 bench "encode/AttoBencode" $ nf B.encode v
58 , let Right v = C.decode torrentFile in
59 bench "encode/b-encode" $ nf C.encode v
60
61 , bench "decode+encode/bencode" $ nf (A.bPack . fromJust . A.bRead)
62 lazyTorrentFile
63 , bench "decode+encode/AttoBencode" $ nf (B.encode . getRight . Atto.parseOnly bValue)
64 torrentFile
65 , bench "decode+encode/b-encode" $ nf (C.encode . getRight . C.decode)
66 torrentFile
67 ]