update soldier to support client.

pull/31/head
Richard Liu 6 years ago
parent 27efed47e2
commit fedbef7fad
  1. 8
      aws-experiment-launch/experiment/commander/main.go
  2. 70
      aws-experiment-launch/experiment/soldier/main.go

@ -10,10 +10,13 @@ import (
) )
const ( const (
message = "init http://localhost:8080/config.txt"
StopCharacter = "\r\n\r\n" StopCharacter = "\r\n\r\n"
) )
var (
configFile *string
)
func SocketClient(addr string) { func SocketClient(addr string) {
conn, err := net.Dial("tcp", addr) conn, err := net.Dial("tcp", addr)
@ -23,6 +26,7 @@ func SocketClient(addr string) {
log.Fatalln(err) log.Fatalln(err)
} }
message := "init http://localhost:8080/" + *configFile
conn.Write([]byte(message)) conn.Write([]byte(message))
// conn.Write([]byte(StopCharacter)) // conn.Write([]byte(StopCharacter))
log.Printf("Send: %s", message) log.Printf("Send: %s", message)
@ -33,7 +37,7 @@ func SocketClient(addr string) {
} }
func main() { func main() {
configFile := flag.String("config_file", "config.txt", "file containing all ip addresses") configFile = flag.String("config_file", "test.txt", "file containing all ip addresses")
flag.Parse() flag.Parse()
configs := readConfigFile(*configFile) configs := readConfigFile(*configFile)

@ -99,9 +99,10 @@ func handleCommand(command string, w *bufio.Writer) {
func handleInitCommand(args []string, w *bufio.Writer) { func handleInitCommand(args []string, w *bufio.Writer) {
log.Println("Init command", args) log.Println("Init command", args)
// create local config file // create local config file
localConfig = "node_config_" + *port + ".txt"
out, err := os.Create(localConfig) out, err := os.Create(localConfig)
if err != nil { if err != nil {
log.Fatal("Failed to create local file") log.Fatal("Failed to create local file", err)
} }
defer out.Close() defer out.Close()
@ -127,28 +128,55 @@ func handleInitCommand(args []string, w *bufio.Writer) {
log.Println("Successfully downloaded config") log.Println("Successfully downloaded config")
log.Println(string(content)) log.Println(string(content))
runInstance() run()
w.Write([]byte("Successfully init-ed")) w.Write([]byte("Successfully init-ed"))
w.Flush() w.Flush()
} }
func runInstance() { func createLogFolder() string {
t := time.Now().Format("20060102-150405") t := time.Now().Format("20060102-150405")
logFolder := "../tmp_log/log-" + t logFolder := "../tmp_log/log-" + t
err := os.MkdirAll(logFolder, os.ModePerm) err := os.MkdirAll(logFolder, os.ModePerm)
if err != nil { if err != nil {
log.Fatal("Failed to create log folder") log.Fatal("Failed to create log folder")
} }
cmdName := "./benchmark" return logFolder
cmdArgs := []string{"-ip", *ip, "-port", *port, "-config_file", localConfig, "-log_folder", logFolder} }
log.Println(cmdName, cmdArgs)
cmdOut, err := exec.Command(cmdName, cmdArgs...).Output() func runCmd(name string, args []string) {
log.Println(name, args)
out, err := exec.Command(name, args...).Output()
if err != nil { if err != nil {
log.Fatal("Failed to run command: ", err) log.Fatal("Failed to run command: ", err)
} }
log.Println(string(cmdOut)) log.Println(string(out))
}
func run() {
config := readConfigFile(localConfig)
myConfig := getMyConfig(*ip, *port, &config)
log.Println(myConfig)
if myConfig[2] == "client" {
runClient()
} else {
runInstance()
}
}
func runInstance() {
log.Println("running instance")
logFolder := createLogFolder()
runCmd("./benchmark", []string{"-ip", *ip, "-port", *port, "-config_file", localConfig, "-log_folder", logFolder})
}
func runClient() {
log.Println("running client")
logFolder := createLogFolder()
runCmd("./txgen", []string{"-config_file", localConfig, "-log_folder", logFolder})
} }
func isTransportOver(data string) (over bool) { func isTransportOver(data string) (over bool) {
@ -156,11 +184,35 @@ func isTransportOver(data string) (over bool) {
return return
} }
func readConfigFile(configFile string) [][]string {
file, _ := os.Open(configFile)
fscanner := bufio.NewScanner(file)
result := [][]string{}
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
result = append(result, p)
}
return result
}
func getMyConfig(myIP string, myPort string, config *[][]string) []string {
for _, node := range *config {
ip, port := node[0], node[1]
if ip == myIP && port == myPort {
return node
}
}
return nil
}
// go build -o bin/soldier aws-experiment-launch/experiment/soldier/main.go
// cd bin/
// ./soldier --port=xxxx
func main() { func main() {
ip = flag.String("ip", "127.0.0.1", "IP of the node.") ip = flag.String("ip", "127.0.0.1", "IP of the node.")
port = flag.String("port", "3000", "port of the node.") port = flag.String("port", "3000", "port of the node.")
flag.Parse() flag.Parse()
localConfig = "node_config_" + *port + ".txt"
SocketServer() SocketServer()
} }

Loading…
Cancel
Save