|
| 1 | +import Data.Aeson (eitherDecodeFileStrict') |
| 2 | +import Data.Char (isHexDigit) |
| 3 | +import Data.List.NonEmpty (NonEmpty) |
| 4 | +import Data.List.NonEmpty qualified as NonEmpty |
| 5 | +import Data.Maybe (catMaybes) |
| 6 | +import Data.Text (Text) |
| 7 | +import Data.Text qualified as T |
| 8 | +import Data.Text.Lazy.IO qualified as LTIO |
| 9 | +import Database.Persist |
| 10 | +import Database.Persist.Sqlite (runMigration, runSqlite) |
| 11 | +import Lucid (renderText) |
| 12 | +import Options.Applicative |
| 13 | +import Perf.DB.Materialize |
| 14 | +import Perf.Types.DB qualified as DB |
| 15 | +import Perf.Types.External qualified as EX |
| 16 | +import Perf.Web.Layout |
| 17 | +import Perf.Web.Plot |
| 18 | +import System.Directory (makeAbsolute) |
| 19 | +import System.Exit (ExitCode (ExitSuccess), die) |
| 20 | +import System.FilePath (takeBaseName) |
| 21 | +import System.Info (os) |
| 22 | +import System.Process (rawSystem) |
| 23 | + |
| 24 | +data Cli = Cli |
| 25 | + { outputPath :: FilePath, |
| 26 | + sqlitePath :: Maybe FilePath, |
| 27 | + branchName :: Text, |
| 28 | + maxCommits :: Int, |
| 29 | + jsonFiles :: [FilePath] |
| 30 | + } |
| 31 | + |
| 32 | +data Source |
| 33 | + = JsonFiles (NonEmpty FilePath) |
| 34 | + | Sqlite FilePath Text Int |
| 35 | + |
| 36 | +main :: IO () |
| 37 | +main = do |
| 38 | + cli <- execParser parserInfo |
| 39 | + source <- validateSource cli |
| 40 | + html <- case source of |
| 41 | + JsonFiles files -> do |
| 42 | + snapshots <- mapM loadSnapshot $ NonEmpty.toList files |
| 43 | + pure $ |
| 44 | + staticLayout_ "Benchmarks" $ |
| 45 | + generateExternalPlots $ |
| 46 | + materializeExternalSnapshots $ |
| 47 | + NonEmpty.fromList snapshots |
| 48 | + Sqlite sqlite branch limit -> do |
| 49 | + benchmarks <- loadBenchmarksFromSqlite sqlite branch limit |
| 50 | + pure $ |
| 51 | + staticLayout_ ("Benchmarks: " <> branch) $ |
| 52 | + generateCommitPlots benchmarks |
| 53 | + absoluteOutput <- makeAbsolute cli.outputPath |
| 54 | + LTIO.writeFile absoluteOutput (renderText html) |
| 55 | + openFile absoluteOutput |
| 56 | + |
| 57 | +validateSource :: Cli -> IO Source |
| 58 | +validateSource cli = |
| 59 | + case (cli.sqlitePath, NonEmpty.nonEmpty cli.jsonFiles) of |
| 60 | + (Just sqlite, Nothing) -> pure $ Sqlite sqlite cli.branchName cli.maxCommits |
| 61 | + (Nothing, Just files) -> pure $ JsonFiles files |
| 62 | + (Just _, Just _) -> die "Use either JSON files or --sqlite, not both." |
| 63 | + (Nothing, Nothing) -> die "Provide one or more JSON files, or use --sqlite." |
| 64 | + |
| 65 | +loadSnapshot :: FilePath -> IO (Text, [EX.Benchmark]) |
| 66 | +loadSnapshot path = do |
| 67 | + decoded <- eitherDecodeFileStrict' path |
| 68 | + case decoded of |
| 69 | + Left err -> die $ "Failed to decode " <> path <> ": " <> err |
| 70 | + Right benchmarks -> pure (labelFromPath path, benchmarks) |
| 71 | + |
| 72 | +labelFromPath :: FilePath -> Text |
| 73 | +labelFromPath path = |
| 74 | + let base = takeBaseName path |
| 75 | + suffix = reverse $ takeWhile (/= '-') $ reverse base |
| 76 | + hasDash = '-' `elem` base |
| 77 | + in if hasDash && not (null suffix) && all isHexDigit suffix |
| 78 | + then T.pack suffix |
| 79 | + else T.pack base |
| 80 | + |
| 81 | +loadBenchmarksFromSqlite :: FilePath -> Text -> Int -> IO (BenchmarkSeries DB.Commit DB.Metric) |
| 82 | +loadBenchmarksFromSqlite sqlite branch limit = |
| 83 | + runSqlite (T.pack sqlite) do |
| 84 | + runMigration DB.migrateAll |
| 85 | + mbranch <- selectFirst [DB.BranchName ==. branch] [] |
| 86 | + case mbranch of |
| 87 | + Nothing -> pure mempty |
| 88 | + Just (Entity branchId _) -> do |
| 89 | + mappings <- |
| 90 | + selectList |
| 91 | + [DB.MapBranchCommitBranchId ==. branchId] |
| 92 | + [Desc DB.MapBranchCommitId, LimitTo limit] |
| 93 | + commits <- mapM (\mapping -> selectFirst [DB.CommitId ==. mapping.entityVal.mapBranchCommitCommitId] []) mappings |
| 94 | + case NonEmpty.nonEmpty $ reverse $ catMaybes commits of |
| 95 | + Nothing -> pure mempty |
| 96 | + Just existingCommits -> materializeCommits existingCommits |
| 97 | + |
| 98 | +openFile :: FilePath -> IO () |
| 99 | +openFile path = |
| 100 | + case os of |
| 101 | + "darwin" -> runOpen "open" |
| 102 | + "linux" -> runOpen "xdg-open" |
| 103 | + _ -> putStrLn $ "Wrote " <> path |
| 104 | + where |
| 105 | + runOpen command = do |
| 106 | + status <- rawSystem command [path] |
| 107 | + case status of |
| 108 | + ExitSuccess -> pure () |
| 109 | + _ -> putStrLn $ "Wrote " <> path |
| 110 | + |
| 111 | +parserInfo :: ParserInfo Cli |
| 112 | +parserInfo = |
| 113 | + info (cliParser <**> helper) $ |
| 114 | + fullDesc |
| 115 | + <> progDesc "Render benchmark graphs into a static HTML file." |
| 116 | + |
| 117 | +cliParser :: Parser Cli |
| 118 | +cliParser = |
| 119 | + Cli |
| 120 | + <$> strOption |
| 121 | + ( long "output" |
| 122 | + <> short 'o' |
| 123 | + <> metavar "PATH" |
| 124 | + <> value "benchmark-display.html" |
| 125 | + <> showDefault |
| 126 | + <> help "Output HTML path." |
| 127 | + ) |
| 128 | + <*> optional |
| 129 | + (strOption |
| 130 | + ( long "sqlite" |
| 131 | + <> metavar "PATH" |
| 132 | + <> help "Read benchmark data from sqlite database." |
| 133 | + )) |
| 134 | + <*> ( T.pack |
| 135 | + <$> strOption |
| 136 | + ( long "branch" |
| 137 | + <> metavar "BRANCH" |
| 138 | + <> value "master" |
| 139 | + <> showDefault |
| 140 | + <> help "Branch name to load in sqlite mode." |
| 141 | + ) |
| 142 | + ) |
| 143 | + <*> option |
| 144 | + auto |
| 145 | + ( long "limit" |
| 146 | + <> metavar "INT" |
| 147 | + <> value 28 |
| 148 | + <> showDefault |
| 149 | + <> help "Number of most recent commits in sqlite mode." |
| 150 | + ) |
| 151 | + <*> many |
| 152 | + (strArgument |
| 153 | + ( metavar "JSON_FILES..." |
| 154 | + <> help "JSON files, each containing a top-level array of Benchmark." |
| 155 | + )) |
0 commit comments