git.iko.soy/typeable/attoparsec
on master help sign in
Refactor header benchmark
commit32390f90c77538885d6e115e644e90ec122284b3 authorBryan O'Sullivan <bos@serpentine.com> dateThu 18 Sep 2014, 11:00 (12 years ago) parent05f019b
4 files changed+52−41
attoparsec.cabal+1 benchmarks/HeadersByteString.hs+1−41 benchmarks/HeadersByteString/Atto.hs+49 benchmarks/attoparsec-benchmarks.cabal+1
attoparsec.cabal+1
@@ -114,6 +114,7 @@ benchmark benchmarks
114114 other-modules:
115115 Common
116116 HeadersByteString
117+ HeadersByteString.Atto
117118 HeadersText
118119 Links
119120 Numbers
benchmarks/HeadersByteString.hs+1−41
@@ -1,54 +1,14 @@
1−{-# LANGUAGE BangPatterns, OverloadedStrings #-}
2−{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-orphans #-}
31module HeadersByteString (headers) where
42
53import Common (pathTo, rechunkBS)
6−import Control.Applicative
7−import Control.DeepSeq (NFData(..))
84import Criterion.Main (bench, bgroup, nf, nfIO)
95import Criterion.Types (Benchmark)
6+import HeadersByteString.Atto (request, response)
107import Network.Wai.Handler.Warp.RequestHeader (parseHeaderLines)
11−import Network.HTTP.Types.Version (HttpVersion, http11)
128import qualified Data.Attoparsec.ByteString.Char8 as B
139import qualified Data.Attoparsec.ByteString.Lazy as BL
1410import qualified Data.ByteString.Char8 as B
1511
16−instance NFData HttpVersion where
17− rnf !_ = ()
18−
19−header = do
20− name <- B.takeWhile1 (B.inClass "a-zA-Z0-9_-") <* B.char ':' <* B.skipSpace
21− body <- bodyLine
22− return (name, body)
23−
24−bodyLine = B.takeTill (\c -> c == '\r' || c == '\n') <* B.endOfLine
25−
26−requestLine = do
27− m <- (B.takeTill B.isSpace <* B.char ' ')
28− (p,q) <- B.break (=='?') <$> (B.takeTill B.isSpace <* B.char ' ')
29− v <- httpVersion
30− return (m,p,q,v)
31−
32−httpVersion = http11 <$ "HTTP/1.1"
33−
34−responseLine = (,,) <$>
35− (httpVersion <* B.skipSpace) <*>
36− (int <* B.skipSpace) <*>
37− bodyLine
38−
39−int :: B.Parser Int
40−int = B.decimal
41−
42−request = (,) <$> (requestLine <* B.endOfLine) <*> manyheader
43−
44−response = (,) <$> responseLine <*> many header
45−
46−manyheader = do
47− c <- B.peekChar'
48− if c == '\r' || c == '\n'
49− then return []
50− else (:) <$> header <*> manyheader
51−
5212headers :: IO Benchmark
5313headers = do
5414 req <- B.readFile =<< pathTo "http-request.txt"
benchmarks/HeadersByteString/Atto.hsnew file+49
@@ -0,0 +1,49 @@
1+{-# LANGUAGE BangPatterns, OverloadedStrings #-}
2+{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-orphans #-}
3+module HeadersByteString.Atto
4+ (
5+ request
6+ , response
7+ ) where
8+
9+import Control.Applicative
10+import Control.DeepSeq (NFData(..))
11+import Network.HTTP.Types.Version (HttpVersion, http11)
12+import qualified Data.Attoparsec.ByteString.Char8 as B
13+import qualified Data.ByteString.Char8 as B
14+
15+instance NFData HttpVersion where
16+ rnf !_ = ()
17+
18+header = do
19+ name <- B.takeWhile1 (B.inClass "a-zA-Z0-9_-") <* B.char ':' <* B.skipSpace
20+ body <- bodyLine
21+ return (name, body)
22+
23+bodyLine = B.takeTill (\c -> c == '\r' || c == '\n') <* B.endOfLine
24+
25+requestLine = do
26+ m <- (B.takeTill B.isSpace <* B.char ' ')
27+ (p,q) <- B.break (=='?') <$> (B.takeTill B.isSpace <* B.char ' ')
28+ v <- httpVersion
29+ return (m,p,q,v)
30+
31+httpVersion = http11 <$ "HTTP/1.1"
32+
33+responseLine = (,,) <$>
34+ (httpVersion <* B.skipSpace) <*>
35+ (int <* B.skipSpace) <*>
36+ bodyLine
37+
38+int :: B.Parser Int
39+int = B.decimal
40+
41+request = (,) <$> (requestLine <* B.endOfLine) <*> manyheader
42+
43+response = (,) <$> responseLine <*> many header
44+
45+manyheader = do
46+ c <- B.peekChar'
47+ if c == '\r' || c == '\n'
48+ then return []
49+ else (:) <$> header <*> manyheader
benchmarks/attoparsec-benchmarks.cabal+1
@@ -11,6 +11,7 @@ executable attoparsec-benchmarks
1111 other-modules:
1212 Common
1313 HeadersByteString
14+ HeadersByteString.Atto
1415 HeadersText
1516 Links
1617 Numbers