Skip to content

Commit

Permalink
Fix time elapsed (#1277)
Browse files Browse the repository at this point in the history
* Show the actual time elapsed; add docs

* `requests.Response._headers_parsed_at` → `requests.Response._httpie_headers_parsed_at`

* Add `ELAPSED_TIME_LABEL` constant

* Tweak docs

* Tweak docs

* Allow multiple blank lines in Markdown files

* Add rudimentary tests for --meta with different --style’s

* Cleanup tests

* Cleanup tests

* Cleanup tests
  • Loading branch information
jkbrzt authored Jan 23, 2022
1 parent 8a03b7a commit c815e21
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 61 deletions.
99 changes: 54 additions & 45 deletions docs/README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/markdownlint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# MD028 Blank line inside blockquote
exclude_rule 'MD028'

# MD012 Multiple consecutive blank lines
exclude_rule 'MD012'

# Tell the linter to use ordered lists:
# 1. Foo
# 2. Bar
Expand Down
3 changes: 2 additions & 1 deletion httpie/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import sys
from contextlib import contextmanager
from time import monotonic
from typing import Any, Dict, Callable, Iterable
from urllib.parse import urlparse, urlunparse

Expand Down Expand Up @@ -108,7 +109,7 @@ def collect_messages(
**send_kwargs_merged,
**send_kwargs,
)

response._httpie_headers_parsed_at = monotonic()
expired_cookies += get_expired_cookies(
response.headers.get('Set-Cookie', '')
)
Expand Down
13 changes: 12 additions & 1 deletion httpie/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from time import monotonic

import requests

from enum import Enum, auto
Expand All @@ -15,6 +17,9 @@
from .utils import split_cookies, parse_content_type_header


ELAPSED_TIME_LABEL = 'Elapsed time'


class HTTPMessage:
"""Abstract class for HTTP messages."""

Expand Down Expand Up @@ -96,7 +101,13 @@ def headers(self):
@property
def metadata(self) -> str:
data = {}
data['Elapsed time'] = str(self._orig.elapsed.total_seconds()) + 's'
time_to_parse_headers = self._orig.elapsed.total_seconds()
# noinspection PyProtectedMember
time_since_headers_parsed = monotonic() - self._orig._httpie_headers_parsed_at
time_elapsed = time_to_parse_headers + time_since_headers_parsed
# data['Headers time'] = str(round(time_to_parse_headers, 5)) + 's'
# data['Body time'] = str(round(time_since_headers_parsed, 5)) + 's'
data[ELAPSED_TIME_LABEL] = str(round(time_elapsed, 10)) + 's'
return '\n'.join(
f'{key}: {value}'
for key, value in data.items()
Expand Down
1 change: 1 addition & 0 deletions httpie/output/formatters/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,5 @@ def make_styles():


PIE_STYLES = make_styles()
PIE_STYLE_NAMES = list(PIE_STYLES.keys())
BUNDLED_STYLES |= PIE_STYLES.keys()
4 changes: 3 additions & 1 deletion httpie/output/lexers/metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pygments

from httpie.models import ELAPSED_TIME_LABEL
from httpie.output.lexers.common import precise

SPEED_TOKENS = {
Expand Down Expand Up @@ -34,7 +36,7 @@ class MetadataLexer(pygments.lexer.RegexLexer):
tokens = {
'root': [
(
r'(Elapsed time)( *)(:)( *)(\d+\.\d+)(s)', pygments.lexer.bygroups(
fr'({ELAPSED_TIME_LABEL})( *)(:)( *)(\d+\.\d+)(s)', pygments.lexer.bygroups(
pygments.token.Name.Decorator, # Name
pygments.token.Text,
pygments.token.Operator, # Colon
Expand Down
14 changes: 10 additions & 4 deletions httpie/output/ui/palette.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Copy the brand palette
from typing import Optional

STYLE_PIE = 'pie'
STYLE_PIE_DARK = 'pie-dark'
STYLE_PIE_LIGHT = 'pie-light'


COLOR_PALETTE = {
# Copy the brand palette
'transparent': 'transparent',
'current': 'currentColor',
'white': '#F5F5F0',
Expand Down Expand Up @@ -138,10 +143,11 @@

COLOR_PALETTE['secondary'] = {'700': '#37523C', '600': '#6c6969', '500': '#6c6969'}


SHADE_NAMES = {
'500': 'pie-dark',
'600': 'pie',
'700': 'pie-light'
'500': STYLE_PIE_DARK,
'600': STYLE_PIE,
'700': STYLE_PIE_LIGHT
}

SHADES = [
Expand Down
20 changes: 15 additions & 5 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from .utils import http
import pytest

from httpie.models import ELAPSED_TIME_LABEL
from httpie.output.formatters.colors import PIE_STYLE_NAMES
from .utils import http, MockEnvironment, COLOR

def test_meta_elapsed_time(httpbin, monkeypatch):
r = http('--meta', httpbin + '/get')
for line in r.splitlines():
assert 'Elapsed time' in r

def test_meta_elapsed_time(httpbin):
r = http('--meta', httpbin + '/delay/1')
assert f'{ELAPSED_TIME_LABEL}: 1.' in r


@pytest.mark.parametrize('style', ['auto', 'fruity', *PIE_STYLE_NAMES])
def test_meta_elapsed_time_colors(httpbin, style):
r = http('--style', style, '--meta', httpbin + '/get', env=MockEnvironment(colors=256))
assert COLOR in r
assert ELAPSED_TIME_LABEL in r
4 changes: 2 additions & 2 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from httpie.cli.definition import parser
from httpie.encoding import UTF8
from httpie.output.formatters.colors import PIE_STYLES, get_lexer
from httpie.output.formatters.colors import get_lexer, PIE_STYLE_NAMES
from httpie.status import ExitStatus
from .fixtures import XML_DATA_RAW, XML_DATA_FORMATTED
from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http, DUMMY_URL
Expand Down Expand Up @@ -227,7 +227,7 @@ def test_ensure_contents_colored(httpbin, endpoint):
assert COLOR in r


@pytest.mark.parametrize('style', PIE_STYLES.keys())
@pytest.mark.parametrize('style', PIE_STYLE_NAMES)
def test_ensure_meta_is_colored(httpbin, style):
env = MockEnvironment(colors=256)
r = http('--meta', '--style', style, 'GET', httpbin + '/get', env=env)
Expand Down
5 changes: 3 additions & 2 deletions tests/utils/matching/test_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Here we test our output parsing and matching implementation, not HTTPie itself.
"""
from httpie.models import ELAPSED_TIME_LABEL
from httpie.output.writer import MESSAGE_SEPARATOR
from ...utils import CRLF
from . import assert_output_does_not_match, assert_output_matches, Expect
Expand Down Expand Up @@ -111,7 +112,7 @@ def test_assert_output_matches_response_meta():
assert_output_matches(
(
'Key: Value\n'
'Elapsed Time: 3.3s'
f'{ELAPSED_TIME_LABEL}: 3.3s'
),
[Expect.RESPONSE_META]
)
Expand All @@ -124,7 +125,7 @@ def test_assert_output_matches_whole_response():
f'AAA:BBB{CRLF}'
f'{CRLF}'
f'CCC{MESSAGE_SEPARATOR}'
'Elapsed Time: 3.3s'
f'{ELAPSED_TIME_LABEL}: 3.3s'
),
[Expect.RESPONSE_HEADERS, Expect.BODY, Expect.RESPONSE_META]
)
Expand Down

0 comments on commit c815e21

Please sign in to comment.