|
| 1 | +# Copyright 2025 PerfKitBenchmarker Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Run memtier_benchmark against a KVrocks cluster.""" |
| 16 | + |
| 17 | +import logging |
| 18 | +from typing import Any, Dict, List |
| 19 | + |
| 20 | +from absl import flags |
| 21 | +from perfkitbenchmarker import background_tasks |
| 22 | +from perfkitbenchmarker import benchmark_spec |
| 23 | +from perfkitbenchmarker import configs |
| 24 | +from perfkitbenchmarker import errors |
| 25 | +from perfkitbenchmarker import sample |
| 26 | +from perfkitbenchmarker.linux_packages import kvrocks_server |
| 27 | +from perfkitbenchmarker.linux_packages import memtier |
| 28 | +from perfkitbenchmarker.linux_packages import prometheus |
| 29 | + |
| 30 | +FLAGS = flags.FLAGS |
| 31 | + |
| 32 | +BENCHMARK_NAME = 'kvrocks_memtier' |
| 33 | +BENCHMARK_CONFIG = """ |
| 34 | +kvrocks_memtier: |
| 35 | + description: > |
| 36 | + Run memtier_benchmark against KVrocks. |
| 37 | + vm_groups: |
| 38 | + servers: |
| 39 | + vm_spec: |
| 40 | + GCP: |
| 41 | + machine_type: c3-standard-44-lssd |
| 42 | + vm_count: 3 |
| 43 | + os_type: rocky-9 |
| 44 | + disk_spec: |
| 45 | + GCP: |
| 46 | + disk_type: local |
| 47 | + num_striped_disks: 8 |
| 48 | + mount_point: /scratch |
| 49 | + AWS: |
| 50 | + disk_type: local |
| 51 | + num_striped_disks: 8 |
| 52 | + mount_point: /scratch |
| 53 | + Azure: |
| 54 | + disk_type: local |
| 55 | + num_striped_disks: 8 |
| 56 | + mount_point: /scratch |
| 57 | + clients: |
| 58 | + vm_spec: |
| 59 | + GCP: |
| 60 | + machine_type: n2-standard-64 |
| 61 | + vm_count: 1 |
| 62 | + os_type: rocky-9 |
| 63 | + disk_spec: |
| 64 | + GCP: |
| 65 | + disk_type: local |
| 66 | + num_striped_disks: 8 |
| 67 | + mount_point: /scratch |
| 68 | + AWS: |
| 69 | + disk_type: local |
| 70 | + num_striped_disks: 8 |
| 71 | + mount_point: /scratch |
| 72 | + Azure: |
| 73 | + disk_type: local |
| 74 | + num_striped_disks: 8 |
| 75 | + mount_point: /scratch |
| 76 | +""" |
| 77 | + |
| 78 | + |
| 79 | +_BenchmarkSpec = benchmark_spec.BenchmarkSpec |
| 80 | + |
| 81 | + |
| 82 | +def GetConfig(user_config: Dict[str, Any]) -> Dict[str, Any]: |
| 83 | + """Load and return benchmark config spec.""" |
| 84 | + return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME) |
| 85 | + |
| 86 | + |
| 87 | +def CheckPrerequisites(_): |
| 88 | + """Verifies that benchmark setup is correct.""" |
| 89 | + pass |
| 90 | + |
| 91 | + |
| 92 | +def Prepare(bm_spec: _BenchmarkSpec) -> None: |
| 93 | + """Install and set up KVrocks and client tools.""" |
| 94 | + server_vms = bm_spec.vm_groups['servers'] |
| 95 | + client_vms = bm_spec.vm_groups['clients'] |
| 96 | + vms = server_vms + client_vms |
| 97 | + |
| 98 | + background_tasks.RunThreaded(lambda vm: vm.Install('kvrocks_server'), vms) |
| 99 | + background_tasks.RunThreaded(lambda vm: vm.Install('memtier'), client_vms) |
| 100 | + |
| 101 | + def _InstallAndStartPrometheus(vm): |
| 102 | + vm.Install('prometheus') |
| 103 | + prometheus.ConfigureAndStart(vm, server_vms) |
| 104 | + |
| 105 | + background_tasks.RunThreaded(_InstallAndStartPrometheus, client_vms) |
| 106 | + |
| 107 | + # Skipping preconditioning for now. |
| 108 | + # TODO(user): Add preconditioning. |
| 109 | + kvrocks_server.StartCluster(server_vms) |
| 110 | + |
| 111 | + def _StartExporter(vm): |
| 112 | + kvrocks_server.StartExporter(vm) |
| 113 | + |
| 114 | + # The prometheus server on the client is configured to scrape exporters |
| 115 | + # from the server VMs. |
| 116 | + background_tasks.RunThreaded(_StartExporter, server_vms) |
| 117 | + |
| 118 | + prometheus.WaitUntilHealthy(client_vms[0]) |
| 119 | + |
| 120 | + # Check cluster status |
| 121 | + master_vm = server_vms[0] |
| 122 | + |
| 123 | + try: |
| 124 | + cluster_info, _ = kvrocks_server.RunRedisCommand( |
| 125 | + master_vm, '-c cluster info' |
| 126 | + ) |
| 127 | + logging.info('KVrocks cluster info: %s', cluster_info) |
| 128 | + cluster_nodes, _ = kvrocks_server.RunRedisCommand( |
| 129 | + master_vm, '-c cluster nodes' |
| 130 | + ) |
| 131 | + logging.info('KVrocks cluster nodes: %s', cluster_nodes) |
| 132 | + except errors.VirtualMachine.RemoteCommandError as e: |
| 133 | + logging.warning('KVrocks cluster not ready or redis commands failed: %s', e) |
| 134 | + |
| 135 | + |
| 136 | +def Run(bm_spec: _BenchmarkSpec) -> List[sample.Sample]: |
| 137 | + """Run the memtier benchmark and collect results.""" |
| 138 | + client_vms = bm_spec.vm_groups['clients'] |
| 139 | + server_vms = bm_spec.vm_groups['servers'] |
| 140 | + master_vm = server_vms[0] |
| 141 | + ip = master_vm.internal_ip |
| 142 | + ports = list(kvrocks_server.INSTANCE_TO_PORT_MAP.values()) |
| 143 | + |
| 144 | + logging.info('Starting memtier runs...') |
| 145 | + try: |
| 146 | + results = memtier.RunOverAllThreadsPipelinesAndClients( |
| 147 | + client_vms, ip, [ports[0]], kvrocks_server.GetPassword() |
| 148 | + ) |
| 149 | + except errors.VirtualMachine.RemoteCommandError as e: |
| 150 | + logging.exception('Memtier run failed: %s', e) |
| 151 | + results = [] |
| 152 | + logging.info('Memtier runs finished. Results: %s', results) |
| 153 | + |
| 154 | + metadata = kvrocks_server.GetMetadata() |
| 155 | + for res in results: |
| 156 | + res.metadata.update(metadata) |
| 157 | + |
| 158 | + # Query prometheus stats from the first client |
| 159 | + client_vm = client_vms[0] |
| 160 | + |
| 161 | + slowlog_total = prometheus.RunQuery(client_vm, 'sum(kvrocks_slowlog_total)') |
| 162 | + commands_total = prometheus.RunQuery( |
| 163 | + client_vm, 'sum(kvrocks_commands_total{cmd="hmset"})' |
| 164 | + ) |
| 165 | + slowlog_ratio = prometheus.RunQuery( |
| 166 | + client_vm, |
| 167 | + 'sum(kvrocks_slowlog_total) / sum(kvrocks_commands_total{cmd="hmset"})', |
| 168 | + ) |
| 169 | + |
| 170 | + logging.info('Slowlog total: %s', slowlog_total) |
| 171 | + logging.info('Commands total: %s', commands_total) |
| 172 | + logging.info('Slowlog ratio: %s', slowlog_ratio) |
| 173 | + |
| 174 | + if slowlog_total is not None: |
| 175 | + results.append( |
| 176 | + sample.Sample('KVRocks Slowlog Total', slowlog_total, 'count', metadata) |
| 177 | + ) |
| 178 | + |
| 179 | + if commands_total is not None: |
| 180 | + results.append( |
| 181 | + sample.Sample( |
| 182 | + 'KVRocks Commands Total HMSET', |
| 183 | + commands_total, |
| 184 | + 'count', |
| 185 | + metadata, |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + if slowlog_ratio is not None: |
| 190 | + results.append( |
| 191 | + sample.Sample('KVRocks Slowlog Ratio', slowlog_ratio, 'ratio', metadata) |
| 192 | + ) |
| 193 | + |
| 194 | + return results |
| 195 | + |
| 196 | + |
| 197 | +def Cleanup(bm_spec: _BenchmarkSpec) -> None: |
| 198 | + """Cleanup resources.""" |
| 199 | + del bm_spec |
0 commit comments