-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
|
||
"math/rand" | ||
"time" | ||
|
||
"bytes" | ||
|
||
"bufio" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Config struct { | ||
ServerAddr string `yaml:"server_addr"` | ||
ServerPort string `yaml:"server_port"` | ||
PayloadSize int `yaml:"payload_size"` | ||
RequestsPerSecond int `yaml:"requests_per_second"` | ||
} | ||
|
||
type Client struct { | ||
config *Config | ||
} | ||
|
||
func (cfg *Config) NewClient() *Client { | ||
server := &Client{config: cfg} | ||
|
||
return server | ||
} | ||
|
||
func NewClientConfigFromFile(filename string) (*Config, error) { | ||
data, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var config *Config | ||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func (c *Client) makePayload(size int) []byte { | ||
payload := new(bytes.Buffer) | ||
|
||
for i := 0; i < size; i++ { | ||
payload.WriteByte(byte(rand.Intn(255))) | ||
} | ||
return payload.Bytes() | ||
} | ||
|
||
// sync | ||
func (c *Client) Run() { | ||
rand.Seed(time.Now().Unix()) | ||
|
||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", c.config.ServerAddr, c.config.ServerPort)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
w := bufio.NewWriterSize(conn, c.config.PayloadSize) | ||
for { | ||
payload := c.makePayload(c.config.PayloadSize) | ||
w.Write(payload) | ||
fmt.Fprintf(conn, "\n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
server_addr: localhost | ||
server_port: 1234 | ||
payload_size: 1500 | ||
requests_per_second: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/domdom82/go-backpressure/client" | ||
"github.com/domdom82/go-backpressure/server" | ||
) | ||
|
||
func main() { | ||
var sconfig *server.ServerConfig | ||
//var cconfig *client.ClientConfig | ||
if len(os.Args) > 1 { | ||
fmt.Printf("Loading sconfig from %q\n", os.Args[1]) | ||
s, err := server.NewServerConfigFromFile(os.Args[1]) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
|
||
usage := func() { | ||
fmt.Printf("Usage: %s <-client|-server> <configfile>\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
|
||
if len(os.Args) > 2 { | ||
if os.Args[1] == "-server" { | ||
fmt.Printf("Loading server config from %q\n", os.Args[2]) | ||
s, err := server.NewServerConfigFromFile(os.Args[2]) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("Starting the server.\n") | ||
s.NewServer().Run() | ||
} else if os.Args[1] == "-client" { | ||
fmt.Printf("Loading client config from %q\n", os.Args[2]) | ||
c, err := client.NewClientConfigFromFile(os.Args[2]) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("Starting the client.\n") | ||
c.NewClient().Run() | ||
} | ||
sconfig = s | ||
} else { | ||
fmt.Printf("ERROR: No configuration file provided.\nUsage: %s <-client|-server> <configfile>", os.Args[0]) | ||
os.Exit(1) | ||
usage() | ||
} | ||
|
||
fmt.Printf("Starting the server.\n") | ||
sconfig.NewServer().Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters