From 8492d0c1e55b8b7d728b71fa76b9d35698540352 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sat, 11 Jun 2016 18:35:46 +0200 Subject: [PATCH] Measure time for different stages --- powerProfile.py | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/powerProfile.py b/powerProfile.py index df77317..0133e6e 100644 --- a/powerProfile.py +++ b/powerProfile.py @@ -4,6 +4,7 @@ Created on 11. 06. 2016 @author: wn ''' +from __future__ import print_function import xlrd import xlwt @@ -11,6 +12,8 @@ from xlutils.copy import copy import pymongo from bson.son import SON import datetime +import time + DBHOST = '172.16.2.17' DBNAME = 'iot' @@ -43,6 +46,19 @@ PIPELINE = [ ] +class Stopwatch(): + def millis(self): + return time.time() * 1000 + def __init__(self, msg): + self.msg = msg + def __enter__(self): + self.start = self.millis() + print("{} ... ".format(self.msg), end="") + def __exit__(self, type, value, traceback): + duration = self.millis() - self.start + print("{}".format(duration)) + + class DataAdder(object): def __init__(self, dbhost, dbname, dbcoll, filename, pipeline): self.dbhost = dbhost @@ -71,23 +87,21 @@ class DataAdder(object): return cursor def run(self): - print("stage 1") - self.openExcel() - print("stage 2") - cursor = self.collectData() - print("stage 3") - for rownum, row in enumerate(cursor): - print(rownum) - if (rownum == 0): - for pos, key in enumerate(row.iterkeys()): - self.sheetW.write(rownum, pos, key) - else: - for pos, value in enumerate(row.itervalues()): - if (pos == 0): - v = str(value) - else: - v = value - self.sheetW.write(rownum, pos, v) + with Stopwatch("stage 1: collecting data"): + cursor = self.collectData() + with Stopwatch("stage 2: opening excel"): + self.openExcel() + with Stopwatch("stage 3: writing excel"): + for rownum, row in enumerate(cursor): + if (rownum == 0): + for pos, key in enumerate(row.iterkeys()): + self.sheetW.write(rownum, pos, key) + else: + for pos, value in enumerate(row.itervalues()): + if (type(value) not in [int, float]): + value = str(value) + self.sheetW.write(rownum, pos, value) + with Stopwatch("stage 4: saving excel"): self.excelW.save(self.filename)