Skip to content

Commit

Permalink
Add a skeleton "http_profile" package (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored Nov 2, 2023
1 parent b9389fe commit 04777ac
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 18 deletions.
79 changes: 62 additions & 17 deletions .github/workflows/dart.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkgs/http_profile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* Skeleton class and test definitions.
27 changes: 27 additions & 0 deletions pkgs/http_profile/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2023, the Dart project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions pkgs/http_profile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
An **experimental** package that allows HTTP clients outside of the Dart SDK
to integrate with the DevTools Network tab.
33 changes: 33 additions & 0 deletions pkgs/http_profile/lib/http_profile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

/// A record of debugging information about an HTTP request.
final class HttpClientRequestProfile {
/// Whether HTTP profiling is enabled or not.
///
/// The value can be changed programmatically or through the DevTools Network
/// UX.
static bool get profilingEnabled => HttpClient.enableTimelineLogging;
static set profilingEnabled(bool enabled) =>
HttpClient.enableTimelineLogging = enabled;

String? requestMethod;
String? requestUri;

HttpClientRequestProfile._();

/// If HTTP profiling is enabled, returns
/// a [HttpClientRequestProfile] otherwise returns `null`.
static HttpClientRequestProfile? profile() {
// Always return `null` in product mode so that the
// profiling code can be tree shaken away.
if (const bool.fromEnvironment('dart.vm.product') || !profilingEnabled) {
return null;
}
final requestProfile = HttpClientRequestProfile._();
return requestProfile;
}
}
14 changes: 14 additions & 0 deletions pkgs/http_profile/mono_pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sdk:
- pubspec
- dev

stages:
- analyze_and_format:
- analyze: --fatal-infos
- format:
sdk:
- dev
- unit_test:
- test: --platform vm
os:
- linux
15 changes: 15 additions & 0 deletions pkgs/http_profile/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: http_profile
description: >-
A library used by HTTP client authors to integrate with the DevTools
Network tab.
publish_to: none
repository: https://github.com/dart-lang/http/tree/master/pkgs/http_profile

environment:
sdk: ^3.0.0

dependencies:
test: ^1.24.9

dev_dependencies:
dart_flutter_team_lints: ^2.1.1
22 changes: 22 additions & 0 deletions pkgs/http_profile/test/profiling_enabled_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:http_profile/http_profile.dart';
import 'package:test/test.dart';

void main() {
test('profiling enabled', () async {
HttpClientRequestProfile.profilingEnabled = true;
expect(HttpClient.enableTimelineLogging, true);
expect(HttpClientRequestProfile.profile(), isNotNull);
});

test('profiling disabled', () async {
HttpClientRequestProfile.profilingEnabled = false;
expect(HttpClient.enableTimelineLogging, false);
expect(HttpClientRequestProfile.profile(), isNull);
});
}
2 changes: 1 addition & 1 deletion tool/ci.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04777ac

Please sign in to comment.