-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
import subprocess | ||
import signal | ||
import time | ||
from sys import platform | ||
|
||
import psutil | ||
|
||
from .port_generator import get_port | ||
|
||
|
||
class Statshouse: | ||
def __init__(self, engine_name, working_dir): | ||
self._working_dir = working_dir | ||
self._port = get_port() | ||
self._statshouse_proc = None | ||
self._statshouse_file = os.path.join(working_dir, engine_name + ".statshouse") | ||
self._statshouse_file_write_fd = None | ||
self._statshouse_file_read_fd = None | ||
self._stats = {} | ||
|
||
@property | ||
def port(self): | ||
return self._port | ||
|
||
@property | ||
def stats(self): | ||
return self._stats | ||
|
||
def start(self): | ||
print("\nStarting statshouse server on port {}".format(self._port)) | ||
self._statshouse_file_write_fd = open(self._statshouse_file, 'wb') | ||
self._statshouse_file_read_fd = open(self._statshouse_file, 'r') | ||
self._statshouse_proc = psutil.Popen( | ||
["nc", "-lu", "" if platform == "darwin" else "-p", str(self._port)], | ||
stdout=self._statshouse_file_write_fd, | ||
stderr=subprocess.STDOUT, | ||
cwd=self._working_dir | ||
) | ||
if not self._statshouse_proc.is_running(): | ||
self._statshouse_file_write_fd.close() | ||
possible_error = self._statshouse_file_read_fd.read() or "empty out" | ||
self._statshouse_file_read_fd.close() | ||
RuntimeError("Can't start statshouse server: " + possible_error) | ||
|
||
def stop(self): | ||
if self._statshouse_proc is None or not self._statshouse_proc.is_running(): | ||
return | ||
|
||
print("\nStopping statshouse server on port {}".format(self._port)) | ||
self._statshouse_proc.send_signal(signal.SIGKILL) | ||
os.waitpid(self._statshouse_proc.pid, 0) | ||
self._statshouse_file_read_fd.close() | ||
self._statshouse_file_write_fd.close() | ||
|
||
def wait_metrics(self, timeout=10): | ||
start = time.time() | ||
while not self.try_update_stats(): | ||
if time.time() - start > timeout: | ||
raise RuntimeError("Waiting next statshouse metrics timeout") | ||
time.sleep(0.1) | ||
|
||
def try_update_stats(self): | ||
self._statshouse_file_read_fd.seek(0, os.SEEK_END) | ||
return bool(self._statshouse_file_read_fd.tell()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from multiprocessing.dummy import Pool as ThreadPool | ||
from python.lib.testcase import KphpServerAutoTestCase | ||
|
||
|
||
class TestStatshouseMetrics(KphpServerAutoTestCase): | ||
WORKERS_NUM = 2 | ||
|
||
@classmethod | ||
def extra_class_setup(cls): | ||
cls.kphp_server.update_options({ | ||
"--workers-num": cls.WORKERS_NUM, | ||
"--statshouse-client": ":" + str(cls.kphp_server._statshouse.port), | ||
}) | ||
|
||
def test_sending_metrics(self): | ||
self.kphp_server.stop() | ||
self.kphp_server.start(None, True) | ||
self.kphp_server.check_statshouse_metrics() |