github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/experiments/internal/pkg/runner/runner.go (about) 1 // Copyright 2023 Google LLC 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 // Package runner is used to create new experiments using experiment texproto files 16 package runner 17 18 import ( 19 "context" 20 "fmt" 21 "os" 22 "path/filepath" 23 "time" 24 25 "github.com/bazelbuild/reclient/experiments/internal/pkg/experiment" 26 "github.com/bazelbuild/reclient/experiments/internal/pkg/gcs" 27 28 "google.golang.org/protobuf/encoding/prototext" 29 30 epb "github.com/bazelbuild/reclient/experiments/api/experiment" 31 ) 32 33 // RunExperiment will create a new experiment based on given experiment textproto 34 func RunExperiment(expDefPath string, dateSuffix string, gcpProject string, resBucket string, logFrequency int) (string, error) { 35 exp, err := os.ReadFile(expDefPath) 36 if err != nil { 37 return "", fmt.Errorf("Failed to read experiment definition file: %v", err) 38 } 39 expPb := &epb.Experiment{} 40 if err := prototext.Unmarshal(exp, expPb); err != nil { 41 return "", fmt.Errorf("Failed to parse experiment definition file: %v", err) 42 } 43 date := time.Now().Format("20060102-1504") 44 if dateSuffix != "" { 45 date = dateSuffix 46 } 47 e, err := experiment.NewExperiment(expPb, filepath.Dir(expDefPath), gcpProject, date, resBucket, logFrequency) 48 if err != nil { 49 return "", fmt.Errorf("Failed to create experiment: %v", err) 50 } 51 ctx := context.Background() 52 53 expName := expPb.GetName() + "_" + date 54 dest := fmt.Sprintf("gs://%v/%v/", resBucket, expName) 55 if err := gcs.Copy(ctx, expDefPath, dest); err != nil { 56 return "", fmt.Errorf("Failed to upload experiment definition: %v", err) 57 } 58 if err := e.Run(ctx); err != nil { 59 return "", fmt.Errorf("Failed to run experiment: %v", err) 60 } 61 return expName, nil 62 }