github.com/annchain/OG@v0.0.9/tests/monitor.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package main
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	"github.com/annchain/OG/arefactor/common/httplib"
    20  
    21  	"github.com/annchain/OG/rpc"
    22  
    23  	"io/ioutil"
    24  	"os"
    25  	"strings"
    26  	"time"
    27  )
    28  
    29  type Monitor struct {
    30  	rpc.Monitor
    31  	Err error
    32  	Id  int
    33  }
    34  
    35  type response struct {
    36  	Data rpc.Monitor `json:"data"`
    37  }
    38  
    39  type Statistics struct {
    40  	PeersNum map[int]int
    41  }
    42  
    43  var fistPort = 8000
    44  
    45  var peerNum = 1
    46  var ipsNum = 1
    47  
    48  func main() {
    49  	ips := GetIps()
    50  	for {
    51  		select {
    52  		case <-time.After(1 * time.Second):
    53  			go run(ips)
    54  		}
    55  	}
    56  }
    57  
    58  type Monitors struct {
    59  	Ms []*Monitor `json:"monitors,omitempty"`
    60  }
    61  
    62  func run(ips []string) {
    63  	var ms = make([]*Monitor, peerNum*len(ips))
    64  	mch := make(chan *Monitor, peerNum*len(ips))
    65  	for i, ip := range ips {
    66  		for j := 0; j < peerNum; j++ {
    67  			go getRequest(ip, i*peerNum+j, j, mch)
    68  		}
    69  	}
    70  	var valid bool
    71  	i := 0
    72  	for {
    73  		select {
    74  		case data := <-mch:
    75  			if data.Err == nil {
    76  				valid = true
    77  				d := *data
    78  				ms[data.Id] = &d
    79  			} else {
    80  				//d:= Monitor{}
    81  				//d.Port = fmt.Sprintf("%d", getPort(data.id))
    82  				//ms[data.id]  = &d
    83  			}
    84  			i++
    85  			if i == peerNum*len(ips) {
    86  				goto Out
    87  			}
    88  
    89  		}
    90  	}
    91  Out:
    92  	if valid {
    93  		fmt.Println("time now", time.Now().Format(time.RFC3339))
    94  		var s Statistics
    95  		s.PeersNum = make(map[int]int)
    96  		for _, m := range ms {
    97  			if m == nil {
    98  				continue
    99  			}
   100  			l := len(m.Peers)
   101  			if v, ok := s.PeersNum[l]; ok {
   102  				s.PeersNum[l] = v + 1
   103  			} else {
   104  				s.PeersNum[l] = 1
   105  			}
   106  
   107  			//m.PeerPipeIns = nil
   108  		}
   109  		monitors := Monitors{ms}
   110  		data, _ := json.MarshalIndent(&monitors, "", "\t")
   111  		sData, _ := json.MarshalIndent(&s, "", "\t")
   112  		fmt.Println(string(data))
   113  		fmt.Println(string(sData))
   114  		fmt.Println("end \n\n")
   115  	}
   116  }
   117  
   118  func getPort(id int) int {
   119  	return fistPort + id*10
   120  }
   121  
   122  func GetIps() []string {
   123  	return []string{"172.28.152.101"}
   124  	dir, _ := os.Getwd()
   125  	fName := fmt.Sprintf("%s/scripts/data/hosts", dir)
   126  	f, err := os.Open(fName)
   127  	if err != nil {
   128  		panic(err)
   129  	}
   130  	defer f.Close()
   131  	data, err := ioutil.ReadAll(f)
   132  	if err != nil {
   133  		panic(err)
   134  	}
   135  	ips := strings.Split(string(data), "\n")
   136  	if len(ips) > ipsNum {
   137  		ips = ips[:ipsNum]
   138  	}
   139  	return ips
   140  }
   141  
   142  func getRequest(ip string, id, portId int, ch chan *Monitor) {
   143  	port := getPort(portId)
   144  	host := fmt.Sprintf("http://%s:%d", ip, port)
   145  	//fmt.Println(host)
   146  	req := httplib.NewBeegoRequest(host+"/monitor", "GET")
   147  	req.SetTimeout(8*time.Second, 8*time.Second)
   148  	var res response
   149  	var m Monitor
   150  	err := req.ToJSON(&res)
   151  	if err != nil {
   152  		m.Err = err
   153  	}
   154  	m.Monitor = res.Data
   155  	m.Id = id
   156  	m.Port = fmt.Sprintf("%s:%d", ip, port)
   157  	ch <- &m
   158  	return
   159  }