diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-03-31 15:49:46 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-03-31 15:49:46 +0400 |
commit | dc19b708c56a168b4c9dcf5d1bf7c2cae0a04c71 (patch) | |
tree | 3c30782e6a9122071e622c5b6433e48a9a3a064f /bench/Main.hs | |
parent | a6b54241f2a5c56ec9b5103f34688bd26e69f810 (diff) |
add bench
Diffstat (limited to 'bench/Main.hs')
-rw-r--r-- | bench/Main.hs | 67 |
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 #-} | ||
2 | module Main (main) where | ||
3 | |||
4 | import Control.DeepSeq | ||
5 | import Data.Maybe | ||
6 | import Data.Attoparsec.ByteString as Atto | ||
7 | import Data.ByteString as B | ||
8 | import Data.ByteString.Lazy as BL | ||
9 | import Criterion.Main | ||
10 | import System.Environment | ||
11 | |||
12 | import "bencode" Data.BEncode as A | ||
13 | import Data.AttoBencode as B | ||
14 | import Data.AttoBencode.Parser as B | ||
15 | import "bencoding" Data.BEncode as C | ||
16 | |||
17 | instance 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 | |||
23 | instance 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 | |||
29 | instance 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 | |||
35 | getRight :: Either String a -> a | ||
36 | getRight (Left e) = error e | ||
37 | getRight (Right x) = x | ||
38 | |||
39 | main :: IO () | ||
40 | main = 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 | ] | ||