Lines Matching defs:self

61     def __init__(self, pid: int, started_at: datetime, run_dir):
62 self._pid = pid
63 self._started_at = started_at
64 self._duration = timedelta(seconds=0)
65 self._run_dir = run_dir
66 self._samples = []
67 self._psu = None
68 self._stats = None
71 def duration(self) -> timedelta:
72 return self._duration
75 def stats(self) -> Optional[Dict[str,Any]]:
76 return self._stats
78 def sample(self):
79 elapsed = datetime.now() - self._started_at
81 if self._psu is None:
82 self._psu = psutil.Process(pid=self._pid)
83 mem = self._psu.memory_info()
84 self._samples.append({
86 'cpu': self._psu.cpu_percent(),
93 def finish(self):
94 self._duration = datetime.now() - self._started_at
95 if len(self._samples) > 0:
96 weights = [s['time'].total_seconds() for s in self._samples]
97 self._stats = {}
98 for key in self.STAT_KEYS:
99 self._stats[key] = fmean([s[key] for s in self._samples], weights)
101 self._stats = None
102 self._psu = None
104 def __repr__(self):
105 return f'RunProfile[pid={self._pid}, '\
106 f'duration={self.duration.total_seconds():.3f}s, '\
107 f'stats={self.stats}]'
112 def __init__(self, env, run_dir):
113 self._env = env
114 self._run_dir = run_dir
115 self._proc = None
116 self._stdoutfile = os.path.join(self._run_dir, 'tcpdump.out')
117 self._stderrfile = os.path.join(self._run_dir, 'tcpdump.err')
120 def stats(self) -> Optional[List[str]]:
121 if self._proc:
124 for l in open(self._stdoutfile).readlines():
129 def stats_excluding(self, src_port) -> Optional[List[str]]:
130 if self._proc:
133 for l in self.stats:
139 def stderr(self) -> List[str]:
140 if self._proc:
143 return open(self._stderrfile).readlines()
145 def sample(self):
149 tcpdump = self._env.tcpdump()
161 with open(self._stdoutfile, 'w') as cout:
162 with open(self._stderrfile, 'w') as cerr:
163 self._proc = subprocess.Popen(args, stdout=cout, stderr=cerr,
164 text=True, cwd=self._run_dir,
166 assert self._proc
167 assert self._proc.returncode is None
168 while self._proc:
170 self._proc.wait(timeout=1)
176 def start(self):
178 self.sample()
182 def finish(self):
183 if self._proc:
185 self._proc.terminate()
186 self._proc = None
191 def __init__(self, args: List[str], exit_code: int,
198 self._args = args
199 self._exit_code = exit_code
200 self._exception = exception
201 self._stdout = stdout
202 self._stderr = stderr
203 self._profile = profile
204 self._tcpdump = tcpdump
205 self._duration = duration if duration is not None else timedelta()
206 self._response = None
207 self._responses = []
208 self._results = {}
209 self._assets = []
210 self._stats = []
211 self._json_out = None
212 self._with_stats = with_stats
214 self._parse_stats()
218 out = ''.join(self._stdout)
219 self._json_out = json.loads(out)
223 def __repr__(self):
224 return f"ExecResult[code={self.exit_code}, exception={self._exception}, "\
225 f"args={self._args}, stdout={self._stdout}, stderr={self._stderr}]"
227 def _parse_stats(self):
228 self._stats = []
229 for l in self._stdout:
231 self._stats.append(json.loads(l))
237 def exit_code(self) -> int:
238 return self._exit_code
241 def args(self) -> List[str]:
242 return self._args
245 def outraw(self) -> bytes:
246 return ''.join(self._stdout).encode()
249 def stdout(self) -> str:
250 return ''.join(self._stdout)
253 def json(self) -> Optional[Dict]:
255 return self._json_out
258 def stderr(self) -> str:
259 return ''.join(self._stderr)
262 def trace_lines(self) -> List[str]:
263 return self._stderr
266 def duration(self) -> timedelta:
267 return self._duration
270 def profile(self) -> Optional[RunProfile]:
271 return self._profile
274 def tcpdump(self) -> Optional[RunTcpDump]:
275 return self._tcpdump
278 def response(self) -> Optional[Dict]:
279 return self._response
282 def responses(self) -> List[Dict]:
283 return self._responses
286 def results(self) -> Dict:
287 return self._results
290 def assets(self) -> List:
291 return self._assets
294 def with_stats(self) -> bool:
295 return self._with_stats
298 def stats(self) -> List:
299 return self._stats
302 def total_connects(self) -> Optional[int]:
303 if len(self.stats):
305 for stat in self.stats:
310 def add_response(self, resp: Dict):
311 self._response = resp
312 self._responses.append(resp)
314 def add_results(self, results: Dict):
315 self._results.update(results)
317 self.add_response(results['response'])
319 def add_assets(self, assets: List):
320 self._assets.extend(assets)
322 def check_exit_code(self, code: Union[int, bool]):
324 assert self.exit_code == 0, f'expected exit code {code}, '\
325 f'got {self.exit_code}\n{self.dump_logs()}'
327 assert self.exit_code != 0, f'expected exit code {code}, '\
328 f'got {self.exit_code}\n{self.dump_logs()}'
330 assert self.exit_code == code, f'expected exit code {code}, '\
331 f'got {self.exit_code}\n{self.dump_logs()}'
333 def check_response(self, http_status: Optional[int] = 200,
339 self.check_exit_code(exitcode)
340 if self.with_stats and isinstance(exitcode, int):
341 for idx, x in enumerate(self.stats):
345 f'got {x["exitcode"]}\n{self.dump_logs()}'
347 if self.with_stats:
348 assert len(self.stats) == count, \
350 f'got {len(self.stats)}\n{self.dump_logs()}'
352 assert len(self.responses) == count, \
354 f'got {len(self.responses)}\n{self.dump_logs()}'
356 if self.with_stats:
357 for idx, x in enumerate(self.stats):
359 f'response #{idx} reports no http_code\n{self.dump_stat(x)}'
362 f'got {x["http_code"]}\n{self.dump_stat(x)}'
364 for idx, x in enumerate(self.responses):
367 f'got {x["status"]}\n{self.dump_stat(x)}'
369 if self.with_stats:
378 for idx, x in enumerate(self.stats):
381 f'got version {x["http_version"]}\n{self.dump_stat(x)}'
383 for idx, x in enumerate(self.responses):
386 f'got {x["protocol"]}\n{self.dump_logs()}'
388 assert self.total_connects == connect_count, \
389 f'expected {connect_count}, but {self.total_connects} '\
390 f'were made\n{self.dump_logs()}'
392 def check_stats(self, count: int, http_status: Optional[int] = None,
397 self.check_exit_code(0)
398 assert len(self.stats) == count, \
399 f'stats count: expected {count}, got {len(self.stats)}\n{self.dump_logs()}'
401 for idx, x in enumerate(self.stats):
403 f'status #{idx} reports no http_code\n{self.dump_stat(x)}'
406 f'got {x["http_code"]}\n{self.dump_stat(x)}'
408 for idx, x in enumerate(self.stats):
412 f'got {x["exitcode"]}\n{self.dump_stat(x)}'
414 for idx, x in enumerate(self.stats):
415 assert 'remote_port' in x, f'remote_port missing\n{self.dump_stat(x)}'
418 f'got {x["remote_port"]}\n{self.dump_stat(x)}'
420 for idx, x in enumerate(self.stats):
421 assert 'remote_ip' in x, f'remote_ip missing\n{self.dump_stat(x)}'
424 f'got {x["remote_ip"]}\n{self.dump_stat(x)}'
426 def dump_logs(self):
428 lines.extend(self._stdout)
430 lines.extend(self._stderr)
434 def dump_stat(self, x):
442 lines.extend(self.xfer_trace_for(xfer_id))
445 lines.extend(self._stderr)
449 def xfer_trace_for(self, xfer_id) -> List[str]:
451 return [line for line in self._stderr if pat.match(line)]
465 def __init__(self, env: Env,
470 self.env = env
471 self._timeout = timeout if timeout else env.test_timeout
472 self._curl = os.environ['CURL'] if 'CURL' in os.environ else env.curl
473 self._run_dir = run_dir if run_dir else os.path.join(env.gen_dir, 'curl')
474 self._stdoutfile = f'{self._run_dir}/curl.stdout'
475 self._stderrfile = f'{self._run_dir}/curl.stderr'
476 self._headerfile = f'{self._run_dir}/curl.headers'
477 self._log_path = f'{self._run_dir}/curl.log'
478 self._silent = silent
479 self._run_env = run_env
480 self._rmrf(self._run_dir)
481 self._mkpath(self._run_dir)
484 def run_dir(self) -> str:
485 return self._run_dir
487 def download_file(self, i: int) -> str:
488 return os.path.join(self.run_dir, f'download_{i}.data')
490 def _rmf(self, path):
494 def _rmrf(self, path):
498 def _mkpath(self, path):
502 def get_proxy_args(self, proto: str = 'http/1.1',
505 proxy_name = '127.0.0.1' if use_ip else self.env.proxy_domain
507 pport = self.env.pts_port(proto) if tunnel else self.env.proxys_port
511 '--proxy-cacert', self.env.ca.cert_file,
517 '--proxy', f'http://{proxy_name}:{self.env.proxy_port}/',
518 '--resolve', f'{proxy_name}:{self.env.proxy_port}:127.0.0.1',
524 def http_get(self, url: str, extra_args: Optional[List[str]] = None,
530 return self._raw(url, options=extra_args,
537 def http_download(self, urls: List[str],
557 self._rmf(self.download_file(i))
562 return self._raw(urls, alpn_proto=alpn_proto, options=extra_args,
568 def http_upload(self, urls: List[str], data: str,
584 return self._raw(urls, alpn_proto=alpn_proto, options=extra_args,
590 def http_delete(self, urls: List[str],
604 return self._raw(urls, alpn_proto=alpn_proto, options=extra_args,
609 def http_put(self, urls: List[str], data=None, fdata=None,
628 return self._raw(urls, intext=data,
634 def http_form(self, urls: List[str], form: Dict[str, str],
650 return self._raw(urls, alpn_proto=alpn_proto, options=extra_args,
654 def ftp_get(self, urls: List[str],
672 self._rmf(self.download_file(i))
677 return self._raw(urls, options=extra_args,
683 def ftp_ssl_get(self, urls: List[str],
694 return self.ftp_get(urls=urls, with_stats=with_stats,
699 def ftp_upload(self, urls: List[str], fupload,
713 return self._raw(urls, options=extra_args,
719 def ftp_ssl_upload(self, urls: List[str], fupload,
729 return self.ftp_upload(urls=urls, fupload=fupload,
734 def response_file(self, idx: int):
735 return os.path.join(self._run_dir, f'download_{idx}.data')
737 def run_direct(self, args, with_stats: bool = False, with_profile: bool = False):
738 my_args = [self._curl]
747 return self._run(args=my_args, with_stats=with_stats, with_profile=with_profile)
749 def _run(self, args, intext='', with_stats: bool = False,
751 self._rmf(self._stdoutfile)
752 self._rmf(self._stderrfile)
753 self._rmf(self._headerfile)
759 tcpdump = RunTcpDump(self.env, self._run_dir)
762 with open(self._stdoutfile, 'w') as cout:
763 with open(self._stderrfile, 'w') as cerr:
765 end_at = started_at + timedelta(seconds=self._timeout) \
766 if self._timeout else None
769 cwd=self._run_dir, shell=False,
770 env=self._run_env)
771 profile = RunProfile(p.pid, started_at, self._run_dir)
782 raise subprocess.TimeoutExpired(cmd=args, timeout=self._timeout)
790 cwd=self._run_dir, shell=False,
792 timeout=self._timeout,
793 env=self._run_env)
799 f'(configured {self._timeout}s): {args}')
804 coutput = open(self._stdoutfile).readlines()
805 cerrput = open(self._stderrfile).readlines()
812 def _raw(self, urls, intext='', timeout=None, options=None, insecure=False,
820 args = self._complete_args(
824 r = self._run(args, intext=intext, with_stats=with_stats,
827 self._parse_headerfile(self._headerfile, r=r)
832 def _complete_args(self, urls, timeout=None, options=None,
840 args = [self._curl, "-s", "--path-as-is"]
845 args.extend(["-D", self._headerfile])
846 if def_tracing is not False and not self._silent:
848 if self.env.verbose > 1:
861 if alpn_proto not in self.ALPN_ARG:
863 args.append(self.ALPN_ARG[alpn_proto])
872 args.extend(["--cacert", self.env.ca.cert_file])
883 def _parse_headerfile(self, headerfile: str, r: ExecResult = None) -> ExecResult: