Skip to content

Instantly share code, notes, and snippets.

@janpipek
Last active October 2, 2019 10:59
Show Gist options
  • Save janpipek/4d0c73341f6457c9770ec5804d62bf45 to your computer and use it in GitHub Desktop.
Save janpipek/4d0c73341f6457c9770ec5804d62bf45 to your computer and use it in GitHub Desktop.
Benchmark pyasn1
from statistics import mean, stdev
import timeit
import pyasn1
from pyasn1.codec.ber.decoder import decode as ber_decode
from pyasn1.codec.der.decoder import decode as der_decode
from pyasn1.type import namedtype
from pyasn1.type import univ
from pyasn1.compat.octets import ints2octs, str2octs, null
from pyasn1_modules import pem
from pyasn1_modules import rfc2314
asn = univ.Set(
componentType=namedtype.NamedTypes(
namedtype.NamedType('place-holder', univ.Null(null)),
namedtype.OptionalNamedType('first-name', univ.OctetString()),
namedtype.DefaultedNamedType('age', univ.Integer(33)),
)
)
pem_64 = """\
MIIDATCCAekCAQAwgZkxCzAJBgNVBAYTAlJVMRYwFAYDVQQIEw1Nb3Njb3cgUmVn
aW9uMQ8wDQYDVQQHEwZNb3Njb3cxGjAYBgNVBAoTEVNOTVAgTGFib3JhdG9yaWVz
MQwwCgYDVQQLFANSJkQxFTATBgNVBAMTDHNubXBsYWJzLmNvbTEgMB4GCSqGSIb3
DQEJARYRaW5mb0Bzbm1wbGFicy5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQC9n2NfGS98JDBmAXQn+vNUyPB3QPYC1cwpX8UMYh9MdAmBZJCnvXrQ
Pp14gNAv6AQKxefmGES1b+Yd+1we9HB8AKm1/8xvRDUjAvy4iO0sqFCPvIfSujUy
pBcfnR7QE2itvyrMxCDSEVnMhKdCNb23L2TptUmpvLcb8wfAMLFsSu2yaOtJysep
oH/mvGqlRv2ti2+E2YA0M7Pf83wyV1XmuEsc9tQ225rprDk2uyshUglkDD2235rf
0QyONq3Aw3BMrO9ss1qj7vdDhVHVsxHnTVbEgrxEWkq2GkVKh9QReMZ2AKxe40j4
og+OjKXguOCggCZHJyXKxccwqCaeCztbAgMBAAGgIjAgBgkqhkiG9w0BCQIxExMR
U05NUCBMYWJvcmF0b3JpZXMwDQYJKoZIhvcNAQEFBQADggEBAAihbwmN9M2bsNNm
9KfxqiGMqqcGCtzIlpDz/2NVwY93cEZsbz3Qscc0QpknRmyTSoDwIG+1nUH0vzkT
Nv8sBmp9I1GdhGg52DIaWwL4t9O5WUHgfHSJpPxZ/zMP2qIsdPJ+8o19BbXRlufc
73c03H1piGeb9VcePIaulSHI622xukI6f4Sis49vkDaoi+jadbEEb6TYkJQ3AMRD
WdApGGm0BePdLqboW1Yv70WRRFFD8sxeT7Yw4qrJojdnq0xMHPGfKpf6dJsqWkHk
b5DRbjil1Zt9pJuF680S9wtBzSi0hsMHXR9TzS7HpMjykL2nmCVY6A78MZapsCzn
GGbx7DI=
"""
pem_text = pem.readBase64fromText(pem_64)
pem_spec = rfc2314.CertificationRequest()
examples = dict(
false=(ints2octs((1, 1, 0)), "ber_decode", {}),
example1=(ints2octs((48, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), "ber_decode", {}),
asn=(ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), "ber_decode", {"asn1Spec": asn}),
pem=(pem_text, "der_decode", {"asn1Spec": pem_spec})
)
TIMES = 10000
REPEAT = 40
TAKE = int(.8 * REPEAT)
if __name__ == "__main__":
for name, triplet in examples.items():
print(name)
example, method, kwargs = triplet
try:
results = timeit.repeat(f"{method}(example, **kwargs)", repeat=REPEAT, number=TIMES, globals=globals())
results = sorted(results)[:TAKE]
mean_, stdev_ = mean(results) / TIMES * 1e6, stdev(results) / TIMES * 1e6
mean_, stdev_ = [round(x, 2) for x in (mean_, stdev_)]
print(mean_, "±", stdev_, "us")
except Exception as ex:
print(ex)
branches=( master support-incomplete-substrate streamoptimum37 )
for branch in "${branches[@]}"
do
echo "${branch}"
git checkout ${branch}
python benchmark.py
done
master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
false
7.23 ± 0.12 us
example1
33.18 ± 0.31 us
asn
53.66 ± 0.28 us
pem
616.09 ± 4.11 us
support-incomplete-substrate
Switched to branch 'support-incomplete-substrate'
Your branch is up to date with 'origin/support-incomplete-substrate'.
false
17.0 ± 0.16 us
example1
60.35 ± 0.24 us
asn
90.99 ± 0.25 us
pem
750.14 ± 2.04 us
streamoptimum37
Switched to branch 'streamoptimum37'
Your branch is up to date with 'origin/streamoptimum37'.
false
8.15 ± 0.36 us
example1
33.84 ± 0.13 us
asn
53.5 ± 0.14 us
pem
623.14 ± 0.98 us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment