Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically reduce dartdoc timeout with total time budget option. #1323

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
directory used for the analysis (helps switching different analyzer SDKs).
- `ToolEnvironment.create()` takes `pubHostedUrl` as `PackageAnalyzer.create()`
was removed.
- `InspectOptions.totalTimeBudget` to allow the dynamic reduction of `dartdocTimeout`.

**BREAKING CHANGES**

Expand Down
22 changes: 20 additions & 2 deletions lib/src/package_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,33 @@ import 'tool/run_constrained.dart';
import 'utils.dart';

class InspectOptions {
/// The PUB_HOSTED_URL to use for the package download and dependency analysis.
final String? pubHostedUrl;

/// The output directory to copy the generated docs. When not specified,
/// the documents will be discarded.
final String? dartdocOutputDir;
final Duration? dartdocTimeout;

/// The total time budget allocated for the full analysis. `pana` may not be
/// able to finish the analysis withing this time, but some parts will be
/// running with reduced timeouts in the attempt to complete the analysis
/// with partial results.
final Duration? totalTimeBudget;

/// The timeout to use when running `dartdoc` on the package.
///
/// When [totalTimeBudget] is also specified, the lower of
/// [dartdocTimeout] and the remaining budget will be used.
final Duration dartdocTimeout;

/// The line length parameter to be used for dart format checks.
final int? lineLength;

InspectOptions({
this.pubHostedUrl,
this.dartdocOutputDir,
this.dartdocTimeout,
this.totalTimeBudget,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we document this a bit.
Can you pass both timeouts? How do they interact? Are there defaults?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this and other options. PTAL

this.dartdocTimeout = const Duration(minutes: 5),
this.lineLength,
});
}
Expand Down
24 changes: 22 additions & 2 deletions lib/src/package_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,34 @@ class PackageContext {
final String packageDir;
final errors = <String>[];
final urlProblems = <String, String>{};
final _stopwatch = Stopwatch();

Pubspec? _pubspec;
List<CodeProblem>? _codeProblems;

PackageContext({
required this.sharedContext,
required this.packageDir,
});
}) {
_stopwatch.start();
}

ToolEnvironment get toolEnvironment => sharedContext.toolEnvironment;
InspectOptions get options => sharedContext.options;

/// Returns the remaining time budget, or a very small but positive duration
/// if we are already above the total budget.
///
/// Returns `null` if the total budget was not specified.
Duration? _remainingTimeBudget() {
if (options.totalTimeBudget == null) {
return null;
}
final threshold = const Duration(seconds: 1);
final remaining = options.totalTimeBudget! - _stopwatch.elapsed;
return remaining > threshold ? remaining : threshold;
}

late final Version currentSdkVersion =
Version.parse(toolEnvironment.runtimeInfo.sdkVersion);

Expand Down Expand Up @@ -235,7 +251,11 @@ class PackageContext {
return DartdocResult.skipped();
}
if (await resolveDependencies()) {
final timeout = options.dartdocTimeout ?? const Duration(minutes: 5);
var timeout = options.dartdocTimeout;
final rtb = _remainingTimeBudget();
if (rtb != null && rtb < timeout) {
timeout = rtb;
}
await normalizeDartdocOptionsYaml(packageDir);
try {
final pr = await toolEnvironment.dartdoc(
Expand Down