Skip to content

Commit 184059d

Browse files
committed
Refactor WIP
1 parent db8bb89 commit 184059d

File tree

6 files changed

+77
-765
lines changed

6 files changed

+77
-765
lines changed

content/learning-paths/servers-and-cloud-computing/go-benchmarking-with-sweet/_benchstat_report.py

Lines changed: 0 additions & 211 deletions
This file was deleted.

content/learning-paths/servers-and-cloud-computing/go-benchmarking-with-sweet/refactor1/rexec_sweet/cli.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .gcp_utils import get_running_instances, choose_instance, get_instance_zone
1313
from .benchmark_runner import BenchmarkRunner
14-
from .config import Config
14+
from .config import Config, COLORS
1515

1616
def parse_args():
1717
"""Parse command line arguments."""
@@ -52,20 +52,20 @@ def select_benchmark(config: Config) -> str:
5252
return benchmark_names[idx-1]
5353
print("Invalid selection, try again.")
5454

55-
def select_instance(prompt_text: str, instance_name: Optional[str] = None) -> Tuple[str, str, str]:
55+
def select_instance(prompt_text: str, instances: List[str], instance_name: Optional[str] = None) -> Tuple[str, str, str]:
5656
"""
5757
Select an instance and get its zone and remote path.
5858
5959
Args:
6060
prompt_text: Text to display when prompting for instance selection
61+
instances: List of available instances
6162
instance_name: Optional pre-selected instance name
6263
6364
Returns:
6465
Tuple of (instance_name, zone, remote_path)
6566
"""
6667
if not instance_name:
6768
print(f"\n{prompt_text}:")
68-
instances = get_running_instances()
6969
if not instances:
7070
print("Error: No running instances found.")
7171
sys.exit(1)
@@ -77,25 +77,45 @@ def select_instance(prompt_text: str, instance_name: Optional[str] = None) -> Tu
7777

7878
return instance_name, zone, remote_path
7979

80-
def use_default_instances() -> bool:
81-
"""Ask if user wants to use the first two instances with default directories."""
82-
choice = input("\nDo you want to run the first two instances found with default install directories? [Y/n]: ").strip().lower()
80+
def display_instances_and_prompt(instances: List[str]) -> bool:
81+
"""Display instance list with first two highlighted and ask if user wants to use them."""
82+
if len(instances) < 2:
83+
print("Error: Need at least two running instances.")
84+
sys.exit(1)
85+
86+
print("\nAvailable instances:")
87+
blue = COLORS['blue']
88+
green = COLORS['green']
89+
reset = COLORS['reset']
90+
91+
for i, name in enumerate(instances):
92+
if i == 0:
93+
print(f"{blue}1. {name} (will be used as first instance){reset}")
94+
elif i == 1:
95+
print(f"{green}2. {name} (will be used as second instance){reset}")
96+
else:
97+
print(f"{i+1}. {name}")
98+
99+
choice = input(f"\nDo you want to run the first two instances found with default install directories? [Y/n]: ").strip().lower()
83100
return choice == "" or choice == "y"
84101

85-
def get_default_instances() -> List[Tuple[str, str, str]]:
102+
def get_default_instances(instances: List[str]) -> List[Tuple[str, str, str]]:
86103
"""Get the first two instances with default directories."""
87-
instances = get_running_instances()
88104
if len(instances) < 2:
89105
print("Error: Need at least two running instances.")
90106
sys.exit(1)
91107

92108
default_path = "~/benchmarks/sweet"
93109
result = []
94110

111+
# Cache zones to avoid multiple API calls
112+
zones = {}
113+
95114
for i in range(2):
96115
instance_name = instances[i]
97-
zone = get_instance_zone(instance_name)
98-
result.append((instance_name, zone, default_path))
116+
if instance_name not in zones:
117+
zones[instance_name] = get_instance_zone(instance_name)
118+
result.append((instance_name, zones[instance_name], default_path))
99119

100120
print(f"\nUsing instances: {instances[0]} and {instances[1]} with default path: {default_path}")
101121
return result
@@ -160,10 +180,13 @@ def main():
160180
# Setup systems for benchmarking
161181
systems = []
162182

183+
# Get instances only once
184+
instances = get_running_instances() if not args.instance1 and not args.instance2 else []
185+
163186
# Check if user wants to use default instances
164-
if not args.instance1 and not args.instance2 and use_default_instances():
187+
if instances and display_instances_and_prompt(instances):
165188
# Get the first two instances with default directories
166-
instance_configs = get_default_instances()
189+
instance_configs = get_default_instances(instances)
167190

168191
for instance_name, zone, remote_path in instance_configs:
169192
systems.append({
@@ -175,7 +198,7 @@ def main():
175198
else:
176199
# Get first instance
177200
instance1_name, zone1, remote_path1 = select_instance(
178-
"Select FIRST instance", args.instance1)
201+
"Select FIRST instance", instances, args.instance1)
179202

180203
systems.append({
181204
"name": instance1_name,
@@ -186,7 +209,7 @@ def main():
186209

187210
# Get second instance
188211
instance2_name, zone2, remote_path2 = select_instance(
189-
"Select SECOND instance", args.instance2)
212+
"Select SECOND instance", instances, args.instance2)
190213

191214
systems.append({
192215
"name": instance2_name,

content/learning-paths/servers-and-cloud-computing/go-benchmarking-with-sweet/refactor1/rexec_sweet/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"etcd": 'sweet run -count 10 -run="etcd" config.toml',
2424
"go-build": 'sweet run -count 10 -run="go-build" config.toml',
2525
"gopher-lua": 'sweet run -count 10 -run="gopher-lua" config.toml',
26-
"gvisor": 'sweet run -count 10 -run="gvisor" config.toml',
2726
"markdown": 'sweet run -count 10 -run="markdown" config.toml',
2827
"tile38": 'sweet run -count 10 -run="tile38" config.toml',
2928
}

0 commit comments

Comments
 (0)