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, proto): 51 if proto == 'h3' and not env.have_h3(): 52 pytest.skip("h3 not supported") 53 count = 1 54 curl = CurlClient(env=env) 55 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 56 f'/curltest/tweak?id=[0-{count - 1}]'\ 57 '&chunks=100&chunk_size=100&chunk_delay=10ms' 58 r = curl.http_download(urls=[urln], alpn_proto=proto) 59 r.check_response(count=1, http_status=200) 60 61 # download 50 files in 100 chunks a 100 bytes with 10ms delay between 62 # prepend 100 file requests to warm up connection processing limits 63 # (Apache2 increases # of parallel processed requests after successes) 64 @pytest.mark.parametrize("proto", ['h2', 'h3']) 65 def test_04_02_100_100_10(self, env: Env, httpd, nghttpx, proto): 66 if proto == 'h3' and not env.have_h3(): 67 pytest.skip("h3 not supported") 68 count = 50 69 warmups = 100 70 curl = CurlClient(env=env) 71 url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]' 72 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 73 f'/curltest/tweak?id=[0-{count-1}]'\ 74 '&chunks=100&chunk_size=100&chunk_delay=10ms' 75 r = curl.http_download(urls=[url1, urln], alpn_proto=proto, 76 extra_args=['--parallel']) 77 r.check_response(count=warmups+count, http_status=200) 78 assert r.total_connects == 1 79 t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total') 80 if t_max < (5 * t_min) and t_min < 2: 81 log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]') 82 83 # download 50 files in 1000 chunks a 10 bytes with 1ms delay between 84 # prepend 100 file requests to warm up connection processing limits 85 # (Apache2 increases # of parallel processed requests after successes) 86 @pytest.mark.parametrize("proto", ['h2', 'h3']) 87 def test_04_03_1000_10_1(self, env: Env, httpd, nghttpx, proto): 88 if proto == 'h3' and not env.have_h3(): 89 pytest.skip("h3 not supported") 90 count = 50 91 warmups = 100 92 curl = CurlClient(env=env) 93 url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]' 94 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 95 f'/curltest/tweak?id=[0-{count - 1}]'\ 96 '&chunks=1000&chunk_size=10&chunk_delay=100us' 97 r = curl.http_download(urls=[url1, urln], alpn_proto=proto, 98 extra_args=['--parallel']) 99 r.check_response(count=warmups+count, http_status=200) 100 assert r.total_connects == 1 101 t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total') 102 if t_max < (5 * t_min): 103 log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]') 104 105 # download 50 files in 10000 chunks a 1 byte with 10us delay between 106 # prepend 100 file requests to warm up connection processing limits 107 # (Apache2 increases # of parallel processed requests after successes) 108 @pytest.mark.parametrize("proto", ['h2', 'h3']) 109 def test_04_04_1000_10_1(self, env: Env, httpd, nghttpx, proto): 110 if proto == 'h3' and not env.have_h3(): 111 pytest.skip("h3 not supported") 112 count = 50 113 warmups = 100 114 curl = CurlClient(env=env) 115 url1 = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{warmups-1}]' 116 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 117 f'/curltest/tweak?id=[0-{count - 1}]'\ 118 '&chunks=10000&chunk_size=1&chunk_delay=50us' 119 r = curl.http_download(urls=[url1, urln], alpn_proto=proto, 120 extra_args=['--parallel']) 121 r.check_response(count=warmups+count, http_status=200) 122 assert r.total_connects == 1 123 t_avg, i_min, t_min, i_max, t_max = self.stats_spread(r.stats[warmups:], 'time_total') 124 if t_max < (5 * t_min): 125 log.warning(f'avg time of transfer: {t_avg} [{i_min}={t_min}, {i_max}={t_max}]') 126 127 def stats_spread(self, stats: List[Dict], key: str) -> Tuple[float, int, float, int, float]: 128 stotals = 0.0 129 s_min = 100.0 130 i_min = -1 131 s_max = 0.0 132 i_max = -1 133 for idx, s in enumerate(stats): 134 val = float(s[key]) 135 stotals += val 136 if val > s_max: 137 s_max = val 138 i_max = idx 139 if val < s_min: 140 s_min = val 141 i_min = idx 142 return stotals/len(stats), i_min, s_min, i_max, s_max 143