gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/grpc/profiling/cmd/local.go (about)

     1  /*
     2   *
     3   * Copyright 2019 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package main
    20  
    21  import (
    22  	"encoding/gob"
    23  	"fmt"
    24  	"os"
    25  )
    26  
    27  func loadSnapshot(snapshotFileName string) (*snapshot, error) {
    28  	logger.Infof("opening snapshot file %s", snapshotFileName)
    29  	snapshotFile, err := os.Open(snapshotFileName)
    30  	if err != nil {
    31  		logger.Errorf("cannot open %s: %v", snapshotFileName, err)
    32  		return nil, err
    33  	}
    34  	defer func(snapshotFile *os.File) {
    35  		_ = snapshotFile.Close()
    36  	}(snapshotFile)
    37  
    38  	logger.Infof("decoding snapshot file %s", snapshotFileName)
    39  	s := &snapshot{}
    40  	decoder := gob.NewDecoder(snapshotFile)
    41  	if err = decoder.Decode(s); err != nil {
    42  		logger.Errorf("cannot decode %s: %v", snapshotFileName, err)
    43  		return nil, err
    44  	}
    45  
    46  	return s, nil
    47  }
    48  
    49  func localCommand() error {
    50  	if *flagSnapshot == "" {
    51  		return fmt.Errorf("-snapshot flag missing")
    52  	}
    53  
    54  	s, err := loadSnapshot(*flagSnapshot)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	if *flagStreamStatsCatapultJSON == "" {
    60  		return fmt.Errorf("snapshot file specified without an action to perform")
    61  	}
    62  
    63  	if *flagStreamStatsCatapultJSON != "" {
    64  		if err = streamStatsCatapultJSON(s, *flagStreamStatsCatapultJSON); err != nil {
    65  			return err
    66  		}
    67  	}
    68  
    69  	return nil
    70  }