Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
code_block_timer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
code_block_timer
Commits
85faaafb
Commit
85faaafb
authored
Dec 19, 2014
by
John Eskew
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update README. Make thread-safe. Use file-relative path to schema.
parent
e4d9750b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
19 deletions
+46
-19
README.md
+28
-1
code_block_timer/__init__.py
+12
-15
code_block_timer/storage.py
+6
-3
No files found.
README.md
View file @
85faaafb
Python module which
puts timing around
Python code blocks.
Python module which
times
Python code blocks.
Features
--------
*
Nesting of timers - higher level timers continue to tick during lower-level timers starting/stopping.
*
Timing storage in a SQLite DB.
Usage
-----
```
from code_block_timer import CodeBlockTimer
for i in xrange(0, 2):
with CodeBlockTimer("all blocks"):
performSetUp()
with CodeBlockTimer("block1"):
perform_lengthy_task()
with CodeBlockTimer("block2"):
perform_another_lengthy_task()
```
In the SQLite DB, you'll now see six rows, similar to these:
```
1|1|all blocks:block1|201.16|2014-10-12 12:26:01
2|1|all blocks:block2|516.492|2014-10-12 12:26:01
3|1|all blocks|916.54|2014-10-12 12:26:01
4|2|all blocks:block1|199.16|2014-10-12 12:26:04
5|2|all blocks:block2|505.142|2014-10-12 12:26:04
6|2|all blocks|903.212|2014-10-12 12:26:04
```
code_block_timer/__init__.py
View file @
85faaafb
import
threading
from
timeit
import
default_timer
from
.storage
import
TimingDataStorage
class
Globals
(
object
):
pass
class
Globals
(
threading
.
local
):
# Current module-level nest stack.
# As CodeBlockTimer objects are __enter__ed, their descriptions are pushed
# onto this stack. The stack length indicates the current nesting level.
nest_stack
=
[]
_m
=
Globals
()
# Current run_id.
# Set from data storage when stack size increases from 0.
# While nest stack is populated, remains at a constant value.
# Identifies all the times from the same run.
run_id
=
None
# Current module-level nest stack.
# As CodeBlockTimer objects are __enter__ed, their descriptions are pushed
# onto this stack. The stack length indicates the current nesting level.
_m
.
nest_stack
=
[]
# Current run_id.
# Set from data storage when stack size increases from 0.
# While nest stack is populated, remains at a constant value.
# Identifies all the times from the same run.
_m
.
run_id
=
None
_m
=
Globals
()
class
CodeBlockTimer
(
object
):
...
...
@@ -28,7 +28,6 @@ class CodeBlockTimer(object):
self
.
data_store
=
TimingDataStorage
(
**
kwargs
)
def
__enter__
(
self
):
global
_m
if
len
(
_m
.
nest_stack
)
==
0
:
_m
.
run_id
=
self
.
data_store
.
run_id
()
_m
.
nest_stack
.
append
(
self
.
block_desc
)
...
...
@@ -36,8 +35,6 @@ class CodeBlockTimer(object):
return
self
def
__exit__
(
self
,
*
args
):
global
_m
# Compute elapsed times.
end
=
self
.
timer
()
self
.
elapsed_secs
=
end
-
self
.
start
...
...
code_block_timer/storage.py
View file @
85faaafb
import
sqlite3
import
os
import
sqlite3
from
path
import
path
# Assumes that the DB exists and the schema in schema.sql exists in it.
MODULE_DIR
=
path
(
__file__
)
.
dirname
()
class
TimingDataStorage
(
object
):
SCHEMA_NAME
=
'schema.sql'
SCHEMA_PATH
=
MODULE_DIR
/
self
.
SCHEMA_NAME
DEFAULT_DB_NAME
=
'block_times.db'
def
__init__
(
self
,
**
kwargs
):
...
...
@@ -23,8 +27,7 @@ class TimingDataStorage(object):
# Create the sqlite DB file.
with
open
(
db_name
,
"w"
)
as
f
:
conn
=
sqlite3
.
connect
(
db_name
)
schema_filepath
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
self
.
SCHEMA_NAME
)
with
open
(
schema_filepath
,
"r"
)
as
schema_file
:
with
open
(
self
.
SCHEMA_PATH
,
"r"
)
as
schema_file
:
schema
=
schema_file
.
read
()
cur
=
conn
.
cursor
()
conn
.
executescript
(
schema
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment