# Path: oslo_utils/strutils.py
# UNIT_PREFIX_EXPONENT = {
#     'k': 1,
#     'K': 1,
#     'Ki': 1,
#     'M': 2,
#     'Mi': 2,
#     'G': 3,
#     'Gi': 3,
#     'T': 4,
#     'Ti': 4,
# }
# UNIT_SYSTEM_INFO = {
#     'IEC': (1024, re.compile(r'(^[-+]?\d*\.?\d+)([KMGT]i?)?(b|bit|B)$')),
#     'SI': (1000, re.compile(r'(^[-+]?\d*\.?\d+)([kMGT])?(b|bit|B)$')),
#     'mixed': (None, re.compile(r'(^[-+]?\d*\.?\d+)([kKMGT]i?)?(b|bit|B)$')),
# }
# TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes')
# FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no')
# SLUGIFY_STRIP_RE = re.compile(r"[^\w\s-]")
# SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+")
# _SANITIZE_KEYS = ['adminpass', 'admin_pass', 'password', 'admin_password',
#                   'auth_token', 'new_pass', 'auth_password', 'secret_uuid',
#                   'secret', 'sys_pswd', 'token', 'configdrive',
#                   'chappassword', 'encrypted_key', 'private_key',
#                   'fernetkey', 'sslkey', 'passphrase',
#                   'cephclusterfsid', 'octaviaheartbeatkey', 'rabbitcookie',
#                   'cephmanilaclientkey', 'pacemakerremoteauthkey',
#                   'designaterndckey', 'cephadminkey', 'heatauthencryptionkey',
#                   'cephclientkey', 'keystonecredential',
#                   'barbicansimplecryptokek', 'cephrgwkey', 'swifthashsuffix',
#                   'migrationsshkey', 'cephmdskey', 'cephmonkey']
# _SANITIZE_PATTERNS_2 = {}
# _SANITIZE_PATTERNS_1 = {}
# _SANITIZE_PATTERNS_WILDCARD = {}
# _FORMAT_PATTERNS_1 = [r'(%(key)s[0-9]*\s*[=]\s*)[^\s^\'^\"]+']
# _FORMAT_PATTERNS_2 = [r'(%(key)s[0-9]*\s*[=]\s*[\"\'])[^\"\']*([\"\'])',
#                       r'(%(key)s[0-9]*\s+[\"\'])[^\"\']*([\"\'])',
#                       r'([-]{2}%(key)s[0-9]*\s+)[^\'^\"^=^\s]+([\s]*)',
#                       r'(<%(key)s[0-9]*>)[^<]*(</%(key)s[0-9]*>)',
#                       r'([\"\']%(key)s[0-9]*[\"\']\s*:\s*[\"\'])[^\"\']*'
#                       r'([\"\'])',
#                       r'([\'"][^"\']*%(key)s[0-9]*[\'"]\s*:\s*u?[\'"])[^\"\']*'
#                       r'([\'"])',
#                       r'([\'"][^\'"]*%(key)s[0-9]*[\'"]\s*,\s*\'--?[A-z]+'
#                       r'\'\s*,\s*u?[\'"])[^\"\']*([\'"])',
#                       r'(%(key)s[0-9]*\s*--?[A-z]+\s*)\S+(\s*)']
# _FORMAT_PATTERNS_WILDCARD = [r'([\'\"][^\"\']*%(key)s[0-9]*[\'\"]\s*:\s*u?[\'\"].*[\'\"])[^\"\']*([\'\"])']  # noqa: E501
# def int_from_bool_as_string(subject):
# def bool_from_string(subject, strict=False, default=False):
# def is_valid_boolstr(value):
# def string_to_bytes(text, unit_system='IEC', return_int=False):
# def to_slug(value, incoming=None, errors="strict"):
# def mask_password(message, secret="***"):  # nosec
# def mask_dict_password(dictionary, secret="***"):  # nosec
# def is_int_like(val):
# def check_string_length(value, name=None, min_length=0, max_length=None):
# def validate_integer(value, name, min_value=None, max_value=None):
# def split_path(path, minsegs=1, maxsegs=None, rest_with_last=False):
# def split_by_commas(value):


import timeit
from oslo_utils import strutils
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Performance tests for mask_password.
"""



# A moderately sized input (~50K) string
# http://paste.openstack.org/raw/155864/
# infile = '155864.txt'

# Untruncated version of the above (~310K)
# http://dl.sileht.net/public/payload.json.gz
infile = 'large_json_payload.txt'

with open(infile, 'r') as f:
    input_str = f.read()
print('payload has %d bytes' % len(input_str))

for pattern in strutils._SANITIZE_PATTERNS_2['admin_pass']:
    print('\ntesting %s' % pattern.pattern)
    t = timeit.Timer(
        r"re.sub(pattern, r'\g<1>***\g<2>', payload)",
        """
import re
payload = '''%s'''

