<commit_before>{-# LANGUAGE CPP, OverloadedStrings, ExistentialQuantification, BangPatterns #-}

#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#include "MachDeps.h"
#endif

module Main where

import Control.DeepSeq
import Control.Exception (evaluate)
import Criterion.Main
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Lazy as L
import Data.Char (ord)
import Data.Word (Word8)

import Control.Applicative
import Data.Binary.Get

import qualified Data.Serialize.Get as Cereal

import qualified Data.Attoparsec.ByteString as A
import qualified Data.Attoparsec.ByteString.Lazy as AL

#if !MIN_VERSION_bytestring(0,10,0)
instance NFData S.ByteString
instance NFData L.ByteString where
  rnf = rnf . L.toChunks
#endif

main :: IO ()
main = do
  evaluate $ rnf [
    rnf brackets,
    rnf bracketsInChunks,
    rnf bracketCount,
    rnf oneMegabyte,
    rnf oneMegabyteLBS
     ]
  defaultMain
    [
      bench "brackets 100kb one chunk input" $
        whnf (checkBracket . runTest bracketParser) brackets
    , bench "brackets 100kb in 100 byte chunks" $
        whnf (checkBracket . runTest bracketParser) bracketsInChunks
    , bench "Attoparsec lazy-bs brackets 100kb one chunk" $
        whnf (checkBracket . runAttoL bracketParser_atto) brackets
    , bench "Attoparsec lazy-bs brackets 100kb in 100 byte chunks" $
        whnf (checkBracket . runAttoL bracketParser_atto) bracketsInChunks
    , bench "Attoparsec strict-bs brackets 100kb" $
        whnf (checkBracket . runAtto bracketParser_atto) $ S.concat (L.toChunks brackets)
    , bench "Cereal strict-bs brackets 100kb" $
        whnf (checkBracket . runCereal bracketParser_cereal) $ S.concat (L.toChunks brackets)
    , bench "Binary getStruct4 1MB struct of 4 word8" $
        whnf (runTest (getStruct4 mega)) oneMegabyteLBS
    , bench "Cereal getStruct4 1MB struct of 4 word8" $
        whnf (runCereal (getStruct4_cereal mega)) oneMegabyte
    , bench "Attoparsec getStruct4 1MB struct of 4 word8" $
        whnf (runAtto (getStruct4_atto mega)) oneMegabyte
    , bench "Binary getWord8 1MB chunk size 1 byte" $
        whnf (runTest (getWord8N1 mega)) oneMegabyteLBS
    , bench "Cereal getWord8 1MB chunk size 1 byte" $
        whnf (runCereal (getWord8N1_cereal mega)) oneMegabyte
    , bench "Attoparsec getWord8 1MB chunk size 1 byte" $
        whnf (runAtto (getWord8N1_atto mega)) oneMegabyte
    , bench "getWord8 1MB chunk size 2 bytes" $
        whnf (runTest (getWord8N2 mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 4 bytes" $
        whnf (runTest (getWord8N4 mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 8 bytes" $
        whnf (runTest (getWord8N8 mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 16 bytes" $
        whnf (runTest (getWord8N16 mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 2 bytes Applicative" $
        whnf (runTest (getWord8N2A mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 4 bytes Applicative" $
        whnf (runTest (getWord8N4A mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 8 bytes Applicative" $
        whnf (runTest (getWord8N8A mega)) oneMegabyteLBS
    , bench "getWord8 1MB chunk size 16 bytes Applicative" $
        whnf (runTest (getWord8N16A mega)) oneMegabyteLBS
    ]

checkBracket :: Int -> Int
checkBracket x | x == bracketCount = x
               | otherwise = error "argh!"

runTest :: Get a -> L.ByteString -> a
runTest decoder inp = runGet decoder inp

runCereal :: Cereal.Get a -> C8.ByteString -> a<commit_msg>Structure the  benchmarks in the the "get" bench in groups.<commit_after>{-# LANGUAGE CPP, OverloadedStrings, ExistentialQuantification, BangPatterns #-}

#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#include "MachDeps.h"
#endif

module Main where

import Control.DeepSeq
import Control.Exception (evaluate)
import Criterion.Main
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Lazy as L
import Data.Char (ord)
import Data.Word (Word8)

import Control.Applicative
import Data.Binary.Get

import qualified Data.Serialize.Get as Cereal

import qualified Data.Attoparsec.ByteString as A
import qualified Data.Attoparsec.ByteString.Lazy as AL

#if !MIN_VERSION_bytestring(0,10,0)
instance NFData S.ByteString
instance NFData L.ByteString where
  rnf = rnf . L.toChunks
#endif

main :: IO ()
main = do
  evaluate $ rnf [
    rnf brackets,
    rnf bracketsInChunks,
    rnf bracketCount,
    rnf oneMegabyte,
    rnf oneMegabyteLBS
     ]
  defaultMain
    [ bgroup "brackets"
        [ bench "Binary 100kb, one chunk" $
            whnf (checkBracket . runTest bracketParser) brackets
        , bench "Binary 100kb, 100 byte chunks" $
            whnf (checkBracket . runTest bracketParser) bracketsInChunks
        , bench "Attoparsec lazy-bs 100kb, one chunk" $
            whnf (checkBracket . runAttoL bracketParser_atto) brackets
        , bench "Attoparsec lazy-bs 100kb, 100 byte chunks" $
            whnf (checkBracket . runAttoL bracketParser_atto) bracketsInChunks
        , bench "Attoparsec strict-bs 100kb" $
            whnf (checkBracket . runAtto bracketParser_atto) $ S.concat (L.toChunks brackets)
        , bench "Cereal strict-bs 100kb" $
            whnf (checkBracket . runCereal bracketParser_cereal) $ S.concat (L.toChunks brackets)
        ]
    , bgroup "comparison getStruct4, 1MB of struct of 4 Word8s"
      [ bench "Attoparsec" $
          whnf (runAtto (getStruct4_atto mega)) oneMegabyte
      , bench "Binary" $
          whnf (runTest (getStruct4 mega)) oneMegabyteLBS
      , bench "Cereal" $
          whnf (runCereal (getStruct4_cereal mega)) oneMegabyte
      ]
    , bgroup "comparison getWord8, 1MB"
        [ bench "Attoparsec" $
            whnf (runAtto (getWord8N1_atto mega)) oneMegabyte
        , bench "Binary" $
            whnf (runTest (getWord8N1 mega)) oneMegabyteLBS
        , bench "Cereal" $
            whnf (runCereal (getWord8N1_cereal mega)) oneMegabyte
        ]
    , bgroup "getWord8 1MB"
        [ bench "chunk size 2 bytes" $
            whnf (runTest (getWord8N2 mega)) oneMegabyteLBS
        , bench "chunk size 4 bytes" $
            whnf (runTest (getWord8N4 mega)) oneMegabyteLBS
        , bench "chunk size 8 bytes" $
            whnf (runTest (getWord8N8 mega)) oneMegabyteLBS
        , bench "chunk size 16 bytes" $
            whnf (runTest (getWord8N16 mega)) oneMegabyteLBS
        ]
    , bgroup "getWord8 1MB Applicative"
        [ bench "chunk size 2 bytes" $
            whnf (runTest (getWord8N2A mega)) oneMegabyteLBS
        , bench "chunk size 4 bytes" $
            whnf (runTest (getWord8N4A mega)) oneMegabyteLBS
        , bench "chunk size 8 bytes" $
            whnf (runTest (getWord8N8A mega)) oneMegabyteLBS
        , bench "chunk size 16 bytes" $
            whnf (runTest (getWord8N16A mega)) oneMegabyteLBS
        ]
    ]

checkBracket :: Int -> Int
checkBracket x | x == bracketCount = x
               | otherwise = error "argh!"

runTest :: Get a -> L.ByteString -> a
runTest decoder inp = runGet decoder inp

runCereal :: Cereal.Get a -> C8.ByteString -> a
{'fraction_non_alphanumeric': 0.07959579180509413, 'fraction_numerical': 0.02560908084163898, 'mean_word_length': 3.0658413055711873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7011506', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2390, 'n_words': 707}