|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +import Criterion.Main (bench, bgroup, defaultMain, |
| 4 | + env, nfIO, whnf, whnfIO) |
| 5 | +import qualified Data.Attoparsec.Binary as Bin |
| 6 | +import qualified Data.ByteString as B |
| 7 | +import qualified Data.ByteString.Char8 as C8 |
| 8 | + |
| 9 | +import Control.Applicative |
| 10 | +import Data.Attoparsec.ByteString as P |
| 11 | +import Data.Attoparsec.ByteString.Char8 (char8, endOfLine, isDigit_w8) |
| 12 | +import Data.Attoparsec.ByteString.Char8 (isEndOfLine, |
| 13 | + isHorizontalSpace) |
| 14 | +import Data.ByteString (ByteString) |
| 15 | +import Data.Word (Word8) |
| 16 | + |
| 17 | +isToken :: Word8 -> Bool |
| 18 | +isToken w = w <= 127 && notInClass "\0-\31()<>@,;:\\\"/[]?={} \t" w |
| 19 | + |
| 20 | +skipSpaces :: Parser () |
| 21 | +skipSpaces = satisfy isHorizontalSpace *> skipWhile isHorizontalSpace |
| 22 | + |
| 23 | +data Request = Request { |
| 24 | + requestMethod :: ByteString |
| 25 | + , requestUri :: ByteString |
| 26 | + , requestVersion :: ByteString |
| 27 | + } deriving (Eq, Ord, Show) |
| 28 | + |
| 29 | +httpVersion :: Parser ByteString |
| 30 | +httpVersion = "HTTP/" *> P.takeWhile (\c -> isDigit_w8 c || c == 46) |
| 31 | + |
| 32 | +requestLine :: Parser Request |
| 33 | +requestLine = Request <$> (takeWhile1 isToken <* char8 ' ') |
| 34 | + <*> (takeWhile1 (/=32) <* char8 ' ') |
| 35 | + <*> (httpVersion <* endOfLine) |
| 36 | + |
| 37 | +data Header = Header { |
| 38 | + headerName :: ByteString |
| 39 | + , headerValue :: [ByteString] |
| 40 | + } deriving (Eq, Ord, Show) |
| 41 | + |
| 42 | +messageHeader :: Parser Header |
| 43 | +messageHeader = Header |
| 44 | + <$> (P.takeWhile isToken <* char8 ':' <* skipWhile isHorizontalSpace) |
| 45 | + <*> ((:) <$> (takeTill isEndOfLine <* endOfLine) |
| 46 | + <*> (many $ skipSpaces *> takeTill isEndOfLine <* endOfLine)) |
| 47 | + |
| 48 | +request :: Parser (Request, [Header]) |
| 49 | +request = (,) <$> requestLine <*> many messageHeader <* endOfLine |
| 50 | + |
| 51 | +allRequests = many1 request |
| 52 | + |
| 53 | +data Response = Response { |
| 54 | + responseVersion :: ByteString |
| 55 | + , responseCode :: ByteString |
| 56 | + , responseMsg :: ByteString |
| 57 | + } deriving (Eq, Ord, Show) |
| 58 | + |
| 59 | +responseLine :: Parser Response |
| 60 | +responseLine = Response <$> (httpVersion <* char8 ' ') |
| 61 | + <*> (P.takeWhile isDigit_w8 <* char8 ' ') |
| 62 | + <*> (takeTill isEndOfLine <* endOfLine) |
| 63 | + |
| 64 | +response :: Parser (Response, [Header]) |
| 65 | +response = (,) <$> responseLine <*> many messageHeader <* endOfLine |
| 66 | + |
| 67 | +smallFile :: FilePath |
| 68 | +smallFile = "../http-requests.txt" |
| 69 | +biggerFile :: FilePath |
| 70 | +biggerFile = "../bigger.txt" |
| 71 | + |
| 72 | +setupEnv = do |
| 73 | + small <- B.readFile smallFile |
| 74 | + bigger <- B.readFile biggerFile |
| 75 | + return (small, bigger) |
| 76 | + |
| 77 | +criterion :: IO () |
| 78 | +criterion = defaultMain |
| 79 | + [ |
| 80 | + env setupEnv $ \ ~(small, bigger) -> |
| 81 | + bgroup "IO" |
| 82 | + [ |
| 83 | + bench "small" $ whnf (P.parseOnly allRequests) small |
| 84 | + , bench "bigger" $ whnf (P.parseOnly allRequests) bigger |
| 85 | + ] |
| 86 | + ] |
| 87 | + |
| 88 | +main :: IO () |
| 89 | +main = criterion |
| 90 | +--main = B.readFile biggerFile >>= print . P.parseOnly allRequests |
0 commit comments