1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
########################################################################
# File name: test_xml.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import base64
import io
import itertools
import unittest
import random
import aioxmpp.xso as xso
import aioxmpp.xml
from aioxmpp.benchtest import times, timed, record
class ShallowRoot(xso.XSO):
TAG = ("uri:test", "shallow")
attr = xso.Attr("a")
data = xso.Text()
def __init__(self, scale=1):
super().__init__()
self.attr = "foobar"*(2*scale)
self.data = "fnord"*(10*scale)
class DeepLeaf(xso.XSO):
TAG = ("uri:test", "leaf")
data = xso.Text()
def generate(self, rng, depth):
self.data = "foo" * (2*rng.randint(1, 10))
class DeepNode(xso.XSO):
TAG = ("uri:test", "node")
data = xso.Attr("attr")
children = xso.ChildList([DeepLeaf])
def generate(self, rng, depth):
self.data = "foo" * (2*rng.randint(1, 10))
if depth >= 5:
cls = DeepLeaf
else:
cls = DeepNode
self.children.append(cls())
for i in range(rng.randint(2, 10)):
if rng.randint(1, 10) == 1:
item = DeepNode()
else:
item = DeepLeaf()
self.children.append(item)
for item in self.children:
item.generate(rng, depth+1)
DeepNode.register_child(DeepNode.children, DeepNode)
class DeepRoot(xso.XSO):
TAG = ("uri:test", "root")
children = xso.ChildList([DeepLeaf, DeepNode])
def generate(self, rng):
self.children[:] = [DeepNode() for i in range(3)]
for child in self.children:
child.generate(rng, 1)
class TestxmlValidateNameValue_str(unittest.TestCase):
KEY = "aioxmpp.xml", "xmlValidateNameValue"
def test_exhaustive(self):
validate = aioxmpp.xml.xmlValidateNameValue_str
r1 = range(0, 0xd800)
r2 = range(0xe000, 0xf0000)
range_iter = itertools.chain(
# exclude surrogates
r1, r2,
)
with timed() as timer:
for cp in range_iter:
validate(chr(cp))
record(self.KEY + ("exhaustive",),
timer.elapsed / (len(r1) + len(r2)),
"s")
def test_exhaustive_dualchar(self):
validate = aioxmpp.xml.xmlValidateNameValue_str
strs = ["x" + chr(cp) for cp in range(0, 0xd800)]
with timed() as timer:
for s in strs:
validate(s)
record(self.KEY + ("exhaustive_dualchar",),
timer.elapsed / (len(strs)),
"s")
def test_random_strings(self):
key = self.KEY + ("random",)
validate = aioxmpp.xml.xmlValidateNameValue_str
rng = random.Random(1)
samples = []
for i in range(1000):
samples.append(base64.b64encode(
random.getrandbits(120).to_bytes(120//8, 'little')
).decode("ascii").rstrip("="))
for sample in samples:
with timed() as timer:
validate(sample)
record(key, timer.elapsed, "s")
class Testwrite_single_xso(unittest.TestCase):
KEY = "aioxmpp.xml", "write_single_xso"
@classmethod
def setUpClass(cls):
rng = random.Random(1)
cls.deep_samples = [
DeepRoot()
for i in range(10)
]
for sample in cls.deep_samples:
with timed(cls.KEY+("deep", "generate")):
sample.generate(rng)
def setUp(self):
self.buf = io.BytesIO(bytearray(1024*1024))
def _reset_buffer(self):
self.buf.seek(0)
@times(1000)
def test_shallow_and_small(self):
key = self.KEY + ("shallow+small",)
item = ShallowRoot()
self._reset_buffer()
with timed() as t:
aioxmpp.xml.write_single_xso(item, self.buf)
record(key+("sz",), self.buf.tell(), "B")
record(key+("rate",), self.buf.tell() / t.elapsed, "B/s")
@times(1000)
def test_shallow_and_large(self):
key = self.KEY + ("shallow+large",)
item = ShallowRoot(scale=100)
self._reset_buffer()
with timed() as t:
aioxmpp.xml.write_single_xso(item, self.buf)
record(key+("sz",), self.buf.tell(), "B")
record(key+("rate",), self.buf.tell() / t.elapsed, "B/s")
@times(1000, pass_iteration=True)
def test_deep(self, iteration=None):
key = self.KEY + ("deep",)
item = self.deep_samples[iteration % len(self.deep_samples)]
self._reset_buffer()
with timed() as t:
aioxmpp.xml.write_single_xso(item, self.buf)
record(key+("sz",), self.buf.tell(), "B")
record(key+("rate",), self.buf.tell() / t.elapsed, "B/s")
|