Skip to content
This repository was archived by the owner on Aug 20, 2023. It is now read-only.

Commit 922d34f

Browse files
authored
1.0.0 beta4 merge request (#5)
* Improvements to readibility README, benchmarking code, and removed debugging code from tinymp main module * Included some benchmarking information Also, tidied up tinymp.py * Included some benchmarking information Also, tidied up tinymp.py * Reformatted table in README * Fleshed out README.rst file * Quick amendment to README to sort typo * Update README.rst * Changes for Beta4 release wrapped
1 parent 7fb2dbc commit 922d34f

File tree

6 files changed

+106
-21
lines changed

6 files changed

+106
-21
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
* Version 1.0.0-Beta4 - 11/26/2017
2+
* Added descriptions of benchmarking and tidied up repo
3+
* Further added information to README about usage
14

2-
* Version 1.0.0-Beta3 - 24/11/2017
5+
* Version 1.0.0-Beta3 - 11/24/2017
36
* Added support for u-msgpack-python alternative msgpack library
47

58
* Version 1.0.0-Beta2 - 11/21/2017

README.rst

+46-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@
1010

1111
TinyMP is a storage backend for TinyDB https://github.com/msiemens/tinydb which is based around the MessagePack compressed JSON format (https://msgpack.org/index.html)
1212

13+
Syntax :
14+
========
15+
TinyMP extends the syntax of the ``tinydb`` class using one of the optional ``kwargs`` as follows:
16+
17+
18+
.. code:: python
19+
class tinydb.database.TinyDB(*args, **kwargs)
20+
21+
.. csv-table:: Values for ``**kwargs``
22+
:header: "Value","Effect"
23+
:widths: 10,90
24+
25+
"``storage=MsgPackStorage``","Default option, will use the ``MsgPack`` library"
26+
"``storage=MsgPackStorage,Lib='msgpack'``","Will use the ``MsgPack`` library"
27+
"``storage=MsgPackStorage,Lib='umsgpack'``","Will use the ``U-MsgPack`` Library"
28+
29+
1330
Example Usage:
1431
==============
1532

@@ -26,9 +43,11 @@ Example Usage:
2643
dbins()
2744
2845
As you can see, it's a simple drop-in replacement for any storage engine
29-
and it can be nested and cached.
46+
and it can be nested and cached. Don't forget, you will need to install as a minimum,
47+
the ``msgpack-python`` library using ``pip install msgpack-python`` and the ``U-MsgPack``
48+
library from https://github.com/vsergeev/u-msgpack-python in order to use that option.
3049

31-
Example Usage using alternative MessgaePack Library:
50+
Example Usage using alternative MessagePack Library:
3251
====================================================
3352

3453
.. code:: python
@@ -45,10 +64,33 @@ Example Usage using alternative MessgaePack Library:
4564
4665
As you can see, it's a simple drop-in replacement for any storage engine
4766
and it can be nested and cached.
67+
68+
Why would I use this?
69+
=====================
70+
Looking at the statistics below, it's apparent that compared to the "standard"
71+
JSON Storage mechanism, MessagePack isn't as quick, however, the filesizes on
72+
disc are smaller - consider the table below, with 1,000 JSON documents of
73+
minute size - clearly, the MessagePack compressed format is smaller than
74+
the JSON format. Whether you choose the default MsgPack library, which is
75+
marginally slower than the U-MsgPack library (at the cost of a small increase
76+
in storage footprint with U-MsgPack) is dependent on your use case.
77+
78+
.. csv-table:: Timings (seconds)
79+
:header: "Format","Run 1", "Run 2", "Run 3", "FileSize"
80+
:widths: 10,30, 30, 30,10
81+
82+
"JSON Write:", 2.147,2.011,2.040,"37.0 Kb"
83+
"MsgPack Write:", 9.562,9.732,9.716,"21.1 Kb"
84+
"U-MsgPack Write:", 9.354,9.066,8.949,"24.1 Kb"
85+
4886
Changes
4987
=======
5088

51-
* Version 1.0.0-Beta3 - 24/11/2017
89+
* Version 1.0.0-Beta4 - 11/26/2017
90+
* Added descriptions of benchmarking and tidied up repo
91+
* Further added information to README about usage
92+
93+
* Version 1.0.0-Beta3 - 11/24/2017
5294
* Added support for u-msgpack-python alternative msgpack library
5395

5496
* Version 1.0.0-Beta2 - 11/21/2017
@@ -70,4 +112,5 @@ References:
70112

71113
* TinyDB https://github.com/msiemens/tinydb
72114
* MessagePack https://msgpack.org/index.html
115+
* U-MsgPack https://github.com/vsergeev/u-msgpack-python
73116

benchmarks/benchmark.py

+43-12
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,53 @@
44

55
import msgpack
66
from tinymp import *
7+
8+
import os
79

8-
db1 = TinyDB('data.msg',storage=MsgPackStorage)
9-
#db1 = TinyDB('data.json')
1010

11-
def dbins1():
12-
db1.insert({'type': 'apple', 'count': 7})
11+
def convert_bytes(num):
12+
"""
13+
this function will convert bytes to MB.... GB... etc
14+
"""
15+
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
16+
if num < 1024.0:
17+
return "%3.1f %s" % (num, x)
18+
num /= 1024.0
1319

14-
def dbins2():
15-
db2.insert({'type': 'apple', 'count': 7})
20+
21+
def file_size(file_path):
22+
"""
23+
this function will return the file size
24+
"""
25+
if os.path.isfile(file_path):
26+
file_info = os.stat(file_path)
27+
return convert_bytes(file_info.st_size)
28+
29+
def dbins(db):
30+
db.insert({'type': 'apple', 'count': 7})
31+
32+
def insertstuff(db,n,method,file_name):
33+
t0 = time.time()
34+
for i in range(n): dbins(db)
35+
t1 = time.time()
36+
37+
print method
38+
print "Write Time: = " + repr(t1-t0) + "s"
39+
print file_size(file_name)
40+
41+
42+
43+
db1 = TinyDB('data.json')
44+
db2 = TinyDB('data.msgpack.msg',storage=MsgPackStorage)
45+
db3 = TinyDB('data.umsgpack.msg',storage=MsgPackStorage,Lib='umsgpack')
46+
47+
48+
insertstuff(db1,1000,'JSON','data.json')
49+
insertstuff(db2,1000,'MsgPack','data.msgpack.msg')
50+
insertstuff(db3,1000,'U-MsgpPack','data.umsgpack.msg')
1651

1752
db1.purge_tables()
53+
db2.purge_tables()
54+
db3.purge_tables()
1855

19-
n = 1000
20-
t0 = time.time()
21-
for i in range(n): dbins1()
22-
t1 = time.time()
2356

24-
print (t1-t0)
25-
print "Write Benchmark: JSON = " + repr(t1-t0)

changenotes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
removed debugging statements from tinymp.py
2+
better inbenchmarking

tests/modifytests.sh

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
#!/bin/sh
2+
3+
# Make a new directory for working
4+
#mkdir originaltests
5+
6+
# Copy ALL tests and duplicate them for both messagepack tests
7+
#copy test*py originaltests
8+
9+
# Create new copies of the Python scripts for each StorageClass subtype
10+
#ls -1 test*.py| awk 'BEGIN { FS = "." } { print "cp " $0 " " $1".msgpack.py; cp "$0 " " $1 ".umsgpack.py" }'
11+
12+
213
echo "import umsgpack as msgpack" >> conftest.py
314
echo "import tinymp" >> conftest.py
415

tinymp.py

-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def touch(fname, create_dirs):
1414

1515
class MsgPackStorage(Storage):
1616
def __init__(self, path, create_dirs=False, **kwargs):
17-
#self.filename = filename
1817
super(MsgPackStorage, self).__init__()
1918
touch(path, create_dirs=create_dirs) # Create file if not exists
2019
self.kwargs = kwargs
@@ -25,15 +24,12 @@ def __init__(self, path, create_dirs=False, **kwargs):
2524
self.library=self.kwargs['Lib']
2625

2726
if self.library == 'umsgpack':
28-
print 'ultra-messagepack'
2927
import umsgpack as msgpack
3028
self.msgpack=msgpack
3129
else:
32-
print 'messagepack'
3330
import msgpack
3431
self.msgpack=msgpack
3532
else:
36-
print 'default'
3733
import msgpack
3834
self.msgpack=msgpack
3935

@@ -42,7 +38,6 @@ def __init__(self, path, create_dirs=False, **kwargs):
4238
def write(self, data):
4339
self._handle.seek(0)
4440
serialized = self.msgpack.dump(data, self._handle)
45-
#self._handle.write(serialized)
4641
self._handle.flush()
4742
self._handle.truncate()
4843

0 commit comments

Comments
 (0)