# -*- coding: utf-8 -*-

from __future__ import print_function

import os
import sys
import timeit

from _test_context import TESTDATADIR


opts = sys.argv[1:]
all = not opts or "all" in opts

try:
    from configmix.config import fast_unquote, fast_quote, \
                                 fast_pathstr2path, _fast_split_ns
except ImportError:
    fast_unquote = fast_quote = fast_pathstr2path = _fast_split_ns = None

setup = """
import os

from _test_context import TESTDATADIR

import configmix
from configmix.config import _HIER_SEPARATOR, \
                             py_quote, py_unquote, py_pathstr2path, \
                             _py_split_ns
try:
    from configmix.config import fast_unquote, fast_quote, fast_pathstr2path, _fast_split_ns
except ImportError:
    fast_unquote = fast_quote = fast_pathstr2path = _fast_split_ns = None

cfg = configmix.load(os.path.join(TESTDATADIR, "conf_perf.py"))

se = u""
s1 = u"abc.def.hij"
ns_s1 = u"PY:abc.def.hij"

"""


num = 1000000
num_quote = 1 * num

if all or "quote" in opts or "unquote" in opts or "path" in opts:
    print("unquote/nothing/split: %.4f" % timeit.timeit('a = [py_unquote(vp) for vp in u"abc.def.hij".split(_HIER_SEPARATOR)]', setup=setup, number=num_quote))
    print("unquote/yes/split: %.4f" % timeit.timeit('a = [py_unquote(vp) for vp in u"ab%x20.def.h%x2ej".split(_HIER_SEPARATOR)]', setup=setup, number=num_quote))
    print("unquote/nothing/no-split: %.4f" % timeit.timeit('a = [py_unquote(vp) for vp in (u"abc," u"def", u"hij")]', setup=setup, number=num_quote))
    print("unquote/yes/no-split: %.4f" % timeit.timeit('a = [py_unquote(vp) for vp in (u"ab%x20", u"def", u"h%x2ej")]', setup=setup, number=num_quote))
    if fast_unquote:
        print("fast-unquote/nothing/split: %.4f" % timeit.timeit('a = [fast_unquote(vp) for vp in u"abc.def.hij".split(_HIER_SEPARATOR)]', setup=setup, number=num_quote))
        print("fast-unquote/yes/split: %.4f" % timeit.timeit('a = [fast_unquote(vp) for vp in u"ab%x20.def.h%x2ej".split(_HIER_SEPARATOR)]', setup=setup, number=num_quote))
        print("fast-unquote/nothing/no-split: %.4f" % timeit.timeit('a = [fast_unquote(vp) for vp in (u"abc," u"def", u"hij")]', setup=setup, number=num_quote))
        print("fast-unquote/yes/no-split: %.4f" % timeit.timeit('a = [fast_unquote(vp) for vp in (u"ab%x20", u"def", u"h%x2ej")]', setup=setup, number=num_quote))
    print("pathstr2path/non-empty: %.4f" % timeit.timeit('a = py_pathstr2path(s1)', setup=setup, number=num_quote))
    print("pathstr2path/empty: %.4f" % timeit.timeit('a = py_pathstr2path(se)', setup=setup, number=num_quote))
    if fast_pathstr2path:
        print("fast-pathstr2path/non-empty: %.4f" % timeit.timeit('a = fast_pathstr2path(s1)', setup=setup, number=num_quote))
        print("fast-pathstr2path/empty: %.4f" % timeit.timeit('a = fast_pathstr2path(se)', setup=setup, number=num_quote))
    print("quote/nothing: %.4f" % timeit.timeit('a = [py_quote(vp) for vp in (u"abc", u"def", u"hij")]', setup=setup, number=num_quote))
    print("quote/yes: %.4f" % timeit.timeit('a = [py_quote(vp) for vp in (u"ab:c", u"def", u"h.ij")]', setup=setup, number=num_quote))
    if fast_quote:
        print("fast-quote/nothing: %.4f" % timeit.timeit('a = [fast_quote(vp) for vp in (u"abc", u"def", u"hij")]', setup=setup, number=num_quote))
        print("fast-quote/yes: %.4f" % timeit.timeit('a = [fast_quote(vp) for vp in (u"ab:c", u"def", u"h.ij")]', setup=setup, number=num_quote))
        
    print("split-ns/no-ns: %.4f" % timeit.timeit('a = _py_split_ns(s1)', setup=setup, number=num_quote))
    print("split-ns/ns: %.4f" % timeit.timeit('a = _py_split_ns(ns_s1)', setup=setup, number=num_quote))    
    if _fast_split_ns:
        print("fast-split-ns/no-ns: %.4f" % timeit.timeit('a = _fast_split_ns(s1)', setup=setup, number=num_quote))
        print("fast-split-ns/ns: %.4f" % timeit.timeit('a = _fast_split_ns(ns_s1)', setup=setup, number=num_quote))                
    print("="*50)

