on master help sign in
Added bench
0ea69daiko
bench/Main.hs+24−1
@@ -1,3 +1,26 @@
1module Main (main) where
1module Main (main) where
2
2
3+import Control.Monad
4+import Data.ByteString.Lazy qualified as BSL
5+import Data.Function
6+import Data.Text.Lazy.Encoding as TL
7+import Gauge
8+import Nock
9+import Nock.Parser (noun)
10+import System.FilePath.Posix
11+import System.Process.Typed
12+import Test.Tasty.Golden (findByExtension)
13+
14+main :: IO ()
3−main = pure ()
15+main = do
16+ files <- findByExtension [".hoon"] "tests/golden"
17+ benchmarks <- forM files $ \hoonFile -> do
18+ hoon <- BSL.readFile hoonFile
19+ nock <-
20+ fmap (Nock.Parser.noun . TL.decodeUtf8 . BSL.dropEnd 6 . BSL.drop 11 . snd)
21+ . readProcessStdout
22+ $ proc "urbit" ["eval"]
23+ & setStdin (byteStringInput hoon)
24+ & setStderr nullStream
25+ pure $ bench (takeBaseName hoonFile) $ nf tar $ cell (atom 0) nock
26+ defaultMain benchmarks
exe/Main.hs+1
@@ -1,4 +1,5 @@
1module Main (main) where
1module Main (main) where
2
2
3+main :: IO ()
3main = pure ()
4main = pure ()
4
5
package.yaml+4
@@ -17,6 +17,7 @@ dependencies:
17 - typed-process
17 - typed-process
18 - gauge
18 - gauge
19 - filepath
19 - filepath
20+ - deepseq
20library:
21library:
21 source-dirs: src
22 source-dirs: src
22
23
@@ -29,6 +30,9 @@ benchmarks:
29 nock-bench:
30 nock-bench:
30 main: bench/Main.hs
31 main: bench/Main.hs
31
32
33+ dependencies:
34+ - tasty
35+ - nock
32ghc-options: >
36ghc-options: >
33 -Weverything
37 -Weverything
34 -Wno-unsafe
38 -Wno-unsafe
src/Nock.hs+5−6
@@ -2,17 +2,16 @@
2
2
3module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) where
3module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) where
4
4
5+import Control.DeepSeq (NFData)
5import Data.Hashable
6import Data.Hashable
6−import Debug.Trace (trace, traceShowId)
7import GHC.Generics (Generic)
7import GHC.Generics (Generic)
8−import GHC.Stack
9import Numeric.Natural
8import Numeric.Natural
10−import Test.QuickCheck.Arbitrary.Generic
11
9
12data Annotation = Annotation
10data Annotation = Annotation
13 { hash :: Int
11 { hash :: Int
14 }
12 }
15− deriving stock (Show)
13+ deriving stock (Show, Generic)
14+ deriving anyclass (NFData)
16
15
17atom :: Natural -> Noun
16atom :: Natural -> Noun
18atom nat = Atom nat Annotation {hash = hash nat}
17atom nat = Atom nat Annotation {hash = hash nat}
@@ -40,6 +39,7 @@ data RawNoun ann
40 = Atom !Natural ann
39 = Atom !Natural ann
41 | Cell (RawNoun ann) (RawNoun ann) ann
40 | Cell (RawNoun ann) (RawNoun ann) ann
42 deriving stock (Generic, Show)
41 deriving stock (Generic, Show)
42+ deriving anyclass (NFData)
43
43
44instance Eq (RawNoun ann) where
44instance Eq (RawNoun ann) where
45 (Atom lhs _) == (Atom rhs _) = lhs == rhs
45 (Atom lhs _) == (Atom rhs _) = lhs == rhs
@@ -70,8 +70,7 @@ fas ~(Cell ~(Atom lhs _) rhs _) =
70 case lhs of
70 case lhs of
71 1 -> rhs
71 1 -> rhs
72 2 -> case rhs of
72 2 -> case rhs of
73− (Cell lhs' _ _) -> lhs'
73+ ~(Cell lhs' _ _) -> lhs'
74− _ -> undefined
75 3 -> case rhs of
74 3 -> case rhs of
76 ~(Cell _ rhs' _) -> rhs'
75 ~(Cell _ rhs' _) -> rhs'
77 n
76 n
src/Nock/Jam.hs−1
@@ -11,7 +11,6 @@ import Data.Bits
11import Data.ByteString.Lazy (ByteString)
11import Data.ByteString.Lazy (ByteString)
12import Data.Map (Map)
12import Data.Map (Map)
13import Data.Map qualified as M
13import Data.Map qualified as M
14−import Data.Word
15import Nock
14import Nock
16import Numeric.Natural
15import Numeric.Natural
17
16
src/Nock/Parser.hs+4−4
@@ -3,19 +3,19 @@
3module Nock.Parser (noun) where
3module Nock.Parser (noun) where
4
4
5import Control.Applicative
5import Control.Applicative
6−import Data.Text as T
6+import Data.Text.Lazy as TL
7import Data.Void (Void)
7import Data.Void (Void)
8import Nock
8import Nock
9import Text.Megaparsec
9import Text.Megaparsec
10import Text.Megaparsec.Char
10import Text.Megaparsec.Char
11import Text.Megaparsec.Char.Lexer (decimal)
11import Text.Megaparsec.Char.Lexer (decimal)
12
12
13−noun :: Text -> Noun
13+noun :: TL.Text -> Noun
14−noun txt = case Text.Megaparsec.parse (space *> parser <* space) "" . T.filter (\c -> c /= '.') $ txt of
14+noun txt = case Text.Megaparsec.parse (space *> parser <* space) "" . TL.filter (\c -> c /= '.') $ txt of
15 Right non -> non
15 Right non -> non
16 Left err -> error $ errorBundlePretty err
16 Left err -> error $ errorBundlePretty err
17
17
18−type Parser = Parsec Void Text
18+type Parser = Parsec Void TL.Text
19
19
20parser :: Parser Noun
20parser :: Parser Noun
21parser = try atomParser <|> cellParser
21parser = try atomParser <|> cellParser
tests/Main.hs+1−17
@@ -1,32 +1,16 @@
1module Main (main) where
1module Main (main) where
2
2
3import Control.Monad
3import Control.Monad
4−import Data.ByteString qualified as BS
5import Data.ByteString.Lazy qualified as BSL
4import Data.ByteString.Lazy qualified as BSL
6−import Data.Char qualified as Char
7import Data.Function
5import Data.Function
8−import Data.Text as T
9−import Data.Text.Encoding qualified as T
10−import Data.Text.IO as T
11−import Data.Text.Lazy as TL
12import Data.Text.Lazy.Encoding as TL
6import Data.Text.Lazy.Encoding as TL
13−import Data.Text.Lazy.IO as TL
14−import Debug.Trace
15−import Debug.Trace (traceShowId)
16−import GHC.Base (unsafeChr)
17import Nock
7import Nock
18−import Nock.Jam
19−import Nock.Parser
20import Nock.Parser qualified
8import Nock.Parser qualified
21import Nock.Printer
9import Nock.Printer
22import System.FilePath.Posix
10import System.FilePath.Posix
23import System.Process.Typed
11import System.Process.Typed
24−import Test.Hspec
25−import Test.Hspec.QuickCheck
26−import Test.QuickCheck
27import Test.Tasty
12import Test.Tasty
28import Test.Tasty.Golden
13import Test.Tasty.Golden
29−import Test.Tasty.Hspec
30
14
31main :: IO ()
15main :: IO ()
32main = do
16main = do
@@ -41,7 +25,7 @@ goldenTest = do
41 pure $ goldenVsString (takeBaseName source) target $ do
25 pure $ goldenVsString (takeBaseName source) target $ do
42 hoon <- BSL.readFile source
26 hoon <- BSL.readFile source
43 nock <-
27 nock <-
44− fmap (Nock.Parser.noun . T.decodeUtf8 . BS.toStrict . BSL.dropEnd 6 . BSL.drop 11 . snd)
28+ fmap (Nock.Parser.noun . TL.decodeUtf8 . BSL.dropEnd 6 . BSL.drop 11 . snd)
45 . readProcessStdout
29 . readProcessStdout
46 $ proc "urbit" ["eval"]
30 $ proc "urbit" ["eval"]
47 & setStdin (byteStringInput hoon)
31 & setStdin (byteStringInput hoon)