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