Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vmware_vm_info.py #2194

Merged

Conversation

eddiehavila
Copy link
Contributor

SUMMARY

unrolling the return statement for "def get_vm_attributes(self, vm)" results in a ~25 fold speed increase processing a 2,200 VMs inventory. From 2.7 seconds average per VM getting attributes for original code to 0.11 seconds for the new unrolled code

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

vmware_vm_info.py

ADDITIONAL INFORMATION

Comparison using original and new code


    def get_vm_attributes1(self, vm):
        return dict((x.name, v.value) for x in self.custom_field_mgr
                    for v in vm.customValue if x.key == v.key)

    def get_vm_attributes2(self, vm):
      custom_field_values = vm.customValue
      custom_field_mgr = self.custom_field_mgr
      vm_attributes = {}
      for custom_field in custom_field_mgr:
          for custom_value in custom_field_values:
              if custom_field.key == custom_value.key:
                  vm_attributes[custom_field.name] = custom_value.value
      return vm_attributes



    def get_virtual_machines(self):
[.............]
            vm_attributes = dict()
            if self.module.params.get('show_attribute'):
                attributes_start = time.time()
                vm_attributes = self.get_vm_attributes1(vm)
                attributes_end = time.time()
                logging.debug(vm_attributes)
                logging.debug("Time to get VM attributes1: %f seconds", attributes_end - attributes_start)
                attributes_start = time.time()
                vm_attributes = self.get_vm_attributes2(vm)
                attributes_end = time.time()
                logging.debug(vm_attributes)
                logging.debug("Time to get VM attributes2: %f seconds", attributes_end - attributes_start)


2024-10-02 16:41:31,188 - DEBUG - {'BusinessArea': 'b1', 'Project': '199400081', 'CriticalityLevel': '1', 'DataSensitivity': 'L1', 'LicensedProduct': 'N/A', 'OS': 'Windows2019', 'SystemEnvironment': 'prd'}
2024-10-02 16:41:31,189 - DEBUG - Time to get VM attributes1: 2.718272 seconds
2024-10-02 16:41:31,303 - DEBUG - {'BusinessArea': 'b1', 'Project': '199400081', 'CriticalityLevel': '1', 'DataSensitivity': 'L1', 'LicensedProduct': 'N/A', 'OS': 'Windows2019', 'SystemEnvironment': 'prd'}
2024-10-02 16:41:31,304 - DEBUG - Time to get VM attributes2: 0.114557 seconds

unrolling the return statement for "def get_vm_attributes(self, vm)" results in a ~25 fold speed increase processing a 2,200 VMs inventory. From 2.7 seconds average per VM getting attributes for original code to 0.11 seconds for the new unrolled code 

example using original and new code:
2024-10-02 16:41:31,188 - DEBUG - {'BusinessArea': 'b1', 'Project': '199400081', 'CriticalityLevel': '1', 'DataSensitivity': 'L1', 'LicensedProduct': 'N/A', 'OS': 'Windows2019', 'SystemEnvironment': 'prd'}
2024-10-02 16:41:31,189 - DEBUG - Time to get VM attributes1: 2.718272 seconds
2024-10-02 16:41:31,303 - DEBUG - {'BusinessArea': 'b1', 'Project': '199400081', 'CriticalityLevel': '1', 'DataSensitivity': 'L1', 'LicensedProduct': 'N/A', 'OS': 'Windows2019', 'SystemEnvironment': 'prd'}
2024-10-02 16:41:31,304 - DEBUG - Time to get VM attributes2: 0.114557 seconds
@mariolenz
Copy link
Collaborator

Thanks @eddiehavila! I wanted to have a closer look at your PR, but ran into some issues with my test environment that I have to fix first. I'll try to have a closer look at you PR next week!

BTW could you please add a changelog fragment? Maybe something like changelogs/fragments/2194-vmware_vm_info.yml:

minor_changes:
  - vmware_vm_info - Improve performance when gathering tag information
    (https://github.com/ansible-collections/community.vmware/pull/2194).

or something similar.

added changelog fragment
@eddiehavila
Copy link
Contributor Author

added the changelog fragment. thanks @mariolenz for your guidance

Copy link

@alinabuzachis
Copy link
Contributor

recheck

Copy link
Collaborator

@mariolenz mariolenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't reproduce your ~25 fold speed increase, and I wonder why. I've tested with an environment that's not quite as large as yours (you're talking about 2,200 VMs) but coming close. Maybe you just use attributes more heavily than we do?

Still, I've seen an improvement for my very simple test case from a little bit more than 7 to somewhat less than 5 minutes.

Thanks @eddiehavila!

Copy link

Build succeeded (gate pipeline).
https://ansible.softwarefactory-project.io/zuul/buildset/a02bbd1c7d5b4002b3c984471e7a7bcb

✔️ ansible-tox-linters SUCCESS in 7m 45s
✔️ build-ansible-collection SUCCESS in 6m 20s
✔️ ansible-galaxy-importer SUCCESS in 4m 26s

@softwarefactory-project-zuul softwarefactory-project-zuul bot merged commit ea1db49 into ansible-collections:main Oct 8, 2024
13 checks passed
softwarefactory-project-zuul bot pushed a commit that referenced this pull request Oct 9, 2024
SUMMARY
Improve the performance of retrieving custom attributes.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
vmware_vm_info
ADDITIONAL INFORMATION
Backport of #2194
cc @eddiehavila
@eddiehavila eddiehavila deleted the patch-1 branch October 22, 2024 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants