1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

feat(cli): support deno bench in the config file (#16608)

This PR adds the ability to set `include/exclude` fields for `deno
bench` in the configuration file.
This commit is contained in:
Geert-Jan Zwiers 2022-12-10 02:30:47 +01:00 committed by GitHub
parent 4eb8e875fd
commit 890b065310
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 189 additions and 11 deletions

View file

@ -429,6 +429,28 @@ pub struct TestConfig {
pub files: FilesConfig,
}
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct SerializedBenchConfig {
pub files: SerializedFilesConfig,
}
impl SerializedBenchConfig {
pub fn into_resolved(
self,
config_file_specifier: &ModuleSpecifier,
) -> Result<BenchConfig, AnyError> {
Ok(BenchConfig {
files: self.files.into_resolved(config_file_specifier)?,
})
}
}
#[derive(Clone, Debug, Default)]
pub struct BenchConfig {
pub files: FilesConfig,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum LockConfig {
@ -445,6 +467,7 @@ pub struct ConfigFileJson {
pub fmt: Option<Value>,
pub tasks: Option<Value>,
pub test: Option<Value>,
pub bench: Option<Value>,
pub lock: Option<Value>,
}
@ -653,9 +676,19 @@ impl ConfigFile {
pub fn to_test_config(&self) -> Result<Option<TestConfig>, AnyError> {
if let Some(config) = self.json.test.clone() {
let lint_config: SerializedTestConfig = serde_json::from_value(config)
let test_config: SerializedTestConfig = serde_json::from_value(config)
.context("Failed to parse \"test\" configuration")?;
Ok(Some(lint_config.into_resolved(&self.specifier)?))
Ok(Some(test_config.into_resolved(&self.specifier)?))
} else {
Ok(None)
}
}
pub fn to_bench_config(&self) -> Result<Option<BenchConfig>, AnyError> {
if let Some(config) = self.json.bench.clone() {
let bench_config: SerializedBenchConfig = serde_json::from_value(config)
.context("Failed to parse \"bench\" configuration")?;
Ok(Some(bench_config.into_resolved(&self.specifier)?))
} else {
Ok(None)
}

View file

@ -6,6 +6,7 @@ mod lockfile;
mod flags_allow_net;
pub use config_file::BenchConfig;
pub use config_file::CompilerOptions;
pub use config_file::ConfigFile;
pub use config_file::EmitConfigOptions;
@ -401,6 +402,14 @@ impl CliOptions {
}
}
pub fn to_bench_config(&self) -> Result<Option<BenchConfig>, AnyError> {
if let Some(config_file) = &self.maybe_config_file {
config_file.to_bench_config()
} else {
Ok(None)
}
}
pub fn to_fmt_config(&self) -> Result<Option<FmtConfig>, AnyError> {
if let Some(config) = &self.maybe_config_file {
config.to_fmt_config()

View file

@ -356,6 +356,31 @@
}
}
},
"bench": {
"description": "Configuration for deno bench",
"type": "object",
"properties": {
"files": {
"type": "object",
"properties": {
"include": {
"type": "array",
"description": "List of files or directories that will be searched for benchmarks.",
"items": {
"type": "string"
}
},
"exclude": {
"type": "array",
"description": "List of files or directories that will not be searched for benchmarks.",
"items": {
"type": "string"
}
}
}
}
}
},
"lock": {
"description": "Whether to use a lock file or the path to use for the lock file. Can be overridden by CLI arguments.",
"type": ["string", "boolean"],

View file

@ -165,6 +165,24 @@ mod bench {
exit_code: 1,
});
itest!(bench_with_config {
args: "bench --config bench/collect/deno.jsonc bench/collect",
exit_code: 0,
output: "bench/collect.out",
});
itest!(bench_with_config2 {
args: "bench --config bench/collect/deno2.jsonc bench/collect",
exit_code: 0,
output: "bench/collect2.out",
});
itest!(bench_with_malformed_config {
args: "bench --config bench/collect/deno.malformed.jsonc",
exit_code: 1,
output: "bench/collect_with_malformed_config.out",
});
#[test]
fn recursive_permissions_pledge() {
let output = util::deno_cmd()

View file

@ -1,7 +1,17 @@
Check [WILDCARD]/bench/collect/bench.ts
Check [WILDCARD]/bench/collect/include/2_bench.ts
Check [WILDCARD]/bench/collect/include/bench.ts
cpu: [WILDCARD]
runtime: deno [WILDCARD] ([WILDCARD])
[WILDCARD]/bench/collect/bench.ts
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------- -----------------------------
[WILDCARD]/bench/collect/include/2_bench.ts
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------- -----------------------------
[WILDCARD]/bench/collect/include/bench.ts
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------- -----------------------------

View file

@ -0,0 +1,7 @@
{
"bench": {
"files": {
"exclude": ["./ignore"]
}
}
}

View file

@ -0,0 +1,5 @@
{
"bench": {
"dont_know_this_field": {}
}
}

View file

@ -0,0 +1,8 @@
{
"bench": {
"files": {
"include": ["./include/"],
"exclude": ["./ignore", "./include/2_bench.ts"]
}
}
}

View file

View file

12
cli/tests/testdata/bench/collect2.out vendored Normal file
View file

@ -0,0 +1,12 @@
Check [WILDCARD]/bench/collect/bench.ts
Check [WILDCARD]/bench/collect/include/bench.ts
cpu: [WILDCARD]
runtime: deno [WILDCARD] ([WILDCARD])
[WILDCARD]/bench/collect/bench.ts
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------- -----------------------------
[WILDCARD]/bench/collect/include/bench.ts
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------- -----------------------------

View file

@ -0,0 +1,4 @@
error: Failed to parse "bench" configuration
Caused by:
unknown field `dont_know_this_field`, expected `files`

View file

@ -1,5 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::args::BenchConfig;
use crate::args::BenchFlags;
use crate::args::Flags;
use crate::args::TypeCheckMode;
@ -14,6 +15,7 @@ use crate::util::file_watcher;
use crate::util::file_watcher::ResolutionResult;
use crate::util::fs::collect_specifiers;
use crate::util::path::is_supported_ext;
use crate::util::path::specifier_to_file_path;
use crate::worker::create_main_worker_for_test_or_bench;
use deno_core::error::generic_error;
@ -490,9 +492,13 @@ pub async fn run_benchmarks(
let ps = ProcState::build(flags).await?;
let permissions =
Permissions::from_options(&ps.options.permissions_options())?;
let selection =
collect_include_ignore(&bench_flags, ps.options.to_bench_config()?);
let specifiers = collect_specifiers(
bench_flags.include.unwrap_or_else(|| vec![".".to_string()]),
&bench_flags.ignore.clone(),
selection.include,
&selection.ignore,
is_supported_bench_path,
)?;
@ -524,9 +530,11 @@ pub async fn run_benchmarks_with_watch(
let permissions =
Permissions::from_options(&ps.options.permissions_options())?;
let include = bench_flags.include.unwrap_or_else(|| vec![".".to_string()]);
let ignore = bench_flags.ignore.clone();
let paths_to_watch: Vec<_> = include.iter().map(PathBuf::from).collect();
let selection =
collect_include_ignore(&bench_flags, ps.options.to_bench_config()?);
let paths_to_watch: Vec<_> =
selection.include.iter().map(PathBuf::from).collect();
let no_check = ps.options.type_check_mode() == TypeCheckMode::None;
let resolver = |changed: Option<Vec<PathBuf>>| {
@ -534,8 +542,8 @@ pub async fn run_benchmarks_with_watch(
let paths_to_watch_clone = paths_to_watch.clone();
let files_changed = changed.is_some();
let include = include.clone();
let ignore = ignore.clone();
let include = selection.include.clone();
let ignore = selection.ignore.clone();
let ps = ps.clone();
async move {
@ -650,8 +658,8 @@ pub async fn run_benchmarks_with_watch(
let operation = |modules_to_reload: Vec<(ModuleSpecifier, ModuleKind)>| {
let filter = bench_flags.filter.clone();
let include = include.clone();
let ignore = ignore.clone();
let include = selection.include.clone();
let ignore = selection.ignore.clone();
let permissions = permissions.clone();
let ps = ps.clone();
@ -687,3 +695,42 @@ pub async fn run_benchmarks_with_watch(
Ok(())
}
struct IncludeIgnoreCollection {
include: Vec<String>,
ignore: Vec<PathBuf>,
}
fn collect_include_ignore(
bench_flags: &BenchFlags,
maybe_bench_config: Option<BenchConfig>,
) -> IncludeIgnoreCollection {
let mut include = bench_flags.include.clone().unwrap_or_default();
let mut ignore = bench_flags.ignore.clone();
if let Some(bench_config) = maybe_bench_config {
if include.is_empty() {
include = bench_config
.files
.include
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>();
}
if ignore.is_empty() {
ignore = bench_config
.files
.exclude
.iter()
.filter_map(|s| specifier_to_file_path(s).ok())
.collect::<Vec<_>>();
}
}
if include.is_empty() {
include.push(".".to_string());
}
IncludeIgnoreCollection { include, ignore }
}