if all or "default" in opts or "non-existing" in opts:
    print("default: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"yyy", default=None)', setup=setup, number=num))
    print("default: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"yyy", default=None)', setup=setup, number=num))
    print("default: %.4f" % timeit.timeit('cfg.getvar(u"tree1.yyy", default=None)', setup=setup, number=num))
    print("default: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.yyy", default=None)', setup=setup, number=num))
    print("-"*50)

if all or "simple-string" in opts:
    print("simple-string: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"tree2", "key4")', setup=setup, number=num))
    print("simple-string: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"tree2", "key4")', setup=setup, number=num))
    print("simple-string: %.4f" % timeit.timeit('cfg.getvar(u"tree1.tree2.key4")', setup=setup, number=num))
    print("simple-string: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.tree2.key4")', setup=setup, number=num))
    print("-"*50)

if all or "expand-string-one" in opts:
    print("expand-string-one: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"tree2", "key11")', setup=setup, number=num))
    print("expand-string-one: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"tree2", "key11")', setup=setup, number=num))
    print("expand-string-one: %.4f" % timeit.timeit('cfg.getvar(u"tree1.tree2.key11")', setup=setup, number=num))
    print("expand-string-one: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.tree2.key11")', setup=setup, number=num))
    print("-"*50)

if all or "expand-string-one-noncached" in opts:
    print("expand-string-one-noncached: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"tree2", "key12")', setup=setup, number=num))
    print("expand-string-one-noncached: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"tree2", "key12")', setup=setup, number=num))
    print("expand-string-one-noncached: %.4f" % timeit.timeit('cfg.getvar(u"tree1.tree2.key12")', setup=setup, number=num))
    print("expand-string-one-noncached: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.tree2.key12")', setup=setup, number=num))
    print("-"*50)

if all or "expand-string-many" in opts:
    print("expand-string-many: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"tree2", "key10")', setup=setup, number=num))
    print("expand-string-many: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"tree2", "key10")', setup=setup, number=num))
    print("expand-string-many: %.4f" % timeit.timeit('cfg.getvar(u"tree1.tree2.key10")', setup=setup, number=num))
    print("expand-string-many: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.tree2.key10")', setup=setup, number=num))
    print("-"*50)

if all or "expand-string-many-noncached" in opts:
    print("expand-string-many-noncached: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"tree2", u"key13")', setup=setup, number=num))
    print("expand-string-many-noncached: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"tree2", u"key13")', setup=setup, number=num))
    print("expand-string-many-noncached: %.4f" % timeit.timeit('cfg.getvar(u"tree1.tree2.key13")', setup=setup, number=num))
    print("expand-string-many-noncached: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.tree2.key13")', setup=setup, number=num))
    print("-"*50)

if all or "expand-list" in opts:
    print("expand-list: %.4f" % timeit.timeit('cfg.getvarl(u"tree1", u"tree2", "key8")', setup=setup, number=num))
    print("expand-list: %.4f" % timeit.timeit('cfg.getvarl_s(u"tree1", u"tree2", "key8")', setup=setup, number=num))
    print("expand-list: %.4f" % timeit.timeit('cfg.getvar(u"tree1.tree2.key8")', setup=setup, number=num))
    print("expand-list: %.4f" % timeit.timeit('cfg.getvar_s(u"tree1.tree2.key8")', setup=setup, number=num))
    print("-"*50)
