This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
152 lines (127 loc) · 4.09 KB
/
load.go
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
143
144
145
146
147
148
149
150
151
152
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package perf
import (
"encoding/json"
"fmt"
"math/rand"
"time"
"istio.io/istio/pkg/log"
)
// Load is the load to apply on the Mixer during the perf test.
// TODO: This should become an interface, and we should be able to specify different load generation strategies.
type Load struct {
// Requests is the set of requests to use.
// TODO: These should all be collapsed to "Requests",
Requests []Request `json:"requests,omitempty"`
// Multiplier is the number of times to apply the specified load on target. If not specified, it will default to 1.
Multiplier int `json:"multiplier,omitempty"`
// StableOrder indicates that the requests will be executed in a stable order. If not set to true, then the
// requests will be randomized before being sent over the wire.
// This is here mostly for debugging.
StableOrder bool `json:"stableOrder,omitempty"`
// RandomSeed is the random seed to use when randomizing load. If omitted (i.e. when set to 0), a time-based seed
// will be used.
// This is here mostly for debugging.
RandomSeed int64 `json:"randomSeed,omitempty"`
}
type loadSerializationState struct {
Iterations int `json:"iterations,omitempty"`
StableOrder bool `json:"stableOrder,omitempty"`
RandomSeed int64 `json:"randomSeed,omitempty"`
Requests []json.RawMessage `json:"requests,omitempty"`
}
// MarshalJSON marshals the load as JSON.
func (l *Load) MarshalJSON() ([]byte, error) {
tmp := loadSerializationState{
Iterations: l.Multiplier,
StableOrder: l.StableOrder,
RandomSeed: l.RandomSeed,
Requests: make([]json.RawMessage, len(l.Requests)),
}
for i, r := range l.Requests {
rb, err := json.Marshal(r)
if err != nil {
return nil, err
}
rr := json.RawMessage(rb)
tmp.Requests[i] = rr
}
return json.Marshal(&tmp)
}
// UnmarshalJSON unmarshals the load from JSON.
func (l *Load) UnmarshalJSON(bytes []byte) error {
tmp := loadSerializationState{}
if err := json.Unmarshal(bytes, &tmp); err != nil {
return err
}
l.Multiplier = tmp.Iterations
l.StableOrder = tmp.StableOrder
l.RandomSeed = tmp.RandomSeed
l.Requests = make([]Request, len(tmp.Requests))
for i, raw := range tmp.Requests {
var rtmp struct {
Type string `json:"type"`
}
if err := json.Unmarshal(raw, &rtmp); err != nil {
return err
}
switch rtmp.Type {
case "basicReport":
var r BasicReport
if err := json.Unmarshal(raw, &r); err != nil {
return err
}
l.Requests[i] = &r
case "basicCheck":
var r BasicCheck
if err := json.Unmarshal(raw, &r); err != nil {
return err
}
l.Requests[i] = &r
default:
return fmt.Errorf("unrecognized request type: '%v'", rtmp.Type)
}
}
return nil
}
func (l *Load) createRequestProtos(c Config) []interface{} {
initialSet := []interface{}{}
for _, r := range l.Requests {
initialSet = append(initialSet, r.createRequestProtos(c)...)
}
requests := initialSet
if l.Multiplier > 1 {
requests = []interface{}{}
for j := 0; j < l.Multiplier; j++ {
requests = append(requests, initialSet...)
}
}
// Shuffle requests if StableOrder is not explicitly requested.
if !l.StableOrder {
seed := l.RandomSeed
if seed == 0 {
seed = time.Now().UnixNano()
}
log.Infof("Random seed for load randomization: '%v'", seed)
// Do not sync the log here to avoid polluting perf timings.
source := rand.NewSource(seed)
random := rand.New(source)
for j := len(requests) - 1; j > 0; j-- {
k := random.Intn(j + 1)
requests[j], requests[k] = requests[k], requests[j]
}
}
return requests
}