Last active
January 17, 2018 19:50
-
-
Save JekaMas/bd726dbc563864998d1df191b6976e1e to your computer and use it in GitHub Desktop.
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
import subprocess | |
import pandas | |
from io import StringIO | |
import time | |
class Container(object): | |
def __init__(self, name): | |
self.name = name | |
self.id = None | |
self.pid = None | |
@staticmethod | |
def sh(command, stdin=None, stdout=None, get_pipes=None, debug=False): | |
if debug: | |
print(command) | |
if stdout is None: | |
stdout = subprocess.PIPE | |
p = subprocess.Popen(command, universal_newlines=True, shell=True, stdin=stdin, stdout=stdout) | |
out, err = p.communicate() | |
if p.returncode != 0: | |
print("got an error %d %s. output %s" % (p.returncode, err, out)) | |
exit(1) | |
return | |
if get_pipes: | |
return out, p.stdin, p.stdout | |
return out.strip() | |
@staticmethod | |
def pipe(commands): | |
if not isinstance(commands, list): | |
return Container.sh(commands) | |
if len(commands) == 1: | |
return Container.sh(commands[0]) | |
return Container.sh(" | ".join(commands)) | |
def get_id(self): | |
if self.id is not None: | |
return self.id | |
docker_name_id_all = 'docker ps --format "{{.ID}} {{.Names}}"' | |
docker_name_id = 'grep {container_name}'.format(container_name=self.name) | |
docker_id = 'awk \'{print $1}\'' | |
self.id = Container.pipe([docker_name_id_all, docker_name_id, docker_id]) | |
return self.id | |
def get_pid(self): | |
if self.pid is not None: | |
return self.pid | |
self.pid = Container.sh('docker inspect -f \'{{{{ .State.Pid }}}}\' {container_id}'. | |
format(container_id=self.get_id())) | |
return self.pid | |
def get_net_stats(self): | |
pid_net_stats = 'cat /proc/{container_pid}/net/dev'.format(container_pid=self.get_pid()) | |
get_eth0 = 'grep eth0' | |
format_stats = 'awk \'{{sum = $2+$10;rec=$2;tra=$10}} END ' \ | |
'{{print \"Receive\\tTransmit\\tTotal\\n\" rec \"\\t\" tra \"\\t\" sum}}\'' | |
return Container.pipe([pid_net_stats, get_eth0, format_stats]) | |
def send_command(self, command): | |
p_stdin = 'cat /proc/{container_pid}/fd/0'.format(container_pid=self.get_pid()) | |
sent_stdin = 'echo "{command}" > {stdin}'.format(command=command, stdin=p_stdin) | |
Container.sh(sent_stdin) | |
def get_stats(n, fns): | |
base_levels = [] | |
stats_list = [] | |
for i in range(0, len(fns)): | |
base_level_str = StringIO(fns[i]()) | |
base_level = pandas.read_csv(base_level_str, sep="\t") | |
base_levels.append(base_level) | |
stats = pandas.DataFrame(columns=base_level.columns.values.tolist()) | |
stats_list.append(stats) | |
print(base_levels) | |
for i in range(0, n): | |
time.sleep(1) | |
for j in range(0, len(fns)): | |
data = fns[j]() | |
new_level = StringIO(data) | |
df = pandas.read_csv(new_level, sep="\t") | |
df = df.subtract(base_levels[j]) | |
stats_list[j] = stats_list[j].append(df) | |
for i in range(0, len(fns)): | |
stats_list[i] = stats_list[i].astype(int) | |
return stats_list | |
#measurement | |
# with whisper | |
geth_docker_name1 = "ethereum1" | |
geth1 = Container(name=geth_docker_name1) | |
# without whisper | |
geth_docker_name2 = "ethereum2" | |
geth2 = Container(name=geth_docker_name2) | |
print(geth2.get_pid()) | |
stats_list = get_stats(600, [geth1.get_net_stats, geth2.get_net_stats]) | |
print(geth_docker_name1) | |
print("Stats (bytes/s):\n", stats_list[0].describe(include='all').astype(int)) | |
print("\nTraffic sum(bytes):\n", stats_list[0].sum(axis=0)) | |
print() | |
print(geth_docker_name2) | |
print("Stats (bytes/s):\n", stats_list[1].describe(include='all').astype(int)) | |
print("\nTraffic sum(bytes):\n", stats_list[1].sum(axis=0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment