|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package replay |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + "github.com/Azure/kperf/api/types" |
| 14 | + "github.com/Azure/kperf/cmd/kperf/commands/utils" |
| 15 | + "github.com/Azure/kperf/metrics" |
| 16 | + "github.com/Azure/kperf/replay" |
| 17 | + |
| 18 | + "github.com/urfave/cli" |
| 19 | +) |
| 20 | + |
| 21 | +var runCommand = cli.Command{ |
| 22 | + Name: "run", |
| 23 | + Usage: "Run a replay test from a profile (local mode)", |
| 24 | + Flags: []cli.Flag{ |
| 25 | + cli.StringFlag{ |
| 26 | + Name: "kubeconfig", |
| 27 | + Usage: "Path to the kubeconfig file", |
| 28 | + Value: utils.DefaultKubeConfigPath, |
| 29 | + }, |
| 30 | + cli.StringFlag{ |
| 31 | + Name: "config", |
| 32 | + Usage: "Path to the replay profile file (YAML, supports .yaml.gz for gzip compressed)", |
| 33 | + Required: true, |
| 34 | + }, |
| 35 | + cli.StringFlag{ |
| 36 | + Name: "result", |
| 37 | + Usage: "Path to the file which stores results (JSON)", |
| 38 | + }, |
| 39 | + cli.BoolFlag{ |
| 40 | + Name: "raw-data", |
| 41 | + Usage: "Include raw latency data in result", |
| 42 | + }, |
| 43 | + }, |
| 44 | + Action: func(cliCtx *cli.Context) error { |
| 45 | + kubeCfgPath := cliCtx.String("kubeconfig") |
| 46 | + configPath := cliCtx.String("config") |
| 47 | + |
| 48 | + // Load the replay profile |
| 49 | + profile, err := replay.LoadProfile(context.Background(), configPath) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("failed to load profile: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Printf("Loaded replay profile: %s\n", profile.Description) |
| 55 | + fmt.Printf(" Total requests: %d\n", len(profile.Requests)) |
| 56 | + fmt.Printf(" Duration: %dms\n", profile.Duration()) |
| 57 | + fmt.Printf(" Runner count: %d\n", profile.Spec.RunnerCount) |
| 58 | + |
| 59 | + // Run the replay |
| 60 | + result, err := replay.Schedule(context.Background(), kubeCfgPath, profile) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to run replay: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Print summary to stdout |
| 66 | + fmt.Printf("\nReplay completed:\n") |
| 67 | + fmt.Printf(" Total requests: %d\n", result.TotalRequests) |
| 68 | + fmt.Printf(" Requests run: %d\n", result.TotalRun) |
| 69 | + fmt.Printf(" Requests failed: %d\n", result.TotalFailed) |
| 70 | + fmt.Printf(" Duration: %s\n", result.Duration) |
| 71 | + fmt.Printf(" Bytes received: %d\n", result.Aggregated.TotalReceivedBytes) |
| 72 | + |
| 73 | + // Write result to file or stdout |
| 74 | + var f *os.File = os.Stdout |
| 75 | + outputFilePath := cliCtx.String("result") |
| 76 | + if outputFilePath != "" { |
| 77 | + outputFileDir := filepath.Dir(outputFilePath) |
| 78 | + |
| 79 | + if _, err := os.Stat(outputFileDir); os.IsNotExist(err) { |
| 80 | + if err := os.MkdirAll(outputFileDir, 0750); err != nil { |
| 81 | + return fmt.Errorf("failed to create output directory %s: %w", outputFileDir, err) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + f, err = os.Create(outputFilePath) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to create result file: %w", err) |
| 88 | + } |
| 89 | + defer f.Close() |
| 90 | + } |
| 91 | + |
| 92 | + rawDataFlagIncluded := cliCtx.Bool("raw-data") |
| 93 | + |
| 94 | + // Build report |
| 95 | + report := buildReplayReport(result, rawDataFlagIncluded) |
| 96 | + |
| 97 | + // Write JSON |
| 98 | + encoder := json.NewEncoder(f) |
| 99 | + encoder.SetIndent("", " ") |
| 100 | + if err := encoder.Encode(report); err != nil { |
| 101 | + return fmt.Errorf("failed to encode result: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | + }, |
| 106 | +} |
| 107 | + |
| 108 | +// ReplayReport is the output format for replay results. |
| 109 | +type ReplayReport struct { |
| 110 | + types.RunnerMetricReport |
| 111 | + // RunnerCount is the number of runners used. |
| 112 | + RunnerCount int `json:"runnerCount"` |
| 113 | + // TotalRun is the number of requests actually executed. |
| 114 | + TotalRun int `json:"totalRun"` |
| 115 | + // TotalFailed is the number of requests that failed. |
| 116 | + TotalFailed int `json:"totalFailed"` |
| 117 | +} |
| 118 | + |
| 119 | +// buildReplayReport builds a ReplayReport from ScheduleResult. |
| 120 | +func buildReplayReport(result *replay.ScheduleResult, includeRawData bool) ReplayReport { |
| 121 | + report := ReplayReport{ |
| 122 | + RunnerMetricReport: types.RunnerMetricReport{ |
| 123 | + Total: result.TotalRequests, |
| 124 | + ErrorStats: metrics.BuildErrorStatsGroupByType(result.Aggregated.Errors), |
| 125 | + Duration: result.Duration.String(), |
| 126 | + TotalReceivedBytes: result.Aggregated.TotalReceivedBytes, |
| 127 | + PercentileLatenciesByURL: map[string][][2]float64{}, |
| 128 | + }, |
| 129 | + RunnerCount: len(result.RunnerResults), |
| 130 | + TotalRun: result.TotalRun, |
| 131 | + TotalFailed: result.TotalFailed, |
| 132 | + } |
| 133 | + |
| 134 | + metrics.BuildPercentileLatenciesReport(&report.RunnerMetricReport, result.Aggregated.LatenciesByURL, includeRawData, result.Aggregated.Errors) |
| 135 | + |
| 136 | + return report |
| 137 | +} |
0 commit comments