예제 #1
0
def test_time_units():
    assert 1000 == AustinArgumentParser().parse_args(["-i", "1ms",
                                                      "python3"]).interval
    assert 1000 == AustinArgumentParser().parse_args(["-t", "1s",
                                                      "python3"]).timeout
    assert 2 == AustinArgumentParser().parse_args(["-x", "2s",
                                                   "python3"]).exposure
    with raises(AustinCommandLineError):
        AustinArgumentParser().parse_args(["-x", "2ls", "python3"]).exposure
    with raises(AustinCommandLineError):
        AustinArgumentParser().parse_args(["-x", "2l", "python3"]).exposure
예제 #2
0
    async def start(self, args: List[str] = None) -> None:
        """Create the start coroutine.

        Use with the ``asyncio`` event loop.
        """
        try:
            _args = list(args or sys.argv[1:])
            _args.insert(0, "-P")
            self.proc = await asyncio.create_subprocess_exec(
                self.binary_path,
                *_args,
                stdin=asyncio.subprocess.PIPE,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE,
            )
        except FileNotFoundError:
            raise AustinError("Austin executable not found.") from None

        if not self.proc.stdout:
            raise AustinError("Standard output stream is unexpectedly missing")
        if not self.proc.stderr:
            raise AustinError("Standard error stream is unexpectedly missing")

        self._running = True

        try:
            if not await self._read_meta():
                raise AustinError("Austin did not start properly")

            self.check_version()

            self._ready_callback(*self._get_process_info(
                AustinArgumentParser().parse_args(args), self.proc.pid))

            # Start readline loop
            while self._running:
                data = (await self.proc.stdout.readline()).rstrip()
                if not data:
                    break

                self.submit_sample(data)

            self._terminate_callback(await self._read_meta())
            self.check_exit(await self.proc.wait(), await self._read_stderr())

        except Exception:
            try:
                self.proc.terminate()
                await self.proc.wait()
            except Exception:
                # best effort
                pass
            raise

        finally:
            self._running = False
예제 #3
0
def test_args_list():

    args = Bunch()
    args.alt_format = True
    args.children = True
    args.exclude_empty = True
    args.full = True
    args.interval = 1000
    args.memory = True
    args.pid = 42
    args.sleepless = True
    args.timeout = 50
    args.command = ["python3", "somescript.py"]

    args.foo = "bar"

    assert AustinArgumentParser.to_list(args) == [
        "-a",
        "-C",
        "-e",
        "-f",
        "-i",
        "1000",
        "-m",
        "-p",
        "42",
        "-s",
        "-t",
        "50",
        "python3",
        "somescript.py",
    ]

    assert AustinArgumentParser.to_list(AustinArgumentParser().parse_args(
        ["-i", "1ms", "python"])) == [
            "-i",
            "1000",
            "python",
        ]
예제 #4
0
    def start(self, args: List[str] = None) -> None:
        """Start the Austin process."""
        try:
            self.proc = subprocess.Popen(
                [self.binary_path] + ["-P"] + (args or sys.argv[1:]),
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
        except FileNotFoundError:
            raise AustinError("Austin executable not found.") from None

        if not self.proc.stdout:
            raise AustinError("Standard output stream is unexpectedly missing")
        if not self.proc.stderr:
            raise AustinError("Standard error stream is unexpectedly missing")

        try:
            if not self._read_meta():
                raise AustinError("Austin did not start properly")

            self.check_version()

            self._ready_callback(*self._get_process_info(
                AustinArgumentParser().parse_args(args), self.proc.pid))

            while self.is_running():
                data = self.proc.stdout.readline().rstrip()
                if not data:
                    break

                self.submit_sample(data)

            self._terminate_callback(self._read_meta())
            try:
                stderr = self.proc.communicate(timeout=1)[1].decode().rstrip()
            except subprocess.TimeoutExpired:
                stderr = ""
            self.check_exit(self.proc.wait(), stderr)

        except Exception:
            self.proc.terminate()
            self.proc.wait()
            raise

        finally:
            self._running = False
예제 #5
0
def test_pid_only():
    args = AustinArgumentParser().parse_args(["-i", "1000", "-p", "1086"])

    assert args.pid == 1086
예제 #6
0
def test_exposure():
    assert 2 == AustinArgumentParser().parse_args(["-x", "2",
                                                   "python3"]).exposure
예제 #7
0
def test_command_with_austin_args():
    args = AustinArgumentParser().parse_args(
        ["-i", "1000", "python3", "my_app.py", "-i", "100"])

    assert args.interval == 1000
    assert args.command == ["python3", "my_app.py", "-i", "100"]
예제 #8
0
def test_command_with_options_and_arguments():
    args = AustinArgumentParser().parse_args(
        ["-i", "1000", "python3", "my_app.py", "-c", 'print("Test")'])

    assert args.command == ["python3", "my_app.py", "-c", 'print("Test")']
예제 #9
0
def test_missing_command_and_pid():
    with raises(AustinCommandLineError):
        AustinArgumentParser().parse_args([])

    with raises(AustinCommandLineError):
        AustinArgumentParser(pid=False, command=False)