github.com/annchain/OG@v0.0.9/p2p/ioperformance/performance.go (about)

     1  package ioperformance
     2  
     3  import (
     4  	"github.com/annchain/OG/arefactor/common/goroutine"
     5  	"github.com/annchain/OG/common"
     6  	"github.com/sirupsen/logrus"
     7  	"sync"
     8  
     9  	"time"
    10  )
    11  
    12  type IoData struct {
    13  	send    common.IOSize
    14  	recv    common.IOSize
    15  	Send    string    `json:"send"`
    16  	Recv    string    `json:"recv"`
    17  	SendNum int       `json:"send_num"`
    18  	RecvNum int       `json:"recv_num"`
    19  	Time    time.Time `json:"time"`
    20  }
    21  
    22  type IoDataInfo struct {
    23  	DataSize     []IoData `json:"data_size"`
    24  	AvgSend      string   `json:"avg_send"`
    25  	TotalSendNum int      `json:"total_send_num"`
    26  	AvgRecv      string   `json:"avg_recv"`
    27  	TotalRecvNum int      `json:"total_receiv_num"`
    28  }
    29  
    30  type iOPerformance struct {
    31  	quit     chan bool
    32  	dataSize []IoData
    33  	send     int
    34  	sendNum  int
    35  	recv     int
    36  	recvNum  int
    37  
    38  	mu sync.RWMutex
    39  }
    40  
    41  var performance *iOPerformance
    42  
    43  func Init() *iOPerformance {
    44  	performance = new(iOPerformance)
    45  	performance.quit = make(chan bool)
    46  	return performance
    47  }
    48  
    49  func (i *iOPerformance) Start() {
    50  	goroutine.New(i.run)
    51  }
    52  
    53  func (i *iOPerformance) Stop() {
    54  	close(i.quit)
    55  }
    56  
    57  func (i *iOPerformance) Name() string {
    58  	return "iOPerformance"
    59  }
    60  
    61  func GetNetPerformance() *IoDataInfo {
    62  	i := performance
    63  	var info IoDataInfo
    64  	var dataSize []IoData
    65  	i.mu.RLock()
    66  	dataSize = i.dataSize
    67  	i.mu.RUnlock()
    68  	if len(dataSize) == 0 {
    69  		return &info
    70  	}
    71  	totalSend := common.IOSize(0)
    72  	totalReceiv := common.IOSize(0)
    73  	for i, d := range dataSize {
    74  		totalSend += d.send
    75  		totalReceiv += d.recv
    76  		info.TotalRecvNum += d.RecvNum
    77  		info.TotalSendNum += d.SendNum
    78  		dataSize[i].Send = d.send.String()
    79  		dataSize[i].Recv = d.recv.String()
    80  		info.DataSize = append(info.DataSize, dataSize[len(dataSize)-i-1])
    81  	}
    82  	avgSend := totalSend / common.IOSize(len(dataSize))
    83  	avgRecv := totalReceiv / common.IOSize(len(dataSize))
    84  	info.AvgSend = avgSend.String()
    85  	info.AvgRecv = avgRecv.String()
    86  	return &info
    87  }
    88  
    89  func AddSendSize(size int) {
    90  	performance.AddSendSize(size)
    91  }
    92  
    93  func AddRecvSize(size int) {
    94  	performance.AddRecvSize(size)
    95  }
    96  
    97  func (i *iOPerformance) AddSendSize(size int) {
    98  	i.mu.Lock()
    99  	defer i.mu.Unlock()
   100  	i.send += size
   101  	i.sendNum++
   102  }
   103  
   104  func (i *iOPerformance) AddRecvSize(size int) {
   105  	i.mu.Lock()
   106  	defer i.mu.Unlock()
   107  	i.recv += size
   108  	i.recvNum++
   109  }
   110  
   111  func (s *iOPerformance) run() {
   112  	var i int
   113  	for {
   114  		select {
   115  		case <-time.After(time.Second):
   116  			s.mu.Lock()
   117  			if i == 60 {
   118  				ioData := IoData{send: common.IOSize(s.send), recv: common.IOSize(s.recv), SendNum: s.sendNum, RecvNum: s.recvNum}
   119  				ioData.Time = time.Now()
   120  				s.dataSize = s.dataSize[1:]
   121  				s.dataSize = append(s.dataSize, ioData)
   122  				s.send, s.recv, s.sendNum, s.recvNum = 0, 0, 0, 0
   123  
   124  			} else {
   125  				ioData := IoData{send: common.IOSize(s.send), recv: common.IOSize(s.recv), SendNum: s.sendNum, RecvNum: s.recvNum}
   126  				ioData.Time = time.Now()
   127  				s.dataSize = append(s.dataSize, ioData)
   128  				s.send, s.recv, s.sendNum, s.recvNum = 0, 0, 0, 0
   129  				i++
   130  			}
   131  			s.mu.Unlock()
   132  			logrus.WithField("data ", s.dataSize[0]).Debug("performance")
   133  		//
   134  		case <-s.quit:
   135  			return
   136  		}
   137  	}
   138  }