xref: /curl/tests/http/test_04_stuttered.py (revision 3b30cc1a)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#***************************************************************************
4#                                  _   _ ____  _
5#  Project                     ___| | | |  _ \| |
6#                             / __| | | | |_) | |
7#                            | (__| |_| |  _ <| |___
8#                             \___|\___/|_| \_\_____|
9#
10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11#
12# This software is licensed as described in the file COPYING, which
13# you should have received as part of this distribution. The terms
14# are also available at https://curl.se/docs/copyright.html.
15#
16# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17# copies of the Software, and permit persons to whom the Software is
18# furnished to do so, under the terms of the COPYING file.
19#
20# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21# KIND, either express or implied.
22#
23# SPDX-License-Identifier: curl
24#
25###########################################################################
26#
27import logging
28import os
29from typing import Tuple, List, Dict
30import pytest
31
32from testenv import Env, CurlClient
33
34
35log = logging.getLogger(__name__)
36
37
38@pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
39@pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
40class TestStuttered:
41
42    @pytest.fixture(autouse=True, scope='class')
43    def _class_scope(self, env, httpd, nghttpx):
44        if env.have_h3():
45            nghttpx.start_if_needed()
46        httpd.clear_extra_configs()
47        httpd.reload()
48
49    # download 1 file, check that delayed response works in general
50    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
51    def test_04_01_download_1(self, env: Env, httpd, nghttpx, repeat,
52                              proto):
53        if proto == 'h3' and not env.have_h3():
54            pytest.skip("h3 not supported")
55        count = 1
56        curl = CurlClient(env=env)
57        urln = f'https://{env.authority_for(env.domain1, proto)}' \
58               f'/curltest/tweak?id=[0-{count - 1}]'\
59               '&chunks=100&chunk_size=100&chunk_delay=10ms'
60        r = curl.http_download(urls=[urln], alpn_proto=proto)
61        r.check_response(count=1, http_status=200)
62
63    # download 50 files in 100 chunks a 100 bytes with 10ms delay between
64    # prepend 100 file requests to warm up connection processing limits
65    # (Apache2 increases # of parallel processed requests after successes)
66    @pytest.mark.parametrize("proto", ['h2', 'h3'])
67    def test_04_02_100_100_10(self, env: Env,
68                                httpd, nghttpx, repeat, proto):
69        if proto == 'h3' and not env.have_h3():
70            pytest.skip("h3 not supported")
71        count = 50
72        warmups = 100
73        curl = CurlClient(env=env)
74        url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]'
75        urln = f'https://{env.authority_for(env.domain1, proto)}' \
76               f'/curltest/tweak?id=[0-{count-1}]'\
77               '&chunks=100&chunk_size=100&chunk_delay=10ms'
78        r = curl.http_download(urls=[url1, urln], alpn_proto=proto,
79                               extra_args=['--parallel'])
80        r.check_response(count=warmups+count, http_status=200)
81        assert r.total_connects == 1
82        t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total')
83        if t_max < (5 * t_min) and t_min < 2:
84            log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]')
85
86    # download 50 files in 1000 chunks a 10 bytes with 1ms delay between
87    # prepend 100 file requests to warm up connection processing limits
88    # (Apache2 increases # of parallel processed requests after successes)
89    @pytest.mark.parametrize("proto", ['h2', 'h3'])
90    def test_04_03_1000_10_1(self, env: Env, httpd, nghttpx, repeat, proto):
91        if proto == 'h3' and not env.have_h3():
92            pytest.skip("h3 not supported")
93        count = 50
94        warmups = 100
95        curl = CurlClient(env=env)
96        url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]'
97        urln = f'https://{env.authority_for(env.domain1, proto)}' \
98               f'/curltest/tweak?id=[0-{count - 1}]'\
99               '&chunks=1000&chunk_size=10&chunk_delay=100us'
100        r = curl.http_download(urls=[url1, urln], alpn_proto=proto,
101                               extra_args=['--parallel'])
102        r.check_response(count=warmups+count, http_status=200)
103        assert r.total_connects == 1
104        t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total')
105        if t_max < (5 * t_min):
106            log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]')
107
108    # download 50 files in 10000 chunks a 1 byte with 10us delay between
109    # prepend 100 file requests to warm up connection processing limits
110    # (Apache2 increases # of parallel processed requests after successes)
111    @pytest.mark.parametrize("proto", ['h2', 'h3'])
112    def test_04_04_1000_10_1(self, env: Env, httpd, nghttpx, repeat, proto):
113        if proto == 'h3' and not env.have_h3():
114            pytest.skip("h3 not supported")
115        count = 50
116        warmups = 100
117        curl = CurlClient(env=env)
118        url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]'
119        urln = f'https://{env.authority_for(env.domain1, proto)}' \
120               f'/curltest/tweak?id=[0-{count - 1}]'\
121               '&chunks=10000&chunk_size=1&chunk_delay=50us'
122        r = curl.http_download(urls=[url1, urln], alpn_proto=proto,
123                               extra_args=['--parallel'])
124        r.check_response(count=warmups+count, http_status=200)
125        assert r.total_connects == 1
126        t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total')
127        if t_max < (5 * t_min):
128            log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]')
129
130    def stats_spread(self, stats: List[Dict], key: str) -> Tuple[float, int, float, int, float]:
131        stotals = 0.0
132        s_min = 100.0
133        i_min = -1
134        s_max = 0.0
135        i_max = -1
136        for idx, s in enumerate(stats):
137            val = float(s[key])
138            stotals += val
139            if val > s_max:
140                s_max = val
141                i_max = idx
142            if val < s_min:
143                s_min = val
144                i_min = idx
145        return stotals/len(stats), i_min, s_min, i_max, s_max
146