Skip to content

Instantly share code, notes, and snippets.

@achanda
Last active December 31, 2015 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achanda/7990477 to your computer and use it in GitHub Desktop.
Save achanda/7990477 to your computer and use it in GitHub Desktop.
Measure performance of two implementations
import argparse
import copy
import datetime
import getpass
import locale
import os
import six
import sys
import time
import mock
import six
from novaclient import exceptions
from novaclient.openstack.common import strutils
from novaclient.openstack.common import timeutils
from novaclient.openstack.common import uuidutils
from novaclient import utils
from novaclient.v1_1 import availability_zones
from novaclient.v1_1 import quotas
from novaclient.v1_1 import servers
import profile
def get_secgroup_proposed(cs, secgroup):
# Check secgroup is an ID (nova-network) or UUID (neutron)
if (utils.is_integer_like(strutils.safe_encode(secgroup))
or uuidutils.is_uuid_like(secgroup)):
try:
return cs.security_groups.get(secgroup)
except exceptions.NotFound:
pass
# Check secgroup as a name
matches = []
encoding = (locale.getpreferredencoding() or
sys.stdin.encoding or
'UTF-8')
groups = cs.security_groups.list()
for group in groups:
if not six.PY3:
group.name = group.name.encode(encoding)
if secgroup == group.name:
matches.append(group)
if len(matches) > 1:
msg = ("Multiple security group matches found for name"
" '%s', use an ID to be more specific." % secgroup)
raise exceptions.NoUniqueMatch(msg)
elif len(matches) == 0:
raise exceptions.CommandError("Secgroup ID or name '%s' not found."
% secgroup)
else:
return matches[0]
def get_secgroup(cs, secgroup):
# Check secgroup is an ID (nova-network) or UUID (neutron)
if (utils.is_integer_like(strutils.safe_encode(secgroup))
or uuidutils.is_uuid_like(secgroup)):
try:
return cs.security_groups.get(secgroup)
except exceptions.NotFound:
pass
# Check secgroup as a name
match_found = False
for s in cs.security_groups.list():
encoding = (locale.getpreferredencoding() or
sys.stdin.encoding or
'UTF-8')
if not six.PY3:
s.name = s.name.encode(encoding)
if secgroup == s.name:
if match_found is not False:
msg = ("Multiple security group matches found for name"
" '%s', use an ID to be more specific." % secgroup)
raise exceptions.NoUniqueMatch(msg)
match_found = s
if match_found is False:
raise exceptions.CommandError("Secgroup ID or name '%s' not found."
% secgroup)
return match_found
if __name__ == '__main__':
groups = [mock.MagicMock() for i in range(1000)]
for i in range(1000):
groups[i].name = str(i)
cs = mock.Mock(**{
'security_groups.get.return_value': 'sec_group',
'security_groups.list.return_value': groups,
})
profile.run("get_secgroup(cs, '999')")
profile.run("get_secgroup_proposed(cs, '999')")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment