Commit 8f86e5e9 by John Eskew

Merge pull request #4 from doctoryes/doctoryes/other_delims

Support specified delimiters between timing levels.
parents efd19daa 9c7801cc
......@@ -40,7 +40,8 @@ class CodeBlockTimer(object):
self.block_desc = block_desc
self.verbose = False if 'verbose' not in kwargs else kwargs['verbose']
self.timer = default_timer
self.data_store = storage.TimingDataStorage(**kwargs)
self.delimiter = kwargs.pop('delimiter', ':')
self.data_store = TimingDataStorage(**kwargs)
def __enter__(self):
if len(_m.nest_stack) == 0:
......@@ -56,7 +57,7 @@ class CodeBlockTimer(object):
self.elapsed = self.elapsed_secs * 1000 # millisecs
# Store the timings.
nested_desc = ":".join(_m.nest_stack)
nested_desc = self.delimiter.join(_m.nest_stack)
self.data_store.store(_m.run_id, nested_desc, self.elapsed)
# Pop the stack.
......
......@@ -3,10 +3,13 @@ import mock
import os
import unittest
import random
import math
import sqlite3
from code_block_timer import CodeBlockTimer, code_block_timer, _m
import ddt
@ddt.ddt
class TestCodeBlockTimer(unittest.TestCase):
"""
Tests for CodeBlockTimer.
......@@ -29,10 +32,9 @@ class TestCodeBlockTimer(unittest.TestCase):
iterations = ['iter0', 'iter1', 'iter2', 'iter3']
with CodeBlockTimer("test", db_name=self.db_name) as timer:
run_id = _m.run_id
z = 0
for i, iter_name in enumerate(iterations):
with CodeBlockTimer(iter_name, db_name=self.db_name) as inner:
z += i
z = math.factorial(10)
self._verifyEvents(run_id, ['test', ] + ["test:{}".format(x) for x in iterations])
@mock.patch('code_block_timer.storage.TimingDataStorage')
......@@ -54,6 +56,32 @@ class TestCodeBlockTimer(unittest.TestCase):
self.assertTrue(test_dict['entered'])
store.store.assert_called_with(45, 'decorator_test', mock.ANY)
def test_exception_handled(self):
msg = "exception_but_still_timed"
try:
with CodeBlockTimer(msg, db_name=self.db_name) as __:
run_id = _m.run_id
z = math.factorial(10)
raise Exception
except Exception:
pass
self._verifyEvents(run_id, [msg])
def test_default_delimiter(self):
with CodeBlockTimer("test", db_name=self.db_name) as timer:
run_id = _m.run_id
with CodeBlockTimer("delimiter", db_name=self.db_name) as inner:
z = math.factorial(10)
self._verifyEvents(run_id, ['test', 'test:delimiter'])
@ddt.data(':::::', '%', '-', '/')
def test_delimiters(self, delimiter):
with CodeBlockTimer("test", delimiter=delimiter, db_name=self.db_name) as timer:
run_id = _m.run_id
with CodeBlockTimer("delimiter", delimiter=delimiter, db_name=self.db_name) as inner:
z = math.factorial(10)
self._verifyEvents(run_id, ['test', 'test{}delimiter'.format(delimiter)])
def tearDown(self):
try:
# Destroy the sqlite DB.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment