Skip to content
Snippets Groups Projects
Commit 4a78b400 authored by Tóth Miklós Tibor's avatar Tóth Miklós Tibor :shrug:
Browse files

add benchmark

parent 14feb38a
Branches
No related tags found
No related merge requests found
package main
import (
"io/ioutil"
"net/http"
"net/url"
"time"
)
func Consume(add string, interv int, run *bool) error {
u, e := url.Parse(add)
if e != nil {
return e
}
u.Path = "/out/"
t := time.NewTicker(time.Millisecond * time.Duration(interv))
for *run {
_ = <-t.C
resp, e := http.Get(u.String())
if e != nil {
return e
}
_, e = ioutil.ReadAll(resp.Body)
if e != nil {
return e
}
}
return nil
}
package main
import (
"flag"
"fmt"
"math/rand"
"strings"
"time"
)
type stringSlice []string
func (s *stringSlice) String() string {
return strings.Join(*s, " ")
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
func (s *stringSlice) RandElement() string {
l := len(*s)
if l == 0 {
return ""
}
n := rand.Intn(l)
return (*s)[n]
}
var (
loc = flag.String("locations", "http://localhost:8080", "location of server")
prod = flag.Int("producers", 100, "number of producers")
cons = flag.Int("consumers", 1, "number of consumers")
prodInt = flag.Int("prod-interval", 1000, "interval of clicks in ms")
consInt = flag.Int("cons-interval", 1000, "interval of loads in ms")
teams, choices stringSlice
)
func init() {
flag.Var(&teams, "team", "add team")
flag.Var(&choices, "choice", "add choice")
}
func main() {
flag.Parse()
run := true
proderr := make(chan error)
conserr := make(chan error)
prodSleep := time.Duration(float64(*prodInt) / float64(*prod))
for i := 0; i < *prod; i++ {
go func() {
proderr <- Produce(*loc, *prodInt, &run, teams, choices)
}()
time.Sleep(prodSleep)
}
consSleep := time.Duration(float64(*consInt) / float64(*cons))
for i := 0; i < *cons; i++ {
go func() {
conserr <- Consume(*loc, *consInt, &run)
}()
time.Sleep(consSleep)
}
fmt.Println("started benchmark")
fmt.Println("voters:", *prod)
fmt.Println("voting interval:", *prodInt)
fmt.Println("consumers:", *cons)
fmt.Println("consumer interval:", *consInt)
fmt.Println("teams:", teams.String())
fmt.Println("choices:", choices.String())
for {
select {
case err := <-conserr:
fmt.Println("consumer error:", err)
case err := <-proderr:
fmt.Println("producer error:", err)
}
}
}
package main
import (
"github.com/gorilla/websocket"
"net/url"
"time"
)
func Produce(add string, interv int, run *bool, teams, choices stringSlice) error {
u, e := url.Parse(add)
if e != nil {
return e
}
u.Scheme = "ws"
u.Path = "/in/websocket"
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return err
}
d := make(chan error)
go func() {
for {
_, _, err := c.ReadMessage()
if err != nil {
d <- err
return
}
}
}()
err = c.WriteMessage(websocket.TextMessage, []byte(teams.RandElement()))
if err != nil {
return err
}
ticker := time.NewTicker(time.Millisecond * time.Duration(interv))
go func() {
for *run {
_ = <-ticker.C
e := c.WriteMessage(websocket.TextMessage, []byte(choices.RandElement()))
if e != nil {
d <- e
return
}
}
}()
return <-d
}
...@@ -21,6 +21,8 @@ services: ...@@ -21,6 +21,8 @@ services:
- POSTGRES_PASSWORD=matrix - POSTGRES_PASSWORD=matrix
- POSTGRES_USER=matrix - POSTGRES_USER=matrix
- POSTGRES_DB=matrix - POSTGRES_DB=matrix
volumes:
- postgres:/var/lib/postgresql/data
volumes: volumes:
postgres: postgres:
\ No newline at end of file
...@@ -6,6 +6,7 @@ go 1.15 ...@@ -6,6 +6,7 @@ go 1.15
require ( require (
github.com/go-pg/pg/v10 v10.7.5 github.com/go-pg/pg/v10 v10.7.5
github.com/gorilla/websocket v1.4.2
github.com/igm/sockjs-go/v3 v3.0.0 github.com/igm/sockjs-go/v3 v3.0.0
github.com/prometheus/client_golang v1.9.0 github.com/prometheus/client_golang v1.9.0
gitlab.com/MikeTTh/env v0.0.0-20210102155928-2e9be3823cc7 gitlab.com/MikeTTh/env v0.0.0-20210102155928-2e9be3823cc7
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment