Created
October 12, 2016 00:42
-
-
Save dvarrazzo/43b43d6ae96e13319cb085a3efe92ac8 to your computer and use it in GitHub Desktop.
Benchmark program to measure psycopg decoding speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Test fetching unicode strings for the database. | |
""" | |
from __future__ import print_function | |
from timeit import Timer | |
def main(): | |
opt = parse_cmdline() | |
setup = """\ | |
import psycopg2 | |
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | |
cnn = psycopg2.connect('') | |
cnn.set_client_encoding('UTF8') | |
cur = cnn.cursor() | |
cur.execute(''' | |
select repeat(chr(n), %d) | |
from generate_series(32, 32 + %d) s(n), generate_series(1, %d) m | |
''') | |
""" % (opt.strsize, opt.chrrange, opt.mult) | |
stmt = """\ | |
cur.scroll(0, 'absolute') | |
x = cur.fetchall() | |
""" | |
timer = Timer(stmt, setup) | |
print("timing for strsize: %s, chrrange: %s, mult: %s" | |
% (opt.strsize, opt.chrrange, opt.mult)) | |
ts = timer.repeat(repeat=5, number=1) | |
print("times: %s sec" % ', '.join("%f" % t for t in ts)) | |
print("best: %f sec" % min(ts)) | |
def parse_cmdline(): | |
from optparse import OptionParser | |
parser = OptionParser(usage="%prog [options]", | |
description=__doc__) | |
parser.add_option('-s', '--strsize', type=int, default=100, | |
help="size of the strings [default: %default]") | |
parser.add_option('-c', '--chrrange', type=int, default=4096, | |
help="max unicode char range (-ish) [default: %default]") | |
parser.add_option('-m', '--mult', type=int, default=10, | |
help="number of repetitions of the data set [default: %default]") | |
opt, args = parser.parse_args() | |
opt.args = args | |
return opt | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment