github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/experiments/internal/pkg/perfgate/perfgate.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 perfgate aids in uploading experiment metric data to the perfgate tool
    16  // for further analysis and regression detection
    17  package perfgate
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os/exec"
    23  
    24  	"github.com/bazelbuild/reclient/experiments/internal/pkg/gcs"
    25  
    26  	log "github.com/golang/glog"
    27  )
    28  
    29  // RunUploader will upload rbe_metric data from each experiment trial to perfgate.
    30  func RunUploader(expDefPath string, resBucket string,
    31  	expName string, gcpProject string, perfgatePath string, benchmarkFile string) error {
    32  	ctx := context.Background()
    33  	configs, err := gcs.List(ctx, fmt.Sprintf("gs://%v/%v", resBucket, expName))
    34  	if err != nil {
    35  		return fmt.Errorf("Failed to find directory for experiment %v: %v", expName, err)
    36  	}
    37  	for _, c := range configs {
    38  		downloadMetric(ctx, c, perfgatePath, expDefPath, benchmarkFile)
    39  	}
    40  	return nil
    41  }
    42  func downloadMetric(ctx context.Context, path string, perfgatePath string, expDefPath string,
    43  	benchmarkFile string) {
    44  	trials, err := gcs.List(ctx, path)
    45  	if err != nil {
    46  		log.Warningf("Failed to find trials for experiment %v: %v", path, err)
    47  	}
    48  	for _, t := range trials {
    49  		if err := gcs.Copy(ctx, fmt.Sprintf("%vrbe_metrics.pb", t), "/tmp/rbe_metrics.pb"); err != nil {
    50  			log.Warningf("Couldn't find RBE metrics for %v: %v", t, err)
    51  			continue
    52  		}
    53  		if err := gcs.Copy(ctx, fmt.Sprintf("%vtime.txt", t), "/tmp/time.txt"); err != nil {
    54  			log.Warningf("Couldn't find elapsed time for %v: %v", t, err)
    55  			continue
    56  		}
    57  		uploadToPerfgate(ctx, perfgatePath, expDefPath, benchmarkFile)
    58  	}
    59  }
    60  func uploadToPerfgate(ctx context.Context, perfgatePath string, expDefPath string, benchmarkFile string) {
    61  	oe, err := exec.CommandContext(ctx, perfgatePath,
    62  		"--rbe_metric_file=/tmp/rbe_metrics.pb", "--rbe_time_file=/tmp/time.txt",
    63  		"--exp_def_path="+expDefPath,
    64  		"--benchmark_path="+benchmarkFile).CombinedOutput()
    65  	if err != nil {
    66  		log.Warningf("Failed to upload to perfgate: %v, outerr: %v", err, string(oe))
    67  	}
    68  	log.Infof("Uploaded to perfgate: %v", string(oe))
    69  }