Measure time for different stages
This commit is contained in:
parent
d2a256957f
commit
8492d0c1e5
@ -4,6 +4,7 @@ Created on 11. 06. 2016
|
|||||||
@author: wn
|
@author: wn
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
import xlrd
|
import xlrd
|
||||||
import xlwt
|
import xlwt
|
||||||
@ -11,6 +12,8 @@ from xlutils.copy import copy
|
|||||||
import pymongo
|
import pymongo
|
||||||
from bson.son import SON
|
from bson.son import SON
|
||||||
import datetime
|
import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
DBHOST = '172.16.2.17'
|
DBHOST = '172.16.2.17'
|
||||||
DBNAME = 'iot'
|
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):
|
class DataAdder(object):
|
||||||
def __init__(self, dbhost, dbname, dbcoll, filename, pipeline):
|
def __init__(self, dbhost, dbname, dbcoll, filename, pipeline):
|
||||||
self.dbhost = dbhost
|
self.dbhost = dbhost
|
||||||
@ -71,23 +87,21 @@ class DataAdder(object):
|
|||||||
return cursor
|
return cursor
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
print("stage 1")
|
with Stopwatch("stage 1: collecting data"):
|
||||||
self.openExcel()
|
|
||||||
print("stage 2")
|
|
||||||
cursor = self.collectData()
|
cursor = self.collectData()
|
||||||
print("stage 3")
|
with Stopwatch("stage 2: opening excel"):
|
||||||
|
self.openExcel()
|
||||||
|
with Stopwatch("stage 3: writing excel"):
|
||||||
for rownum, row in enumerate(cursor):
|
for rownum, row in enumerate(cursor):
|
||||||
print(rownum)
|
|
||||||
if (rownum == 0):
|
if (rownum == 0):
|
||||||
for pos, key in enumerate(row.iterkeys()):
|
for pos, key in enumerate(row.iterkeys()):
|
||||||
self.sheetW.write(rownum, pos, key)
|
self.sheetW.write(rownum, pos, key)
|
||||||
else:
|
else:
|
||||||
for pos, value in enumerate(row.itervalues()):
|
for pos, value in enumerate(row.itervalues()):
|
||||||
if (pos == 0):
|
if (type(value) not in [int, float]):
|
||||||
v = str(value)
|
value = str(value)
|
||||||
else:
|
self.sheetW.write(rownum, pos, value)
|
||||||
v = value
|
with Stopwatch("stage 4: saving excel"):
|
||||||
self.sheetW.write(rownum, pos, v)
|
|
||||||
self.excelW.save(self.filename)
|
self.excelW.save(self.filename)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user