xref: /curl/tests/http/test_04_stuttered.py (revision 0f7ba5c5)
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
28from typing import Tuple, List, Dict
29import pytest
30
31from testenv import Env, CurlClient
32
33
34log = logging.getLogger(__name__)
35
36
37@pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
38@pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
39class TestStuttered:
40
41    @pytest.fixture(autouse=True, scope='class')
42    def _class_scope(self, env, httpd, nghttpx):
43        if env.have_h3():
44            nghttpx.start_if_needed()
45        httpd.clear_extra_configs()
46        httpd.reload()
47
48    # download 1 file, check that delayed response works in general
49    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
50    def test_04_01_download_1(self, env: Env, httpd, nghttpx, repeat,
51                              proto):
52        if proto == 'h3' and not env.have_h3():
53            pytest.skip("h3 not supported")
54        count = 1
55        curl = CurlClient(env=env)
56        urln = f'https://{env.authority_for(env.domain1, proto)}' \
57               f'/curltest/tweak?id=[0-{count - 1}]'\
58               '&chunks=100&chunk_size=100&chunk_delay=10ms'
59        r = curl.http_download(urls=[urln], alpn_proto=proto)
60        r.check_response(count=1, http_status=200)
61
62    # download 50 files in 100 chunks a 100 bytes with 10ms delay between
63    # prepend 100 file requests to warm up connection processing limits
64    # (Apache2 increases # of parallel processed requests after successes)
65    @pytest.mark.parametrize("proto", ['h2', 'h3'])
66    def test_04_02_100_100_10(self, env: Env,
67                                httpd, nghttpx, repeat, proto):
68        if proto == 'h3' and not env.have_h3():
69            pytest.skip("h3 not supported")
70        count = 50
71        warmups = 100
72        curl = CurlClient(env=env)
73        url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]'
74        urln = f'https://{env.authority_for(env.domain1, proto)}' \
75               f'/curltest/tweak?id=[0-{count-1}]'\
76               '&chunks=100&chunk_size=100&chunk_delay=10ms'
77        r = curl.http_download(urls=[url1, urln], alpn_proto=proto,
78                               extra_args=['--parallel'])
79        r.check_response(count=warmups+count, http_status=200)
80        assert r.total_connects == 1
81        t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total')
82        if t_max < (5 * t_min) and t_min < 2:
83            log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]')
84
85    # download 50 files in 1000 chunks a 10 bytes with 1ms delay between
86    # prepend 100 file requests to warm up connection processing limits
87    # (Apache2 increases # of parallel processed requests after successes)
88    @pytest.mark.parametrize("proto", ['h2', 'h3'])
89    def test_04_03_1000_10_1(self, env: Env, httpd, nghttpx, repeat, proto):
90        if proto == 'h3' and not env.have_h3():
91            pytest.skip("h3 not supported")
92        count = 50
93        warmups = 100
94        curl = CurlClient(env=env)
95        url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]'
96        urln = f'https://{env.authority_for(env.domain1, proto)}' \
97               f'/curltest/tweak?id=[0-{count - 1}]'\
98               '&chunks=1000&chunk_size=10&chunk_delay=100us'
99        r = curl.http_download(urls=[url1, urln], alpn_proto=proto,
100                               extra_args=['--parallel'])
101        r.check_response(count=warmups+count, http_status=200)
102        assert r.total_connects == 1
103        t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total')
104        if t_max < (5 * t_min):
105            log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]')
106
107    # download 50 files in 10000 chunks a 1 byte with 10us delay between
108    # prepend 100 file requests to warm up connection processing limits
109    # (Apache2 increases # of parallel processed requests after successes)
110    @pytest.mark.parametrize("proto", ['h2', 'h3'])
111    def test_04_04_1000_10_1(self, env: Env, httpd, nghttpx, repeat, proto):
112        if proto == 'h3' and not env.have_h3():
113            pytest.skip("h3 not supported")
114        count = 50
115        warmups = 100
116        curl = CurlClient(env=env)
117        url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]'
118        urln = f'https://{env.authority_for(env.domain1, proto)}' \
119               f'/curltest/tweak?id=[0-{count - 1}]'\
120               '&chunks=10000&chunk_size=1&chunk_delay=50us'
121        r = curl.http_download(urls=[url1, urln], alpn_proto=proto,
122                               extra_args=['--parallel'])
123        r.check_response(count=warmups+count, http_status=200)
124        assert r.total_connects == 1
125        t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total')
126        if t_max < (5 * t_min):
127            log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]')
128
129    def stats_spread(self, stats: List[Dict], key: str) -> Tuple[float, int, float, int, float]:
130        stotals = 0.0
131        s_min = 100.0
132        i_min = -1
133        s_max = 0.0
134        i_max = -1
135        for idx, s in enumerate(stats):
136            val = float(s[key])
137            stotals += val
138            if val > s_max:
139                s_max = val
140                i_max = idx
141            if val < s_min:
142                s_min = val
143                i_min = idx
144        return stotals/len(stats), i_min, s_min, i_max, s_max
145