Last active
October 26, 2016 17:34
-
-
Save Mic92/d562353960056aba426a3f529dc6b044 to your computer and use it in GitHub Desktop.
Use docker + python for reproducible benchmarks
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
from docker import Client | |
class Container(): | |
def __init__(self, **kwargs): | |
self.api = Client() | |
self.container_args = kwargs | |
def stats(self): | |
s = next(self.api.stats(self.id)) | |
return json.loads(s.decode("utf-8")) | |
def __enter__(self): | |
self.id = self.api.create_container(**self.container_args).get("Id") | |
self.api.start(container=self.id) | |
data = self.api.inspect_container(self.id) | |
self.ip = data["NetworkSettings"]["IPAddress"] | |
return self | |
def __exit__(self, type, value, tb): | |
self.api.stop(container=self.id) | |
self.api.remove_container(container=self.id) | |
with Container(image='influxdb:alpine', ports=[8083, 8086]) as container: | |
before = container.stats() | |
# do work | |
after = container.stats() | |
# before and after contain ressource statistics of the container |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment