-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
FROM toxchat/toktok-stack:haskell AS build | ||
|
||
RUN ["rm", "-rf", "/work/hs-msgpack-types"] | ||
COPY --chown=builder:users . /work/hs-msgpack-types | ||
RUN ["stack", "install", "msgpack-types"] | ||
RUN ["rm", "-rf", "/work/hs-msgpack-arbitrary"] | ||
COPY --chown=builder:users . /work/hs-msgpack-arbitrary | ||
RUN ["stack", "install", "msgpack-arbitrary"] | ||
|
||
FROM scratch | ||
COPY --from=build /home/builder/.local/ / |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,3 @@ | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
{-# LANGUAGE StrictData #-} | ||
{-# LANGUAGE Trustworthy #-} | ||
module Data.MessagePack.Arbitrary () where | ||
|
||
import qualified Data.ByteString as S | ||
import Data.MessagePack.Types (Object (..)) | ||
import qualified Data.Text as T | ||
import qualified Data.Vector as V | ||
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary) | ||
import qualified Test.QuickCheck.Gen as Gen | ||
|
||
|
||
instance Arbitrary Object where | ||
arbitrary = Gen.sized $ \n -> Gen.oneof | ||
[ pure ObjectNil | ||
, ObjectBool <$> arbitrary | ||
, ObjectInt <$> negatives | ||
, ObjectWord <$> arbitrary | ||
, ObjectFloat <$> arbitrary | ||
, ObjectDouble <$> arbitrary | ||
, ObjectStr <$> (T.pack <$> arbitrary) | ||
, ObjectBin <$> (S.pack <$> arbitrary) | ||
, ObjectArray <$> (V.fromList <$> Gen.resize (n `div` 2) arbitrary) | ||
, ObjectMap <$> (V.fromList <$> Gen.resize (n `div` 4) arbitrary) | ||
, ObjectExt <$> arbitrary <*> (S.pack <$> arbitrary) | ||
] | ||
where negatives = Gen.choose (minBound, -1) | ||
import Test.QuickCheck.Instances.MessagePack () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Test.MessagePack.Generate (generate) where | ||
|
||
import Control.Monad (when) | ||
import qualified Data.ByteString.Lazy as L | ||
import Data.Int (Int64) | ||
import Data.MessagePack.Arbitrary () | ||
import Data.MessagePack.Types (Object (..)) | ||
import Data.Time.Clock (diffUTCTime, getCurrentTime) | ||
import System.Environment (getArgs) | ||
import System.IO (hPutStr, hPutStrLn, stderr) | ||
import Test.QuickCheck.Arbitrary (arbitrary) | ||
import qualified Test.QuickCheck.Gen as Gen | ||
import Test.QuickCheck.Instances.Vector () | ||
import Test.QuickCheck.Random (mkQCGen) | ||
|
||
|
||
seed :: Int | ||
seed = 33 | ||
|
||
|
||
showBytes :: Int64 -> String | ||
showBytes size | ||
| size > 10 * (1024 * 1024) = show (size `div` (1024 * 1024)) <> " MiB" | ||
| size > 10 * 1024 = show (size `div` 1024) <> " KiB" | ||
| otherwise = show size <> " B" | ||
|
||
|
||
showSpeed :: Int64 -> Double -> String | ||
showSpeed size time = | ||
show (fromIntegral size / (1024 * 1024) / time) <> " MiB/s" | ||
|
||
|
||
generate :: (Object -> L.ByteString) -> IO () | ||
generate pack = do | ||
size:_ <- (++[30]) . map read <$> getArgs | ||
|
||
start <- getCurrentTime | ||
hPutStrLn stderr "Generating sample..." | ||
|
||
let sample@(ObjectArray array) = ObjectArray $ Gen.unGen (Gen.resize size arbitrary) (mkQCGen 0) seed | ||
when (sample == sample) $ -- force deep evaluation of the whole structure (kind of deepseq) | ||
hPutStr stderr $ "Generated msgpack array of length " <> show (length array) | ||
sampleTime <- getCurrentTime | ||
hPutStrLn stderr $ " in " <> show (diffUTCTime sampleTime start) | ||
|
||
let packed = pack sample | ||
hPutStr stderr $ "Message packed into " <> showBytes (L.length packed) | ||
packTime <- getCurrentTime | ||
hPutStrLn stderr $ " in " <> show (diffUTCTime packTime sampleTime) | ||
|
||
hPutStrLn stderr $ "Packing speed: " <> showSpeed (L.length packed) (realToFrac (diffUTCTime packTime sampleTime)) | ||
|
||
L.putStr packed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
{-# LANGUAGE StrictData #-} | ||
{-# LANGUAGE Trustworthy #-} | ||
module Test.QuickCheck.Instances.MessagePack () where | ||
|
||
import qualified Data.ByteString as S | ||
import Data.MessagePack.Types (Object (..)) | ||
import qualified Data.Text as T | ||
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary) | ||
import qualified Test.QuickCheck.Gen as Gen | ||
import Test.QuickCheck.Instances.ByteString () | ||
import Test.QuickCheck.Instances.Vector () | ||
|
||
|
||
instance Arbitrary Object where | ||
arbitrary = Gen.sized $ \n -> Gen.oneof | ||
[ pure ObjectNil | ||
, ObjectBool <$> arbitrary | ||
, ObjectInt <$> negatives | ||
, ObjectWord <$> arbitrary | ||
, ObjectFloat <$> arbitrary | ||
, ObjectDouble <$> arbitrary | ||
, ObjectStr <$> (T.pack <$> arbitrary) | ||
, ObjectBin <$> (S.pack <$> arbitrary) | ||
, ObjectArray <$> Gen.resize (n `div` 2) arbitrary | ||
, ObjectMap <$> Gen.resize (n `div` 4) arbitrary | ||
, ObjectExt <$> arbitrary <*> arbitrary | ||
] | ||
where negatives = Gen.choose (minBound, -1) |