storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/examples/profiling.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   *
    19   */
    20  
    21  package main
    22  
    23  import (
    24  	"context"
    25  	"io"
    26  	"log"
    27  	"os"
    28  	"time"
    29  
    30  	"storj.io/minio/pkg/madmin"
    31  )
    32  
    33  func main() {
    34  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    35  	// dummy values, please replace them with original values.
    36  
    37  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    38  	// dummy values, please replace them with original values.
    39  
    40  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    41  	// New returns an MinIO Admin client object.
    42  	madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    43  	if err != nil {
    44  		log.Fatalln(err)
    45  	}
    46  
    47  	profiler := madmin.ProfilerCPU
    48  	log.Println("Starting " + profiler + " profiling..")
    49  
    50  	startResults, err := madmClnt.StartProfiling(context.Background(), profiler)
    51  	if err != nil {
    52  		log.Fatalln(err)
    53  	}
    54  
    55  	for _, result := range startResults {
    56  		if !result.Success {
    57  			log.Printf("Unable to start profiling on node `%s`, reason = `%s`\n", result.NodeName, result.Error)
    58  			continue
    59  		}
    60  		log.Printf("Profiling successfully started on node `%s`\n", result.NodeName)
    61  	}
    62  
    63  	sleep := time.Duration(10)
    64  	time.Sleep(time.Second * sleep)
    65  
    66  	log.Println("Stopping profiling..")
    67  
    68  	profilingData, err := madmClnt.DownloadProfilingData(context.Background())
    69  	if err != nil {
    70  		log.Fatalln(err)
    71  	}
    72  
    73  	profilingFile, err := os.Create("/tmp/profiling-" + string(profiler) + ".zip")
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  
    78  	if _, err := io.Copy(profilingFile, profilingData); err != nil {
    79  		log.Fatal(err)
    80  	}
    81  
    82  	if err := profilingFile.Close(); err != nil {
    83  		log.Fatal(err)
    84  	}
    85  
    86  	if err := profilingData.Close(); err != nil {
    87  		log.Fatal(err)
    88  	}
    89  
    90  	log.Println("Profiling files " + profilingFile.Name() + " successfully downloaded.")
    91  }