weigh 0.0.8 → 0.0.9
raw patch · 4 files changed
+72/−58 lines, 4 files
Files
- CHANGELOG +4/−1
- README.md +18/−45
- src/Weigh.hs +49/−11
- weigh.cabal +1/−1
CHANGELOG view
@@ -1,4 +1,7 @@-0.0.7:+0.0.9:+ * Support markdown output++0.0.8: * Support grouping 0.0.6:
README.md view
@@ -1,4 +1,4 @@-# weigh [](https://travis-ci.org/fpco/weigh)+# weigh [](https://travis-ci.org/fpco/weigh) Measures the memory usage of a Haskell value or function @@ -7,54 +7,27 @@ ``` haskell import Weigh --- | Weigh integers. main :: IO () main =- mainWith (do integers- ints)---- | Just counting integers.-integers :: Weigh ()-integers =- do func "integers count 0" count 0- func "integers count 1" count 1- func "integers count 2" count 2- func "integers count 3" count 3- func "integers count 10" count 10- func "integers count 100" count 100- where count :: Integer -> ()- count 0 = ()- count a = count (a - 1)---- | We count ints and ensure that the allocations are optimized away--- to only two 64-bit Ints (16 bytes).-ints :: Weigh ()-ints =- do validateFunc "ints count 1" count 1 (maxAllocs 24)- validateFunc "ints count 10" count 10 (maxAllocs 24)- validateFunc "ints count 1000000" count 1000000 (maxAllocs 24)- where count :: Int -> ()- count 0 = ()- count a = count (a - 1)+ mainWith+ (do func "integers count 0" count 0+ func "integers count 1" count 1+ func "integers count 10" count 10+ func "integers count 100" count 100)+ where+ count :: Integer -> ()+ count 0 = ()+ count a = count (a - 1) ``` Output results: -```-Case Bytes GCs Check-integers count 0 0 0 OK-integers count 1 32 0 OK-integers count 2 64 0 OK-integers count 3 96 0 OK-integers count 10 320 0 OK-integers count 100 3,200 0 OK-ints count 1 0 0 OK-ints count 10 0 0 OK-ints count 1000000 0 0 OK-```--You can try this out with `stack test` in the `weight` directory.--To try out other examples, try:+|Case|Allocated|GCs|+|:---|---:|---:|+|integers count 0|16|0|+|integers count 1|88|0|+|integers count 10|736|0|+|integers count 100|7,216|0| -* `stack test :weigh-maps --flag weigh:weigh-maps`+Output by default is plain text table; pass `--markdown` to get a+markdown output like the above.
src/Weigh.hs view
@@ -91,8 +91,12 @@ data Config = Config { configColumns :: [Column] , configPrefix :: String+ , configFormat :: !Format } deriving (Show) +data Format = Plain | Markdown+ deriving (Show)+ -- | Weigh specification monad. newtype Weigh a = Weigh {runWeigh :: State (Config, [Grouped Action]) a}@@ -167,7 +171,12 @@ Nothing -> (w, Nothing) Just a -> (w, actionCheck a w))) weights- , config)+ , config+ { configFormat =+ if any (== "--markdown") args+ then Markdown+ else configFormat config+ }) -------------------------------------------------------------------------------- -- User DSL@@ -178,7 +187,9 @@ -- | Default config. defaultConfig :: Config-defaultConfig = Config {configColumns = defaultColumns, configPrefix = ""}+defaultConfig =+ Config+ {configColumns = defaultColumns, configPrefix = "", configFormat = Plain} -- | Set the config. Default is: 'defaultConfig'. setColumns :: [Column] -> Weigh ()@@ -458,13 +469,20 @@ gs reportGroup :: Config -> [Char] -> [Grouped (Weight, Maybe String)] -> [Char]-reportGroup config title gs = title ++ "\n\n" ++ indent (report config gs)+reportGroup config title gs =+ case configFormat config of+ Plain -> title ++ "\n\n" ++ indent (report config gs)+ Markdown -> "#" ++ title ++ "\n\n" ++ report config gs -- | Make a report of the weights. reportTabular :: Config -> [(Weight,Maybe String)] -> String reportTabular config = tabled where- tabled = tablize . (select headings :) . map (select . toRow)+ tabled =+ (case configFormat config of+ Plain -> tablize+ Markdown -> mdtable) .+ (select headings :) . map (select . toRow) select row = mapMaybe (\name -> lookup name row) (configColumns config) headings = [ (Case, (True, "Case"))@@ -487,16 +505,36 @@ Just {} -> "INVALID")) ] +-- | Make a markdown table.+mdtable ::[[(Bool,String)]] -> String+mdtable rows = List.intercalate "\n" [heading, align, body]+ where+ heading = columns (map (\(_, str) -> str) (fromMaybe [] (listToMaybe rows)))+ align =+ columns+ (map+ (\(shouldAlignLeft, _) ->+ if shouldAlignLeft+ then ":---"+ else "---:")+ (fromMaybe [] (listToMaybe rows)))+ body =+ List.intercalate "\n" (map (\row -> columns (map snd row)) (drop 1 rows))+ columns xs = "|" ++ List.intercalate "|" xs ++ "|"+ -- | Make a table out of a list of rows. tablize :: [[(Bool,String)]] -> String tablize xs =- List.intercalate "\n"- (map (List.intercalate " " . map fill . zip [0 ..]) xs)- where fill (x',(left',text')) = printf ("%" ++ direction ++ show width ++ "s") text'- where direction = if left'- then "-"- else ""- width = maximum (map (length . snd . (!! x')) xs)+ List.intercalate "\n" (map (List.intercalate " " . map fill . zip [0 ..]) xs)+ where+ fill (x', (left', text')) =+ printf ("%" ++ direction ++ show width ++ "s") text'+ where+ direction =+ if left'+ then "-"+ else ""+ width = maximum (map (length . snd . (!! x')) xs) -- | Formatting an integral number to 1,000,000, etc. commas :: (Num a,Integral a,Show a) => a -> String
weigh.cabal view
@@ -1,5 +1,5 @@ name: weigh-version: 0.0.8+version: 0.0.9 synopsis: Measure allocations of a Haskell functions/values description: Please see README.md homepage: https://github.com/fpco/weigh#readme