swarm/bench/Benchmark.hs

122 lines
3.9 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Control.Lens ((&), (.~))
import Control.Monad (replicateM_)
import Control.Monad.Except (runExceptT)
import Control.Monad.State (evalStateT, execStateT)
import Criterion.Main (Benchmark, bench, bgroup, defaultConfig, defaultMainWith, whnfAppIO)
import Criterion.Types (Config (timeLimit))
import Data.Functor.Const qualified as F
import Data.Int (Int64)
import Linear.V2 (V2 (V2))
import Swarm.Game.CESK (emptyStore, initMachine)
Use a new opaque type for robots instead of strings (#303) The basic idea of this change is to create a new `robot` type and use it to identify robots instead of `string` names. Internally, a `robot` value is just a (unique) `Int`. Closes #212 . This ended up turning into a sort of constellation of related changes. - Add the `robot` type and change the type of various built-in functions which used to take a robot name so they now take a `robot` (`give`, `install`, `reprogram`, `view`, `upload`) and change `build` so it returns a `robot`. - All internal data structures that store robots are now keyed by a unique (`Int`) robot ID rather than by name. - Add a `setname` command for setting a robot's display name (which no longer needs to uniquely identify a robot). - Import a big list of words which we can use to randomly pick names for robots, just for fun. This is why the diff says "+31,050 -265"; I did not write 31 thousand lines of code. - Add constants `base`, `parent`, and `self` for getting a `robot` value referring to the base, one's parent, and one's self, respectively. - Top-level binders like `r <- build {move}` now export a variable binding which can be used in later expressions entered at the REPL; additionally, unlike Haskell, a binder can now appear as the last statement in a block. - Fix the pretty-printer for `Value` by doubling down on our current strategy of injecting `Value`s back into `Term`s and then pretty-printing the result. I am now convinced this is the Right Way (tm) to do this; it only required adding a couple additional kinds of `Term` which represent internal results of evaluation and cannot show up in the surface language (`TRef`, `TRobot`). - Update the tutorial. - While updating the tutorial, I noticed that #294 had introduced a bug, where the inventory display no longer updated when 0 copies of an entity are added to the inventory (as with `scan` + `upload`), so I fixed that by changing the way inventory hashes are computed. I tried running the benchmarks both before & after this change. I was hoping that it might speed things up to be using `IntMap` and `IntSet` instead of looking things up by `Text` keys in a `Map` all the time. However, if I'm interpreting the results correctly, it seems like it didn't really make all that much difference, at least for the particular benchmarks we have.
2022-03-02 06:00:44 +03:00
import Swarm.Game.Display (defaultRobotDisplay)
import Swarm.Game.Robot (URobot, mkRobot)
import Swarm.Game.State (GameState, addURobot, classicGame0, creativeMode, world)
import Swarm.Game.Step (gameTick)
import Swarm.Game.Terrain (TerrainType (DirtT))
import Swarm.Game.World (newWorld)
import Swarm.Language.Context qualified as Context
import Swarm.Language.Pipeline (ProcessedTerm)
import Swarm.Language.Pipeline.QQ (tmQ)
import Swarm.Language.Syntax (north)
-- | The program of a robot that does nothing.
idleProgram :: ProcessedTerm
idleProgram = [tmQ| {} |]
-- | The program of a robot which waits a random number of ticks, changes its
-- appearence, then waits another random number of ticks, places a tree, and
-- then self-destructs.
treeProgram :: ProcessedTerm
treeProgram =
[tmQ|
Only update robots that are not waiting. (#176) Resolves #20 by introducing an active robots set and a waiting robots queue as discussed in the issue. Growing trees benchmark before change: ``` benchmarking run 1000 game ticks/10 trees time 141.1 ms (137.3 ms .. 146.4 ms) 0.997 R² (0.995 R² .. 1.000 R²) mean 140.1 ms (138.5 ms .. 142.2 ms) std dev 3.807 ms (2.703 ms .. 5.189 ms) benchmarking run 1000 game ticks/20 trees time 298.9 ms (281.7 ms .. 326.0 ms) 0.989 R² (0.972 R² .. 0.998 R²) mean 301.7 ms (295.0 ms .. 311.0 ms) std dev 13.93 ms (10.02 ms .. 19.92 ms) benchmarking run 1000 game ticks/30 trees time 470.9 ms (432.0 ms .. 513.0 ms) 0.991 R² (0.978 R² .. 1.000 R²) mean 450.1 ms (441.9 ms .. 468.4 ms) std dev 18.85 ms (5.941 ms .. 30.21 ms) variance introduced by outliers: 11% (moderately inflated) ``` After change: ``` benchmarking run 1000 game ticks/10 trees time 4.666 ms (4.529 ms .. 4.955 ms) 0.963 R² (0.899 R² .. 0.999 R²) mean 4.447 ms (4.361 ms .. 4.668 ms) std dev 539.2 μs (185.5 μs .. 1.097 ms) variance introduced by outliers: 80% (severely inflated) benchmarking run 1000 game ticks/20 trees time 6.955 ms (6.826 ms .. 7.122 ms) 0.996 R² (0.993 R² .. 0.999 R²) mean 6.855 ms (6.795 ms .. 6.936 ms) std dev 262.8 μs (197.0 μs .. 340.6 μs) variance introduced by outliers: 25% (moderately inflated) benchmarking run 1000 game ticks/30 trees time 10.53 ms (9.738 ms .. 11.47 ms) 0.954 R² (0.914 R² .. 0.988 R²) mean 9.504 ms (9.226 ms .. 9.974 ms) std dev 1.211 ms (809.9 μs .. 1.948 ms) variance introduced by outliers: 76% (severely inflated) ```
2021-10-05 23:54:12 +03:00
{
r <- random 100;
wait (r + 300);
appear "|";
r <- random 100;
wait (r + 300);
place "tree";
selfdestruct
}
|]
-- | The program of a robot that moves forward forever.
moverProgram :: ProcessedTerm
moverProgram =
[tmQ|
let forever : cmd () -> cmd () = \c. c; forever c
in forever move
|]
-- | The program of a robot that moves in circles forever.
circlerProgram :: ProcessedTerm
circlerProgram =
[tmQ|
let forever : cmd () -> cmd () = \c. c; forever c
in forever (
move;
turn right;
move;
turn right;
move;
turn right;
move;
turn right;
)
|]
-- | Initializes a robot with program prog at location loc facing north.
initRobot :: ProcessedTerm -> V2 Int64 -> URobot
initRobot prog loc = mkRobot (F.Const ()) Nothing "" [] north loc defaultRobotDisplay (initMachine prog Context.empty emptyStore) [] [] False 0
-- | Creates a GameState with numRobot copies of robot on a blank map, aligned
-- in a row starting at (0,0) and spreading east.
mkGameState :: (V2 Int64 -> URobot) -> Int -> IO GameState
mkGameState robotMaker numRobots = do
let robots = [robotMaker (V2 (fromIntegral x) 0) | x <- [0 .. numRobots - 1]]
Right initState <- runExceptT classicGame0
execStateT
(mapM addURobot robots)
( initState
& creativeMode .~ True
& world .~ newWorld (const (fromEnum DirtT, Nothing))
)
-- | Runs numGameTicks ticks of the game.
runGame :: Int -> GameState -> IO ()
runGame numGameTicks = evalStateT (replicateM_ numGameTicks gameTick)
main :: IO ()
main = do
idlers <- mkGameStates idleProgram [10, 20 .. 40]
trees <- mkGameStates treeProgram [10, 20 .. 40]
circlers <- mkGameStates circlerProgram [10, 20 .. 40]
movers <- mkGameStates moverProgram [10, 20 .. 40]
-- In theory we should force the evaluation of these game states to normal
-- form before running the benchmarks. In practice, the first of the many
-- criterion runs for each of these benchmarks doesn't look like an outlier.
defaultMainWith
(defaultConfig {timeLimit = 10})
[ bgroup
"run 1000 game ticks"
[ bgroup "idlers" (toBenchmarks idlers)
, bgroup "trees" (toBenchmarks trees)
, bgroup "circlers" (toBenchmarks circlers)
, bgroup "movers" (toBenchmarks movers)
]
]
where
mkGameStates :: ProcessedTerm -> [Int] -> IO [(Int, GameState)]
mkGameStates prog sizes = zip sizes <$> mapM (mkGameState (initRobot prog)) sizes
toBenchmarks :: [(Int, GameState)] -> [Benchmark]
toBenchmarks gameStates =
[ bench (show n) $ whnfAppIO (runGame 1000) gameState
| (n, gameState) <- gameStates
]