Skip to content

Commit 3913a88

Browse files
committed
Basic workload generation and saving to CSV
1 parent e247294 commit 3913a88

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ requests = generate_workload(pool, rate_fn, duration=3600)
6969
```
7070

7171
See `examples/` for more detailed examples:
72+
- `basic_usage.py`: Basic workload generation and saving to CSV
7273
- `generate_custom.py`: Custom workload patterns
7374
- `generate_realistic.py`: Realistic workload generation
7475
- `generate_advanced.py`: Multimodal and reasoning workloads

examples/basic_usage.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Example: Generate a workload and save it as a CSV file.
4+
5+
This script demonstrates how to:
6+
1. Basic usage of load client data and generate a workload
7+
2. Save the generated workload to a CSV file
8+
"""
9+
10+
from servegen import Category, ClientPool
11+
from servegen.construct import generate_workload
12+
from servegen.utils import save_requests_to_csv
13+
14+
15+
def main():
16+
# Load client data
17+
print("Loading client pool...")
18+
pool = ClientPool(Category.LANGUAGE, "m-large")
19+
print(f"Loaded {len(pool.clients)} clients.")
20+
21+
# Generate workload
22+
print("Generating workload...")
23+
rate_fn = {0: 100.0, 600: 150.0} # requests per second
24+
requests = generate_workload(pool, rate_fn, duration=1200)
25+
print(f"Generated {len(requests)} requests.")
26+
27+
# Save to CSV
28+
output_file = "workload.csv"
29+
print(f"Saving to {output_file}...")
30+
save_requests_to_csv(requests, output_file)
31+
print(f"Saved to {output_file}.")
32+
33+
# Print first few rows
34+
print("\nFirst 10 rows of the generated workload:")
35+
with open(output_file, "r") as f:
36+
lines = f.readlines()
37+
for line in lines[:11]: # header + 10 rows
38+
print(line.strip())
39+
40+
41+
if __name__ == "__main__":
42+
main()

servegen/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
from servegen.workload_types import Category, ArrivalPat
33
from servegen.clientpool import ClientPool, Client
44
from servegen.construct import generate_workload
5+
from servegen.utils import save_requests_to_csv
56

6-
__all__ = ['Category', 'ArrivalPat', 'ClientPool', 'Client', 'generate_workload']
7+
__all__ = [
8+
"Category",
9+
"ArrivalPat",
10+
"ClientPool",
11+
"Client",
12+
"generate_workload",
13+
"save_requests_to_csv",
14+
]

servegen/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Dict, List, Optional, Union, Tuple, Any
22
import numpy as np
3+
import csv
34
from servegen.clientpool import ClientPool, ClientPoolView, ClientWindow
5+
from servegen.construct import Request
46

57
def get_constant_rate_fn(
68
pool: Union[ClientPool, ClientPoolView],
@@ -132,3 +134,32 @@ def sample_from_cdf(
132134
indices = np.clip(indices, 0, len(values) - 1)
133135

134136
return values[indices]
137+
138+
def save_requests_to_csv(
139+
requests: List[Request],
140+
filename: str
141+
) -> None:
142+
"""Save list of Request objects to a CSV file.
143+
144+
Args:
145+
requests: List of Request objects to save.
146+
filename: Path to the output CSV file.
147+
148+
Raises:
149+
ValueError: If no requests are provided.
150+
"""
151+
if not requests:
152+
raise ValueError("No requests to save.")
153+
154+
with open(filename, "w", newline="") as csvfile:
155+
fieldnames = ["request_id", "timestamp"] + list(requests[0].data.keys())
156+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
157+
158+
writer.writeheader()
159+
for req in requests:
160+
row = {
161+
"request_id": req.request_id,
162+
"timestamp": req.timestamp,
163+
**req.data # Unpack the data dictionary
164+
}
165+
writer.writerow(row)

0 commit comments

Comments
 (0)