# Path: oslo_utils/strutils.py
# UNIT_PREFIX_EXPONENT = {
#     'k': 1,
#     'K': 1,
#     'Ki': 1,
#     'M': 2,
#     'Mi': 2,
#     'G': 3,
#     'Gi': 3,
#     'T': 4,
#     'Ti': 4,
# }
# UNIT_SYSTEM_INFO = {
#     'IEC': (1024, re.compile(r'(^[-+]?\d*\.?\d+)([KMGT]i?)?(b|bit|B)$')),
#     'SI': (1000, re.compile(r'(^[-+]?\d*\.?\d+)([kMGT])?(b|bit|B)$')),
#     'mixed': (None, re.compile(r'(^[-+]?\d*\.?\d+)([kKMGT]i?)?(b|bit|B)$')),
# }
# TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes')
# FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no')
# SLUGIFY_STRIP_RE = re.compile(r"[^\w\s-]")
# SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+")
# _SANITIZE_KEYS = ['adminpass', 'admin_pass', 'password', 'admin_password',
#                   'auth_token', 'new_pass', 'auth_password', 'secret_uuid',
#                   'secret', 'sys_pswd', 'token', 'configdrive',
#                   'chappassword', 'encrypted_key', 'private_key',
#                   'fernetkey', 'sslkey', 'passphrase',
#                   'cephclusterfsid', 'octaviaheartbeatkey', 'rabbitcookie',
#                   'cephmanilaclientkey', 'pacemakerremoteauthkey',
#                   'designaterndckey', 'cephadminkey', 'heatauthencryptionkey',
#                   'cephclientkey', 'keystonecredential',
#                   'barbicansimplecryptokek', 'cephrgwkey', 'swifthashsuffix',
#                   'migrationsshkey', 'cephmdskey', 'cephmonkey']
# _SANITIZE_PATTERNS_2 = {}
# _SANITIZE_PATTERNS_1 = {}
# _SANITIZE_PATTERNS_WILDCARD = {}
# _FORMAT_PATTERNS_1 = [r'(%(key)s[0-9]*\s*[=]\s*)[^\s^\'^\"]+']
# _FORMAT_PATTERNS_2 = [r'(%(key)s[0-9]*\s*[=]\s*[\"\'])[^\"\']*([\"\'])',
#                       r'(%(key)s[0-9]*\s+[\"\'])[^\"\']*([\"\'])',
#                       r'([-]{2}%(key)s[0-9]*\s+)[^\'^\"^=^\s]+([\s]*)',
#                       r'(<%(key)s[0-9]*>)[^<]*(</%(key)s[0-9]*>)',
#                       r'([\"\']%(key)s[0-9]*[\"\']\s*:\s*[\"\'])[^\"\']*'
#                       r'([\"\'])',
#                       r'([\'"][^"\']*%(key)s[0-9]*[\'"]\s*:\s*u?[\'"])[^\"\']*'
#                       r'([\'"])',
#                       r'([\'"][^\'"]*%(key)s[0-9]*[\'"]\s*,\s*\'--?[A-z]+'
#                       r'\'\s*,\s*u?[\'"])[^\"\']*([\'"])',
#                       r'(%(key)s[0-9]*\s*--?[A-z]+\s*)\S+(\s*)']
# _FORMAT_PATTERNS_WILDCARD = [r'([\'\"][^\"\']*%(key)s[0-9]*[\'\"]\s*:\s*u?[\'\"].*[\'\"])[^\"\']*([\'\"])']  # noqa: E501
# def int_from_bool_as_string(subject):
# def bool_from_string(subject, strict=False, default=False):
# def is_valid_boolstr(value):
# def string_to_bytes(text, unit_system='IEC', return_int=False):
# def to_slug(value, incoming=None, errors="strict"):
# def mask_password(message, secret="***"):  # nosec
# def mask_dict_password(dictionary, secret="***"):  # nosec
# def is_int_like(val):
# def check_string_length(value, name=None, min_length=0, max_length=None):
# def validate_integer(value, name, min_value=None, max_value=None):
# def split_path(path, minsegs=1, maxsegs=None, rest_with_last=False):
# def split_by_commas(value):


# Path: tools/perf_test_mask_password.py
import timeit
from oslo_utils import strutils

#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Performance tests for mask_password.
"""



# A moderately sized input (~50K) string
# http://paste.openstack.org/raw/155864/
# infile = '155864.txt'

# Untruncated version of the above (~310K)
# http://dl.sileht.net/public/payload.json.gz
infile = 'large_json_payload.txt'

with open(infile, 'r') as f:
    input_str = f.read()
print('payload has %d bytes' % len(input_str))

for pattern in strutils._SANITIZE_PATTERNS_2['admin_pass']:
    print('\ntesting %s' % pattern.pattern)
    t = timeit.Timer(
        r"re.sub(pattern, r'\g<1>***\g<2>', payload)",
        """
import re
payload = '''%s'''

pattern = re.compile(r'''%s''')