github.com/minio/madmin-go@v1.7.5/examples/profiling.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  //
     5  // MinIO Object Storage (c) 2021 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  package main
    21  
    22  import (
    23  	"context"
    24  	"io"
    25  	"log"
    26  	"os"
    27  	"time"
    28  
    29  	"github.com/minio/madmin-go"
    30  )
    31  
    32  func main() {
    33  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    34  	// dummy values, please replace them with original values.
    35  
    36  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    37  	// dummy values, please replace them with original values.
    38  
    39  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    40  	// New returns an MinIO Admin client object.
    41  	madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    42  	if err != nil {
    43  		log.Fatalln(err)
    44  	}
    45  
    46  	profiler := madmin.ProfilerCPU
    47  	log.Println("Starting " + profiler + " profiling..")
    48  
    49  	startResults, err := madmClnt.StartProfiling(context.Background(), profiler)
    50  	if err != nil {
    51  		log.Fatalln(err)
    52  	}
    53  
    54  	for _, result := range startResults {
    55  		if !result.Success {
    56  			log.Printf("Unable to start profiling on node `%s`, reason = `%s`\n", result.NodeName, result.Error)
    57  			continue
    58  		}
    59  		log.Printf("Profiling successfully started on node `%s`\n", result.NodeName)
    60  	}
    61  
    62  	sleep := time.Duration(10)
    63  	time.Sleep(time.Second * sleep)
    64  
    65  	log.Println("Stopping profiling..")
    66  
    67  	profilingData, err := madmClnt.DownloadProfilingData(context.Background())
    68  	if err != nil {
    69  		log.Fatalln(err)
    70  	}
    71  
    72  	profilingFile, err := os.Create("/tmp/profiling-" + string(profiler) + ".zip")
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  
    77  	if _, err := io.Copy(profilingFile, profilingData); err != nil {
    78  		log.Fatal(err)
    79  	}
    80  
    81  	if err := profilingFile.Close(); err != nil {
    82  		log.Fatal(err)
    83  	}
    84  
    85  	if err := profilingData.Close(); err != nil {
    86  		log.Fatal(err)
    87  	}
    88  
    89  	log.Println("Profiling files " + profilingFile.Name() + " successfully downloaded.")
    90  }