Merge branch 'master' of github.com:simple-rules/harmony-benchmark

pull/20/head
alok 6 years ago
commit 35d69bfb2c
  1. 44
      aws-code/experiment/commander/main.go
  2. 119
      aws-code/experiment/soldier/main.go
  3. 21
      consensus/consensus_leader.go

@ -0,0 +1,44 @@
package main
import (
"log"
"net"
"strconv"
"strings"
)
const (
message = "Ping"
StopCharacter = "\r\n\r\n"
)
func SocketClient(ip string, port int) {
addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
conn, err := net.Dial("tcp", addr)
defer conn.Close()
if err != nil {
log.Fatalln(err)
}
conn.Write([]byte(message))
conn.Write([]byte(StopCharacter))
log.Printf("Send: %s", message)
buff := make([]byte, 1024)
n, _ := conn.Read(buff)
log.Printf("Receive: %s", buff[:n])
}
func main() {
var (
ip = "127.0.0.1"
port = 3333
)
SocketClient(ip, port)
}

@ -0,0 +1,119 @@
package main
import (
"bufio"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
const (
Message = "Pong"
StopCharacter = "\r\n\r\n"
)
func SocketServer(port int) {
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
defer listen.Close()
if err != nil {
log.Fatalf("Socket listen port %d failed,%s", port, err)
os.Exit(1)
}
log.Printf("Begin listen for command on port: %d", port)
for {
conn, err := listen.Accept()
if err != nil {
log.Fatalln(err)
continue
}
go handler(conn)
}
}
func handler(conn net.Conn) {
defer conn.Close()
var (
buf = make([]byte, 1024)
r = bufio.NewReader(conn)
w = bufio.NewWriter(conn)
)
ILOOP:
for {
n, err := r.Read(buf)
data := string(buf[:n])
switch err {
case io.EOF:
break ILOOP
case nil:
log.Println("Receive:", data)
go handleCommand(data)
if isTransportOver(data) {
break ILOOP
}
default:
log.Fatalf("Receive data failed:%s", err)
return
}
}
w.Write([]byte(Message))
w.Flush()
log.Printf("Send: %s", Message)
}
func handleCommand(command string) {
// assume this is init command
handleInitCommand(command)
}
func handleInitCommand(command string) {
log.Println("Init command")
out, err := os.Create("config_copy.txt")
if err != nil {
panic("Failed to create local file")
}
log.Println("Created local file")
defer out.Close()
resp, err := http.Get("http://localhost/config.txt")
if err != nil {
log.Println("Failed to read file content")
panic("Failed to read file content")
}
log.Println("Read file content")
log.Println(resp)
log.Println(resp.Body)
n, err := io.Copy(out, resp.Body)
if err != nil {
panic("Failed to copy file")
}
log.Println("copy done")
log.Println(resp.Body)
defer resp.Body.Close()
log.Println(n)
}
func isTransportOver(data string) (over bool) {
over = strings.HasSuffix(data, "\r\n\r\n")
return
}
func main() {
port := 3333
SocketServer(port)
}

@ -7,10 +7,15 @@ import (
"encoding/gob"
"harmony-benchmark/blockchain"
"harmony-benchmark/p2p"
"runtime"
"strings"
"time"
)
var (
startTime time.Time
)
// WaitForNewBlock waits for a new block.
func (consensus *Consensus) WaitForNewBlock(blockChannel chan blockchain.Block) {
consensus.Log.Debug("Waiting for block", "consensus", consensus)
@ -56,6 +61,7 @@ func (consensus *Consensus) ProcessMessageLeader(message []byte) {
// Handler for message which triggers consensus process
func (consensus *Consensus) processStartConsensusMessage(payload []byte) {
startTime = time.Now()
tx := blockchain.NewCoinbaseTX("x", "y", 0)
consensus.startConsensus(blockchain.NewGenesisBlock(tx, 0))
}
@ -315,9 +321,24 @@ func (consensus *Consensus) processResponseMessage(payload []byte) {
}
consensus.OnConsensusDone(&blockHeaderObj)
// TODO: @ricl these logic are irrelevant to consensus, move them to another file, say profiler.
endTime := time.Now()
timeElapsed := endTime.Sub(startTime)
numOfTxs := blockHeaderObj.NumTransactions
consensus.Log.Info("TPS Report", "numOfTXs", numOfTxs, "timeElapsed", timeElapsed, "TPS", float64(numOfTxs)/timeElapsed.Seconds())
var m runtime.MemStats
runtime.ReadMemStats(&m)
consensus.Log.Info("Mem Report", "Alloc", bToMb(m.Alloc), "TotalAlloc", bToMb(m.TotalAlloc),
"Sys", bToMb(m.Sys), "NumGC", m.NumGC)
// Send signal to Node so the new block can be added and new round of consensus can be triggered
consensus.ReadySignal <- 1
}
consensus.mutex.Unlock()
}
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

Loading…
Cancel
Save