mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
perf(instances): VM lists read each host once, not three calls a VM
A VM list row showed the VM's state, memory, vCPUs and title, read through the VM's own connection object: a lookup by UUID, virDomainGetInfo and the live XML, one after the other for every VM on the page. The lists (Instances, grouped or not, and a compute's VMs) now read each host once: the states in one getAllDomainStats call (the state group only, as the lists' poll; no other group, so a VM busy in the QEMU monitor holds nothing up) and the memory, vCPUs and title from each VM's live XML, which the title already read. Checked on QEMU against virDomainGetInfo for running, paused and shut-off VMs: the same values. A VM gone from its host, or a host that cannot be read or fails its listing, shows its VMs unknown as before. Only the grouped list, a superuser's, prefetches every host's VMs and owners.
This commit is contained in:
parent
37b87839c1
commit
2d0f41ee57
9 changed files with 130 additions and 104 deletions
|
|
@ -1,8 +1,11 @@
|
|||
"""Instance lists ask libvirt for a VM's info once per row."""
|
||||
"""Instance lists read their VMs once a host (get_domain_rows): the states in
|
||||
one call and each VM's live XML once, no lookup or info call a VM; on
|
||||
libvirt's test driver."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from collections import Counter
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
import libvirt
|
||||
from appsettings.models import AppSettings
|
||||
from computes.models import Compute
|
||||
from django.contrib.auth import get_user_model
|
||||
|
|
@ -10,78 +13,71 @@ from django.test import TestCase
|
|||
from django.urls import reverse
|
||||
from instances.models import Instance
|
||||
|
||||
# virDomainGetInfo: state, max memory KiB, memory KiB, vCPUs, CPU time
|
||||
INFO = [1, 4194304, 2097152, 3, 0]
|
||||
DOMAIN = """<domain type='test'><name>{name}</name><uuid>{uuid}</uuid><title>{name} title</title>
|
||||
<memory unit='MiB'>4096</memory><currentMemory unit='MiB'>2048</currentMemory><vcpu current='3'>4</vcpu>
|
||||
<os><type arch='i686'>hvm</type></os></domain>"""
|
||||
VMS = {"vm-a": "11111111-1111-1111-1111-111111111111", "vm-b": "22222222-2222-2222-2222-222222222222"}
|
||||
|
||||
|
||||
class FakeDomain:
|
||||
# a plain class: Django templates do not call MagicMock attributes
|
||||
def __init__(self):
|
||||
self.info_calls = 0
|
||||
self.xml_calls = 0
|
||||
|
||||
def info(self):
|
||||
self.info_calls += 1
|
||||
return INFO
|
||||
|
||||
def XMLDesc(self, flags=0):
|
||||
self.xml_calls += 1
|
||||
return "<domain><title></title><currentMemory>2097152</currentMemory></domain>"
|
||||
|
||||
|
||||
class ListInfoCallTests(TestCase):
|
||||
class ListReadTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client.force_login(
|
||||
get_user_model().objects.create_superuser(username="rpc-admin", password="pwd", email="rpc@example.com")
|
||||
)
|
||||
self.conn = libvirt.open("test:///default")
|
||||
self.addCleanup(self.conn.close)
|
||||
self.compute = Compute.objects.create(name="rpc-compute", hostname="localhost", type=4, login="", password="")
|
||||
Instance.objects.create(compute=self.compute, name="vm-a", uuid="11111111-1111-1111-1111-111111111111")
|
||||
Instance.objects.create(compute=self.compute, name="vm-b", uuid="22222222-2222-2222-2222-222222222222")
|
||||
self.domains = []
|
||||
|
||||
def proxy(self, *args, **kwargs):
|
||||
domain = FakeDomain()
|
||||
self.domains.append(domain)
|
||||
return SimpleNamespace(
|
||||
instance=domain,
|
||||
get_uuid=lambda: "uuid",
|
||||
get_title=lambda: domain.XMLDesc() and "",
|
||||
get_cur_memory=lambda: int(domain.XMLDesc().split(">")[4].split("<")[0]) // 1024,
|
||||
)
|
||||
for name, uuid in VMS.items():
|
||||
domain = self.conn.defineXML(DOMAIN.format(name=name, uuid=uuid))
|
||||
domain.create()
|
||||
self.addCleanup(domain.undefine)
|
||||
self.addCleanup(domain.destroy)
|
||||
Instance.objects.create(compute=self.compute, name=name, uuid=uuid)
|
||||
|
||||
def get(self, url):
|
||||
compute_patches = {"status": True, "cpu_count": 1, "ram_size": 1024, "ram_usage": 0}
|
||||
with patch("instances.views.utils.refr"), patch("computes.views.utils.refresh_instance_database"), patch(
|
||||
"instances.models.wvmInstance", side_effect=self.proxy
|
||||
):
|
||||
patches = [patch.object(Compute, k, new_callable=PropertyMock, return_value=v) for k, v in compute_patches.items()]
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
response = self.client.get(url)
|
||||
finally:
|
||||
for p in patches:
|
||||
p.stop()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
return response
|
||||
calls = Counter()
|
||||
|
||||
def assert_one_info_per_vm(self, response):
|
||||
self.assertEqual(len(self.domains), 2, "one proxy per VM row")
|
||||
for domain in self.domains:
|
||||
self.assertEqual(domain.info_calls, 1)
|
||||
self.assertEqual(domain.xml_calls, 1, "only the title reads the XML")
|
||||
self.assertContains(response, "2048 MiB", count=2)
|
||||
def counted(cls, name):
|
||||
real = getattr(cls, name)
|
||||
|
||||
def call(*args, **kwargs):
|
||||
calls[name] += 1
|
||||
return real(*args, **kwargs)
|
||||
|
||||
return patch.object(cls, name, call)
|
||||
|
||||
patches = [patch("instances.views.utils.refr"), patch("computes.views.utils.refresh_instance_database"),
|
||||
patch("vrtManager.connection.connection_manager.get_connection", return_value=self.conn),
|
||||
counted(libvirt.virDomain, "info"), counted(libvirt.virDomain, "XMLDesc"),
|
||||
counted(libvirt.virConnect, "lookupByUUIDString")]
|
||||
# the grouped list's host line; the test driver has no memory stats
|
||||
patches += [patch.object(Compute, k, new_callable=PropertyMock, return_value=v)
|
||||
for k, v in {"cpu_count": 1, "ram_size": 1024, "ram_usage": 0}.items()]
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
response = self.client.get(url)
|
||||
finally:
|
||||
for p in reversed(patches):
|
||||
p.stop()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
return response, calls
|
||||
|
||||
def assert_read_once_a_host(self, url):
|
||||
response, calls = self.get(url)
|
||||
# every domain of the host once (the test driver's own, and other tests', too)
|
||||
self.assertEqual(calls, {"XMLDesc": len(self.conn.listAllDomains())})
|
||||
self.assertContains(response, "2048 MiB", count=len(VMS))
|
||||
self.assertContains(response, "Active")
|
||||
self.assertContains(response, "vm-a title")
|
||||
|
||||
def test_nongrouped_index(self):
|
||||
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="nongrouped")
|
||||
self.assert_one_info_per_vm(self.get(reverse("instances:index")))
|
||||
self.assert_read_once_a_host(reverse("instances:index"))
|
||||
|
||||
def test_grouped_index(self):
|
||||
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="grouped")
|
||||
self.assert_one_info_per_vm(self.get(reverse("instances:index")))
|
||||
self.assert_read_once_a_host(reverse("instances:index"))
|
||||
|
||||
def test_compute_instances(self):
|
||||
# this page shows the maximum memory, as before
|
||||
# the VM's current memory, as on the Instances page, not its maximum
|
||||
self.assert_one_info_per_vm(self.get(reverse("instances", args=[self.compute.id])))
|
||||
self.assert_read_once_a_host(reverse("instances", args=[self.compute.id]))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue