8000 Add Stopwatch on TraceableClient · symfony/symfony@a622a5a · GitHub
[go: up one dir, main page]

Skip to content

Commit a622a5a

Browse files
committed
Add Stopwatch on TraceableClient
1 parent 76c22fa commit a622a5a

File tree

5 files changed

+84
-20
lines changed

5 files changed

+84
-20
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ class Theme {
413413
'doctrine': '#ff6633',
414414
'messenger_middleware': '#bdb81e',
415415
'controller.argument_value_resolver': '#8c5de6',
416+
'http_client_request': '#ffa333',
416417
};
417418

418419
this.customCategoryColors = [

src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\HttpClient\TraceableHttpClient;
1819

@@ -36,7 +37,7 @@ public function process(ContainerBuilder $container)
3637

3738
foreach ($container->findTaggedServiceIds($this->clientTag) as $id => $tags) {
3839
$container->register('.debug.'.$id, TraceableHttpClient::class)
39-
->setArguments([new Reference('.debug.'.$id.'.inner')])
40+
->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)])
4041
->setDecoratedService($id);
4142
$container->getDefinition('data_collector.http_client')
4243
->addMethodCall('registerClient', [$id, new Reference('.debug.'.$id)]);

src/Symfony/Component/HttpClient/Response/TraceableResponse.php

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpClient\Exception\RedirectionException;
1616
use Symfony\Component\HttpClient\Exception\ServerException;
1717
use Symfony\Component\HttpClient\TraceableHttpClient;
18+
use Symfony\Component\Stopwatch\StopwatchEvent;
1819
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
1920
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
2021
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -32,57 +33,94 @@ class TraceableResponse implements ResponseInterface, StreamableInterface
3233
private $client;
3334
private $response;
3435
private $content;
36+
private $event;
3537

36-
public function __construct(HttpClientInterface $client, ResponseInterface $response, &$content)
38+
public function __construct(HttpClientInterface $client, ResponseInterface $response, &$content, StopwatchEvent $event = null)
3739
{
3840
$this->client = $client;
3941
$this->response = $response;
4042
$this->content = &$content;
43+
$this->event = $event;
44+
}
45+
46+
public function __destruct()
47+
{
48+
try {
49+
unset($this->response);
50+
} finally {
51+
if ($this->event && $this->event->isStarted()) {
52+
$this->event->stop();
53+
}
54+
}
4155
}
4256

4357
public function getStatusCode(): int
4458
{
59+
if ($this->event && $this->event->isStarted()) {
60+
$this->event->lap();
61+
}
62+
4563
return $this->response->getStatusCode();
4664
}
4765

4866
public function getHeaders(bool $throw = true): array
4967
{
68+
if ($this->event && $this->event->isStarted()) {
69+
$this->event->lap();
70+
}
71+
5072
return $this->response->getHeaders($throw);
5173
}
5274

5375
public function getContent(bool $throw = true): string
5476
{
55-
if (false === $this->content) {
56-
return $this->response->getContent($throw);
57-
}
77+
try {
78+
if (false === $this->content) {
79+
return $this->response->getContent($throw);
80+
}
5881

59-
$this->content = $this->response->getContent(false);
82+
$this->content = $this->response->getContent(false);
6083

61-
if ($throw) {
62-
$this->checkStatusCode($this->response->getStatusCode());
63-
}
84+
if ($throw) {
85+
$this->checkStatusCode($this->response->getStatusCode());
86+
}
6487

65-
return $this->content;
88+
return $this->content;
89+
} finally {
90+
if ($this->event && $this->event->isStarted()) {
91+
$this->event->stop();
92+
}
93+
}
6694
}
6795

6896
public function toArray(bool $throw = true): array
6997
{
70-
if (false === $this->content) {
71-
return $this->response->toArray($throw);
72-
}
98+
try {
99+
if (false === $this->content) {
100+
return $this->response->toArray($throw);
101+
}
73102

74-
$this->content = $this->response->toArray(false);
103+
$this->content = $this->response->toArray(false);
75104

76-
if ($throw) {
77-
$this->checkStatusCode($this->response->getStatusCode());
78-
}
105+
if ($throw) {
106+
$this->checkStatusCode($this->response->getStatusCode());
107+
}
79108

80-
return $this->content;
109+
return $this->content;
110+
} finally {
111+
if ($this->event && $this->event->isStarted()) {
112+
$this->event->stop();
113+
}
114+
}
81115
}
82116

83117
public function cancel(): void
84118
{
85119
$this->response->cancel();
120+
121+
if ($this->event && $this->event->isStarted()) {
122+
$this->event->stop();
123+
}
86124
}
87125

88126
public function getInfo(string $type = null)
@@ -132,6 +170,9 @@ public static function stream(HttpClientInterface $client, iterable $responses,
132170
}
133171

134172
foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) {
173+
if ($traceableMap[$r]->event && $traceableMap[$r]->event->isStarted() && $chunk->isLast()) {
174+
$traceableMap[$r]->event->stop();
175+
}
135176
yield $traceableMap[$r] => $chunk;
136177
}
137178
}

src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpClient\NativeHttpClient;
1717
use Symfony\Component\HttpClient\Response\MockResponse;
1818
use Symfony\Component\HttpClient\TraceableHttpClient;
19+
use Symfony\Component\Stopwatch\Stopwatch;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
2122

@@ -115,4 +116,21 @@ public function testStream()
115116
$this->assertGreaterThan(1, \count($chunks));
116117
$this->assertSame('Symfony is awesome!', implode('', $chunks));
117118
}
119+
120+
public function testStopwatch()
121+
{
122+
$sw = new Stopwatch(true);
123+
$sut = new TraceableHttpClient(new NativeHttpClient(), $sw);
124+
$response = $sut->request('GET', 'http://localhost:8057');
125+
126+
$response->getStatusCode();
127+
$response->getHeaders();
128+
$response->getContent();
129+
130+
$this->assertArrayHasKey('__root__', $sections = $sw->getSections());
131+
$this->assertCount(1, $events = $sections['__root__']->getEvents());
132+
$this->assertArrayHasKey('GET http://localhost:8057', $events);
133+
$this->assertCount(3, $events['GET http://localhost:8057']->getPeriods());
134+
$this->assertGreaterThan(0.0, $events['GET http://localhost:8057']->getDuration());
135+
}
118136
}

src/Symfony/Component/HttpClient/TraceableHttpClient.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\HttpClient\Response\ResponseStream;
1717
use Symfony\Component\HttpClient\Response\TraceableResponse;
18+
use Symfony\Component\Stopwatch\Stopwatch;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
1920
use Symfony\Contracts\HttpClient\ResponseInterface;
2021
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
@@ -27,10 +28,12 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface,
2728
{
2829
private $client;
2930
private $tracedRequests = [];
31+
private $stopwatch;
3032

31-
public function __construct(HttpClientInterface $client)
33+
public function __construct(HttpClientInterface $client, Stopwatch $stopwatch = null)
3234
{
3335
$this->client = $client;
36+
$this->stopwatch = $stopwatch;
3437
}
3538

3639
/**
@@ -62,7 +65,7 @@ public function request(string $method, string $url, array $options = []): Respo
6265
}
6366
};
6467

65-
return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $content);
68+
return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $content, null === $this->stopwatch ? null : $this->stopwatch->start("$method $url", 'http_client_request'));
6669
}
6770

6871
/**

0 commit comments

Comments
 (0)
0