Skip to content

Commit 88f184c

Browse files
committed
allow running benchmarks on examples generated via a script
1 parent 4c73eaf commit 88f184c

File tree

2 files changed

+71
-17
lines changed

2 files changed

+71
-17
lines changed

ghcide-bench/src/Experiments.hs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ charEdit p =
7979
.+ #rangeLength .== Nothing
8080
.+ #text .== "a"
8181

82+
headerEdit :: TextDocumentContentChangeEvent
83+
headerEdit =
84+
TextDocumentContentChangeEvent $ InL $ #range .== Range (Position 0 0) (Position 0 0)
85+
.+ #rangeLength .== Nothing
86+
.+ #text .== "-- header comment \n"
87+
8288
data DocumentPositions = DocumentPositions {
8389
-- | A position that can be used to generate non null goto-def and completion responses
8490
identifierP :: Maybe Position,
@@ -112,6 +118,16 @@ experiments =
112118
waitForProgressDone
113119
return True,
114120
---------------------------------------------------------------------------------------
121+
bench "edit-header" $ \docs -> do
122+
forM_ docs $ \DocumentPositions{..} -> do
123+
changeDoc doc [headerEdit]
124+
-- wait for a fresh build start
125+
waitForProgressStart
126+
-- wait for the build to be finished
127+
output "edit: waitForProgressDone"
128+
waitForProgressDone
129+
return True,
130+
---------------------------------------------------------------------------------------
115131
bench "hover after edit" $ \docs -> do
116132
forM_ docs $ \DocumentPositions{..} ->
117133
changeDoc doc [charEdit stringLiteralP]
@@ -276,23 +292,26 @@ configP =
276292
<*> optional (option auto (long "samples" <> metavar "NAT" <> help "override sampling count"))
277293
<*> strOption (long "ghcide" <> metavar "PATH" <> help "path to ghcide" <> value "ghcide")
278294
<*> option auto (long "timeout" <> value 60 <> help "timeout for waiting for a ghcide response")
279-
<*> ( Example "name"
280-
<$> (Right <$> packageP)
295+
<*> ( Example
296+
<$> exampleName
297+
<*> (ExampleHackage <$> packageP)
281298
<*> (some moduleOption <|> pure ["src/Distribution/Simple.hs"])
282299
<*> pure []
283-
<|>
284-
Example "name"
285-
<$> (Left <$> pathP)
286-
<*> some moduleOption
287-
<*> pure [])
300+
<|> Example
301+
<$> exampleName
302+
<*> pathOrScriptP
303+
<*> some moduleOption
304+
<*> pure [])
288305
<*> switch (long "lsp-config" <> help "Read an LSP config payload from standard input")
289306
where
290307
moduleOption = strOption (long "example-module" <> metavar "PATH")
308+
exampleName = strOption (long "example-name" <> metavar "NAME")
291309

292310
packageP = ExamplePackage
293311
<$> strOption (long "example-package-name" <> value "Cabal")
294312
<*> option versionP (long "example-package-version" <> value (makeVersion [3,6,0,0]))
295-
pathP = strOption (long "example-path")
313+
pathOrScriptP = ExamplePath <$> strOption (long "example-path")
314+
<|> ExampleScript <$> strOption (long "example-script") <*> many (strOption (long "example-script-args" <> help "arguments for the example generation script"))
296315

297316
versionP :: ReadM Version
298317
versionP = maybeReader $ extract . readP_to_S parseVersion
@@ -581,13 +600,25 @@ setup :: HasConfig => IO SetupResult
581600
setup = do
582601
-- when alreadyExists $ removeDirectoryRecursive examplesPath
583602
benchDir <- case exampleDetails(example ?config) of
584-
Left examplePath -> do
603+
ExamplePath examplePath -> do
585604
let hieYamlPath = examplePath </> "hie.yaml"
586605
alreadyExists <- doesFileExist hieYamlPath
587606
unless alreadyExists $
588607
cmd_ (Cwd examplePath) (FileStdout hieYamlPath) ("gen-hie"::String)
589608
return examplePath
590-
Right ExamplePackage{..} -> do
609+
ExampleScript examplePath' scriptArgs -> do
610+
let exampleDir = examplesPath </> exampleName (example ?config)
611+
alreadySetup <- doesDirectoryExist exampleDir
612+
unless alreadySetup $ do
613+
createDirectoryIfMissing True exampleDir
614+
examplePath <- makeAbsolute examplePath'
615+
cmd_ (Cwd exampleDir) examplePath scriptArgs
616+
let hieYamlPath = exampleDir </> "hie.yaml"
617+
alreadyExists <- doesFileExist hieYamlPath
618+
unless alreadyExists $
619+
cmd_ (Cwd exampleDir) (FileStdout hieYamlPath) ("gen-hie"::String)
620+
return exampleDir
621+
ExampleHackage ExamplePackage{..} -> do
591622
let path = examplesPath </> package
592623
package = packageName <> "-" <> showVersion packageVersion
593624
hieYamlPath = path </> "hie.yaml"
@@ -633,8 +664,9 @@ setup = do
633664
whenJust (shakeProfiling ?config) $ createDirectoryIfMissing True
634665

635666
let cleanUp = case exampleDetails(example ?config) of
636-
Right _ -> removeDirectoryRecursive examplesPath
637-
Left _ -> return ()
667+
ExampleHackage _ -> removeDirectoryRecursive examplesPath
668+
ExampleScript _ _ -> removeDirectoryRecursive examplesPath
669+
ExamplePath _ -> return ()
638670

639671
runBenchmarks = runBenchmarksFun benchDir
640672

ghcide-bench/src/Experiments/Types.hs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,19 @@ data ExamplePackage = ExamplePackage {packageName :: !String, packageVersion ::
4040

4141
data Example = Example
4242
{ exampleName :: !String
43-
, exampleDetails :: Either FilePath ExamplePackage
43+
, exampleDetails :: ExampleDetails
4444
, exampleModules :: [FilePath]
4545
, exampleExtraArgs :: [String]}
4646
deriving (Eq, Generic, Show)
4747
deriving anyclass (Binary, Hashable, NFData)
4848

49+
data ExampleDetails
50+
= ExamplePath FilePath
51+
| ExampleHackage ExamplePackage
52+
| ExampleScript FilePath [String]
53+
deriving (Eq, Generic, Show)
54+
deriving anyclass (Binary, Hashable, NFData)
55+
4956
instance FromJSON Example where
5057
parseJSON = withObject "example" $ \x -> do
5158
exampleName <- x .: "name"
@@ -55,24 +62,39 @@ instance FromJSON Example where
5562
path <- x .:? "path"
5663
case path of
5764
Just examplePath -> do
58-
let exampleDetails = Left examplePath
65+
script <- fromMaybe False <$> x.:? "script"
66+
args <- fromMaybe [] <$> x .:? "script-args"
67+
let exampleDetails
68+
| script = ExampleScript examplePath args
69+
| otherwise = ExamplePath examplePath
5970
return Example{..}
6071
Nothing -> do
6172
packageName <- x .: "package"
6273
packageVersion <- x .: "version"
63-
let exampleDetails = Right ExamplePackage{..}
74+
let exampleDetails = ExampleHackage ExamplePackage{..}
6475
return Example{..}
6576

6677
exampleToOptions :: Example -> [String] -> [String]
67-
exampleToOptions Example{exampleDetails = Right ExamplePackage{..}, ..} extraArgs =
78+
exampleToOptions Example{exampleDetails = ExampleHackage ExamplePackage{..}, ..} extraArgs =
6879
["--example-package-name", packageName
6980
,"--example-package-version", showVersion packageVersion
81+
,"--example-name", exampleName
7082
] ++
7183
["--example-module=" <> m | m <- exampleModules
7284
] ++
7385
["--ghcide-options=" <> o | o <- exampleExtraArgs ++ extraArgs]
74-
exampleToOptions Example{exampleDetails = Left examplePath, ..} extraArgs =
86+
exampleToOptions Example{exampleDetails = ExamplePath examplePath, ..} extraArgs =
7587
["--example-path", examplePath
88+
,"--example-name", exampleName
89+
] ++
90+
["--example-module=" <> m | m <- exampleModules
91+
] ++
92+
["--ghcide-options=" <> o | o <- exampleExtraArgs ++ extraArgs]
93+
exampleToOptions Example{exampleDetails = ExampleScript examplePath exampleArgs, ..} extraArgs =
94+
["--example-script", examplePath
95+
,"--example-name", exampleName
96+
] ++
97+
["--example-script-args=" <> o | o <- exampleArgs
7698
] ++
7799
["--example-module=" <> m | m <- exampleModules
78100
] ++

0 commit comments

Comments
 (0)