Skip to content

Commit cecfac5

Browse files
authored
feat(k6): pass sql file name to ts as var (#25)
Closes RNDSTROPPY-52
1 parent 8c35903 commit cecfac5

File tree

5 files changed

+35
-54
lines changed

5 files changed

+35
-54
lines changed

internal/runner/script_extractor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ func prepareVMEnvironment(
306306
return fmt.Errorf("failed to set __ENV: %w", err)
307307
}
308308

309+
if err := vm.Set("__SQL_FILE", ""); err != nil {
310+
return fmt.Errorf("failed to set __SQL_FILE: %w", err)
311+
}
312+
309313
return nil
310314
}
311315

internal/runner/script_runner.go

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewScriptRunner(scriptPath, sqlPath string) (*ScriptRunner, error) {
3939
// Validate SQL path if provided
4040
if sqlPath != "" {
4141
if _, err := os.Stat(sqlPath); err != nil {
42-
return nil, fmt.Errorf("SQL file not found: %w", err)
42+
return nil, fmt.Errorf("SQL file %q not found: %w", sqlPath, err)
4343
}
4444
}
4545

@@ -81,57 +81,47 @@ func (r *ScriptRunner) Run(ctx context.Context) error {
8181
r.logger.Info("Working directory", zap.String("path", tempDir))
8282

8383
// Copy static files to temp directory
84-
if err := r.copyStaticFiles(tempDir); err != nil {
84+
if err := static.CopyAllStaticFilesToPath(tempDir, common.FileMode); err != nil {
8585
return fmt.Errorf("failed to copy static files: %w", err)
8686
}
8787

8888
// Copy user's script to temp directory
8989
scriptName := filepath.Base(r.scriptPath)
90-
if err := r.copyFile(r.scriptPath, path.Join(tempDir, scriptName)); err != nil {
91-
return fmt.Errorf("failed to copy script: %w", err)
92-
}
90+
sqlName := filepath.Base(r.sqlPath)
9391

94-
// Copy SQL file if explicitly provided
95-
if r.sqlPath != "" {
96-
sqlName := filepath.Base(r.sqlPath)
97-
if err := r.copyFile(r.sqlPath, path.Join(tempDir, sqlName)); err != nil {
98-
return fmt.Errorf("failed to copy SQL file: %w", err)
92+
if r.sqlPath == "" { // copy single ts file
93+
if err := copyFile(r.scriptPath, path.Join(tempDir, scriptName)); err != nil {
94+
return fmt.Errorf("failed to copy script: %w", err)
95+
}
96+
} else { // copy ts + sql + add name in variable
97+
if err := copyFileWithPrepend(
98+
r.scriptPath,
99+
path.Join(tempDir, scriptName),
100+
fmt.Sprintf(`const __SQL_FILE = %q;`, sqlName),
101+
); err != nil {
102+
return fmt.Errorf("failed to copy script: %w", err)
99103
}
100-
}
101104

102-
// Copy all SQL files from script's directory (for scripts that use open())
103-
if err := r.copySQLFilesFromScriptDir(tempDir); err != nil {
104-
r.logger.Warn("Could not copy SQL files from script directory", zap.Error(err))
105+
if err := copyFile(r.sqlPath, path.Join(tempDir, sqlName)); err != nil {
106+
return fmt.Errorf("failed to copy SQL file %q: %w", sqlName, err)
107+
}
105108
}
106109

107-
// Build k6 arguments
108110
args := []string{"run", scriptName}
109111

110-
// Build environment variables
111112
envs := r.buildEnvVars()
112113

113-
// Add OTLP exporter args if configured
114114
if r.config.GlobalConfig.GetExporter().GetOtlpExport() != nil {
115115
args, envs = r.addOtelExportArgs(args, envs)
116116
}
117117

118118
r.logger.Debug("Running k6", zap.Strings("args", args))
119119

120-
// Run k6
121120
return r.runK6Binary(ctx, tempDir, args, envs)
122121
}
123122

124-
// copyStaticFiles copies required static files to the temp directory.
125-
func (r *ScriptRunner) copyStaticFiles(tempDir string) error {
126-
return static.CopyStaticFilesToPath(
127-
tempDir,
128-
common.FileMode,
129-
static.StaticFiles...,
130-
)
131-
}
132-
133123
// copyFile copies a file from src to dst.
134-
func (r *ScriptRunner) copyFile(src, dst string) error {
124+
func copyFile(src, dst string) error {
135125
data, err := os.ReadFile(src)
136126
if err != nil {
137127
return err
@@ -140,31 +130,13 @@ func (r *ScriptRunner) copyFile(src, dst string) error {
140130
return os.WriteFile(dst, data, common.FileMode)
141131
}
142132

143-
// copySQLFilesFromScriptDir copies all SQL files from the script's directory.
144-
func (r *ScriptRunner) copySQLFilesFromScriptDir(tempDir string) error {
145-
scriptDir := filepath.Dir(r.scriptPath)
146-
147-
entries, err := os.ReadDir(scriptDir)
133+
func copyFileWithPrepend(src, dst, prepend string) error {
134+
data, err := os.ReadFile(src)
148135
if err != nil {
149136
return err
150137
}
151138

152-
for _, entry := range entries {
153-
if entry.IsDir() {
154-
continue
155-
}
156-
157-
if filepath.Ext(entry.Name()) == ".sql" {
158-
srcPath := filepath.Join(scriptDir, entry.Name())
159-
160-
dstPath := path.Join(tempDir, entry.Name())
161-
if err := r.copyFile(srcPath, dstPath); err != nil {
162-
return err
163-
}
164-
}
165-
}
166-
167-
return nil
139+
return os.WriteFile(dst, append([]byte(prepend), data...), common.FileMode)
168140
}
169141

170142
// buildEnvVars builds environment variables for k6 execution.
@@ -226,7 +198,6 @@ func (r *ScriptRunner) addOtelExportArgs(args, envs []string) (argsOut, envsOut
226198
}
227199

228200
// runK6Binary executes the k6 binary.
229-
// TODO: pass sql file name
230201
func (r *ScriptRunner) runK6Binary(ctx context.Context, workdir string, args, envs []string) error {
231202
binaryPath := path.Join(workdir, static.K6PluginFileName.String())
232203

internal/static/embed.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,7 @@ func CopyStaticFilesToPath(targetPath string, perm os.FileMode, files ...FileNam
109109

110110
return nil
111111
}
112+
113+
func CopyAllStaticFilesToPath(targetPath string, perm os.FileMode) error {
114+
return CopyStaticFilesToPath(targetPath, perm, StaticFiles...)
115+
}

workloads/execute_sql/execute_sql.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const options: Options = {
3030
executor: "constant-vus",
3131
exec: "workload",
3232
vus: 10,
33-
duration: "5m",
33+
duration: "1s",
3434
},
3535
},
3636
};
@@ -144,9 +144,10 @@ const workloads: WorkloadDescriptor[] = [
144144
}),
145145
];
146146

147-
// Load and parse SQL file, then update workloads with SQL
147+
declare const __SQL_FILE: string;
148148

149-
const sqlContent = open("tpcb_mini.sql");
149+
// Load and parse SQL file, then update workloads with SQL
150+
const sqlContent = open(__SQL_FILE);
150151
const parsedWorkloads = parse_sql(sqlContent);
151152
update_with_sql(workloads, parsedWorkloads);
152153

workloads/tpcds/tpcds.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ defineConfig({
7777

7878
declare function open(path: string): string; // k6 function to get files content
7979

80-
const content: string = open("query_0.sql"); // TODO: push file name trought go cli argument
80+
declare const __SQL_FILE: string;
81+
const content: string = open(__SQL_FILE);
8182
const parsedQueries = parse_sql(content);
8283

8384
export function setup() {

0 commit comments

Comments
 (0)