def stats(self): cpu_total = 0.0 cpu_system = 0.0 cpu_percent = 0.0 for x in self.d.stats(self.container_id, decode=True, stream=True): blk_read, blk_write = calculate_blkio_bytes(x) net_r, net_w = calculate_network_bytes(x) mem_current = x["memory_stats"]["usage"] mem_total = x["memory_stats"]["limit"] try: cpu_percent, cpu_system, cpu_total = calculate_cpu_percent2( x, cpu_total, cpu_system) except KeyError as e: logger.error( "error while getting new CPU stats: %r, falling back") cpu_percent = calculate_cpu_percent(x) r = { "cpu_percent": cpu_percent, "mem_current": mem_current, "mem_total": x["memory_stats"]["limit"], "mem_percent": (mem_current / mem_total) * 100.0, "blk_read": blk_read, "blk_write": blk_write, "net_rx": net_r, "net_tx": net_w, } yield r
def test_stats(): mock() b = DockerBackend() c = b.get_containers() c0 = c.response.pop() operation = c0.stats() stats_stream = operation.response assert next(stats_stream) for x in c0.d.stats('x', decode=True, stream=True): calculate_cpu_percent(x) t = 0.0 s = 0.0 for x in c0.d.stats('x', decode=True, stream=True): calculate_cpu_percent(x) _, s, t = calculate_cpu_percent2(x, t, s)
def stats(self): for x in self.d.stats(self.container_id, decode=True, stream=True): blk_read, blk_write = calculate_blkio_bytes(x) net_r, net_w = calculate_network_bytes(x) mem_current = x["memory_stats"]["usage"] mem_total = x["memory_stats"]["limit"] r = { "cpu_percent": calculate_cpu_percent(x), "mem_current": mem_current, "mem_total": x["memory_stats"]["limit"], "mem_percent": (mem_current / mem_total) * 100.0, "blk_read": blk_read, "blk_write": blk_write, "net_rx": net_r, "net_tx": net_w, } yield r