-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.go
More file actions
142 lines (115 loc) · 3.4 KB
/
client.go
File metadata and controls
142 lines (115 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package perf
import (
"context"
"time"
"github.com/go-logr/logr"
"github.com/newcloudtechnologies/memlimiter/utils/config/prepare"
"github.com/rcrowley/go-metrics"
"golang.org/x/time/rate"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/newcloudtechnologies/memlimiter/test/allocator/schema"
"github.com/newcloudtechnologies/memlimiter/utils/breaker"
"github.com/pkg/errors"
)
// Client - нагрузочный клиент.
type Client struct {
startTime time.Time
logger logr.Logger
grpcConn *grpc.ClientConn
client schema.AllocatorClient
breaker *breaker.Breaker
requestsInFlight metrics.Counter
cfg *Config
}
// Run запускает нагрузочную сессию.
func (p *Client) Run() error {
if err := p.breaker.Inc(); err != nil {
return errors.Wrap(err, "breaker inc")
}
defer p.breaker.Dec()
monitoringTicker := time.NewTicker(time.Second)
defer monitoringTicker.Stop()
timer := time.NewTimer(p.cfg.LoadDuration.Duration)
defer timer.Stop()
limiter := rate.NewLimiter(p.cfg.RPS, 1)
for {
// ожидаем, пока лимитер разрешит выполнять запрос
if err := limiter.Wait(p.breaker); err != nil {
return errors.Wrap(err, "limiter wait")
}
// запрос
if err := p.breaker.Inc(); err != nil {
return errors.Wrap(err, "breaker inc")
}
go p.makeRequest()
select {
case <-monitoringTicker.C:
// периодическая печать прогресса
p.printProgress()
case <-timer.C:
// завершение нагрузки
return nil
default:
}
}
}
func (p *Client) makeRequest() {
defer p.breaker.Dec()
// обновление счётчика запросов в полете
p.requestsInFlight.Inc(1)
defer p.requestsInFlight.Dec(1)
ctx, cancel := context.WithTimeout(p.breaker, p.cfg.RequestTimeout.Duration)
defer cancel()
request := &schema.MakeAllocationRequest{
Size: p.cfg.AllocationSize.Value,
}
if p.cfg.PauseDuration.Duration != 0 {
request.Duration = durationpb.New(p.cfg.PauseDuration.Duration)
}
_, err := p.client.MakeAllocation(ctx, request)
if err != nil {
p.logger.Error(err, "make allocation request")
}
}
func (p *Client) printProgress() {
p.logger.Info(
"progress",
"elapsed_time", time.Since(p.startTime),
"in_flight", p.requestsInFlight.Count(),
)
}
// Quit корректно завершает работу нагрузчика.
func (p *Client) Quit() {
p.breaker.ShutdownAndWait()
if err := p.grpcConn.Close(); err != nil {
p.logger.Error(err, "gprc connection close")
}
}
// NewClient создаёт нагрузочный клиент.
func NewClient(cfg *Config) (*Client, error) {
if err := prepare.Prepare(cfg); err != nil {
return nil, errors.Wrap(err, "configs prepare")
}
// FIXME:
/*
logger, err := gaben.FromConfig(cfg.Logging)
if err != nil {
return nil, errors.Wrap(err, "gaben from config")
}
*/
grpcConn, err := grpc.Dial(cfg.Endpoint, grpc.WithInsecure())
if err != nil {
return nil, errors.Wrap(err, "dial error")
}
client := schema.NewAllocatorClient(grpcConn)
return &Client{
grpcConn: grpcConn,
logger: logr.Logger{}, // FIXME
client: client,
startTime: time.Now(),
cfg: cfg,
requestsInFlight: metrics.NewCounter(),
breaker: breaker.NewBreaker(),
}, nil
}