5. Performance: host vs guest
“My VM is slow” is not a measurement. In this page you learn to measure the CPU, the memory and the disk, first on the host and then in the guest, and to explain the difference with numbers. This is the last piece of the unit: it connects what you know about hypervisors, resources and snapshots.
1. What do we expect?
Sección titulada «1. What do we expect?»A virtual machine runs on the same hardware as the host, so it cannot be faster. The question is how much slower it is, and in which kind of work.
| Resource | What happens in a VM | Typical difference |
|---|---|---|
| CPU | With VT-x or AMD-V, most instructions run directly on the processor. | Very small; often close to native speed (for example 90 % or more) |
| Memory (RAM) | The guest uses a part of the host memory. Nested paging (EPT/NPT) makes the address translation fast. | Small, if the host has enough memory |
| Disk | Every disk request goes through a virtual controller and the host file system. | The biggest difference; it depends a lot on the disk and on the other VMs |
| Network | Goes through a virtual card and the host network. | Small for normal use |
The numbers are only orientation: a result depends on your processor, your disk, the other programs running and the VM settings. That is why we measure instead of guessing.
2. What makes a VM slower or faster?
Sección titulada «2. What makes a VM slower or faster?»| Factor | Effect | What to do |
|---|---|---|
| Disk type | An SSD closes most of the gap; a mechanical disk makes disk work in the guest slow. | Store the VM on an SSD |
| Hardware virtualization | Without VT-x or AMD-V, the VM is much slower or does not start. | Enable it in the UEFI/BIOS |
| Too many vCPUs | If you give more vCPUs than the host can run at the same time, the VM waits for free cores. | Use 1-2 vCPUs, never more than the host cores |
| Too little RAM in the guest | The guest starts using the disk as memory (swap), which is very slow. | Give the guest what it needs |
| Too much RAM for the guest | The host runs out of memory and swaps, and all VMs suffer. | Leave 2-3 GB or more for the host |
| Long snapshot chains | Each disk read may have to look through several layers. | Keep few snapshots; delete the old ones |
| Disk allocation | A dynamic disk has to grow while the guest writes. A fixed disk is more predictable. | Use fixed size for performance tests |
| Other VMs and programs | They compete for CPU, memory and disk. | Do not run other heavy tasks while you measure |
| Guest Additions | Without them the graphics and the mouse feel slow, even if the CPU is fine. | Install them |
| Controller | SATA/AHCI is a good general choice. | Keep the default unless you have a reason to change it |
3. How to measure fairly
Sección titulada «3. How to measure fairly»A good comparison needs a method. Follow these rules every time:
-
Same test, same size. Use the same program and the same parameters on the host and in the guest.
-
Quiet system. Close browsers, updates and other VMs. Plug in a laptop, because some laptops slow down on battery.
-
Repeat. Run each test three times and use the median (the middle result). The first run is often different because of caches.
-
Equal conditions for the CPU test. If the guest has 2 vCPUs, test the host with 2 threads too (and, as an extra, with all its cores).
-
Write everything down. Note the host (CPU, RAM, disk type), the VM (vCPUs, RAM, disk) and the commands. Without this, nobody can repeat or understand your results.
-
Calculate the relative performance. For results where more is better (operations per second, MB/s):
relative performance (%) = guest result ÷ host result × 100
For results where less is better (seconds), use host time ÷ guest time × 100.
Know your system first
Sección titulada «Know your system first»In a Linux host or guest:
lscpu # processor model, cores, virtualization supportnproc # number of CPUs the system can usefree -h # total and free memorylsblk -d -o NAME,SIZE,ROTA # disks; ROTA 1 = mechanical, 0 = SSD (a virtual disk may show either)systemd-detect-virt # inside a VM it names the hypervisor (VirtualBox shows "oracle")4. Tests on Linux
Sección titulada «4. Tests on Linux»You can install a benchmark tool called sysbench:
sudo apt install sysbenchsysbench cpu --cpu-max-prime=20000 --threads=1 runLook at events per second (more is better) or at the total time (less is better). Repeat with --threads=2, or with the same number of vCPUs as your guest.
sysbench memory --memory-block-size=1M --memory-total-size=10G runLook at the MiB transferred per second (more is better).
You can measure the writing speed with dd, a standard tool that copies data. This command writes a file of 1 GB, skipping the memory cache, and then deletes it:
dd if=/dev/zero of=./test.tmp bs=1M count=1024 oflag=direct status=progressrm ./test.tmpdd prints the speed in MB/s at the end. If you want to test random reads and writes with sysbench:
sysbench fileio --file-total-size=2G preparesysbench fileio --file-total-size=2G --file-test-mode=rndrw --time=30 runsysbench fileio --file-total-size=2G cleanupSimple tests without installing anything
Sección titulada «Simple tests without installing anything»If you cannot install programs, you can measure the time of a task:
time sha256sum bigfile.iso # CPU and disk readingtime cp bigfile.iso copy.iso # disk reading and writingtime shows real (what you wait), user and sys. For the comparison use real.
5. Tests on Windows
Sección titulada «5. Tests on Windows»On a Windows host or guest you can use the free benchmark of 7-Zip (menu Tools → Benchmark, or 7z b in a terminal) to compare CPU and memory. For disk, measure the time of copying a big file with PowerShell:
Measure-Command { Copy-Item .\big.bin .\copy.bin }(The TotalSeconds value is the time.) Use the same file on the host and in the guest.
6. Reading and explaining the results
Sección titulada «6. Reading and explaining the results»A result table might look like this (the numbers here are an example, not a rule):
| Test | Host | Guest | Relative performance |
|---|---|---|---|
| CPU, 1 thread (events/s) | 1200 | 1090 | 91 % |
| Memory (MiB/s) | 9500 | 8300 | 87 % |
| Disk write (MB/s) | 480 | 310 | 65 % |
How to explain it: the CPU is close to native because of VT-x; the memory is a little lower; the disk is the weakest because every write goes through the virtual controller and the host file system, and in this case the guest disk was dynamic.
When you write a report:
- Describe the system and the method.
- Give the table with the results and the relative performance.
- Write a conclusion that explains the biggest difference with one or two causes from section 2.
- Say what you would change to improve it and why.
Check what you have learned
Sección titulada «Check what you have learned»-
In which resource do you normally find the biggest performance difference between host and guest? Why?
Answer
In the disk. Every request goes through a virtual controller and the host file system, and the disk is shared with the host and the other VMs.
-
Your CPU test gives 800 events/s on the host and 720 events/s in the guest. What is the relative performance?
Answer
720 ÷ 800 × 100 = 90 %.
-
A student gives 8 vCPUs to a VM on a host with 4 cores and the VM is slower. Why?
Answer
The hypervisor cannot run 8 virtual processors at the same time on 4 real cores, so the VM waits for free cores. It is better to use 1-2 vCPUs, and never more than the host cores.
-
Why do you repeat each test three times and use the median?
Answer
Because one result can be affected by caches or by background tasks. The median of three runs is more reliable.
-
Give three actions that can improve the performance of a VM.
Answer
For example: store the VM on an SSD, enable VT-x/AMD-V, keep the number of vCPUs reasonable, give enough RAM but leave memory for the host, install Guest Additions, and delete old snapshots.
Glossary
Sección titulada «Glossary»| English | Castellano |
|---|---|
| benchmark | prueba de rendimiento |
| performance | rendimiento |
| throughput | velocidad de transferencia |
| latency | latencia |
| median | mediana |
| thread | hilo |
| swap | memoria de intercambio |
| native | nativo |
| relative performance | rendimiento relativo |
| cache | caché |
Esta web no usa cookies ni recoge datos personales. Tu navegador solo guarda, en tu propio equipo, algunas preferencias: el tema claro u oscuro, el estado del menú y, en los laboratorios, la última pestaña abierta y el tamaño de letra.