github.com/minio/madmin-go/v2@v2.2.1/examples/profiling.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Copyright (c) 2015-2022 MinIO, Inc.
     5  //
     6  // This file is part of MinIO Object Storage stack
     7  //
     8  // This program is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU Affero General Public License as
    10  // published by the Free Software Foundation, either version 3 of the
    11  // License, or (at your option) any later version.
    12  //
    13  // This program is distributed in the hope that it will be useful,
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU Affero General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU Affero General Public License
    19  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    20  //
    21  
    22  package main
    23  
    24  import (
    25  	"context"
    26  	"io"
    27  	"log"
    28  	"os"
    29  	"time"
    30  
    31  	"github.com/minio/madmin-go/v2"
    32  )
    33  
    34  func main() {
    35  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    36  	// dummy values, please replace them with original values.
    37  
    38  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    39  	// dummy values, please replace them with original values.
    40  
    41  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    42  	// New returns an MinIO Admin client object.
    43  	madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    44  	if err != nil {
    45  		log.Fatalln(err)
    46  	}
    47  
    48  	profiler := madmin.ProfilerCPU
    49  	log.Println("Starting " + profiler + " profiling..")
    50  
    51  	startResults, err := madmClnt.StartProfiling(context.Background(), profiler)
    52  	if err != nil {
    53  		log.Fatalln(err)
    54  	}
    55  
    56  	for _, result := range startResults {
    57  		if !result.Success {
    58  			log.Printf("Unable to start profiling on node `%s`, reason = `%s`\n", result.NodeName, result.Error)
    59  			continue
    60  		}
    61  		log.Printf("Profiling successfully started on node `%s`\n", result.NodeName)
    62  	}
    63  
    64  	sleep := time.Duration(10)
    65  	time.Sleep(time.Second * sleep)
    66  
    67  	log.Println("Stopping profiling..")
    68  
    69  	profilingData, err := madmClnt.DownloadProfilingData(context.Background())
    70  	if err != nil {
    71  		log.Fatalln(err)
    72  	}
    73  
    74  	profilingFile, err := os.Create("/tmp/profiling-" + string(profiler) + ".zip")
    75  	if err != nil {
    76  		log.Fatal(err)
    77  	}
    78  
    79  	if _, err := io.Copy(profilingFile, profilingData); err != nil {
    80  		log.Fatal(err)
    81  	}
    82  
    83  	if err := profilingFile.Close(); err != nil {
    84  		log.Fatal(err)
    85  	}
    86  
    87  	if err := profilingData.Close(); err != nil {
    88  		log.Fatal(err)
    89  	}
    90  
    91  	log.Println("Profiling files " + profilingFile.Name() + " successfully downloaded.")
    92  }