forked from datastax/pulsar-heartbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
174 lines (147 loc) · 4.7 KB
/
payload.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Copyright (c) 2020-2021 Datastax, Inc.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 cfg
import (
"fmt"
"math"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
"github.com/datastax/pulsar-heartbeat/src/util"
)
// manage the payload size of Pulsar message
const (
// PrefixDelimiter for message prefix
PrefixDelimiter = "-"
)
// Payload defines the payload size
type Payload struct {
Ceiling int
Floor int
DefaultPayload []byte // to save time for large payload size generation
}
// GenDefaultPayload generates default payload size
func (p *Payload) GenDefaultPayload() []byte {
p.DefaultPayload = p.createPayload()
return p.DefaultPayload
}
func (p Payload) createPayload() []byte {
size := randRange(p.Ceiling, p.Floor)
//use random to make compress impossible
return util.RandStringBytes(size)
}
// PrefixPayload creates string prefix in the payload
func (p Payload) PrefixPayload(prefix string) []byte {
return append([]byte(prefix), p.createPayload()...)
}
// PrefixDefaultPayload creates string prefix in the payload
func (p Payload) PrefixDefaultPayload(prefix string) []byte {
return append([]byte(prefix), p.DefaultPayload...)
}
func randRange(ceiling, floor int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(ceiling-floor+1) + floor
}
// GenPayload generates an array of bytes with prefix string
// and payload size. If the specified payload size is less than
// the prefix size, the payload will just be the prefix.
func GenPayload(prefix, size string) ([]byte, int) {
numOfBytes := NumOfBytes(size)
if len(prefix) > numOfBytes {
return []byte(prefix), numOfBytes
}
numOfBytes = numOfBytes - len(prefix)
p := NewPayload(numOfBytes)
return p.PrefixDefaultPayload(prefix), numOfBytes
}
// NewPayload returns a new Payload object with a fixed payload size
func NewPayload(size int) Payload {
p := Payload{
Ceiling: size,
Floor: size,
}
p.GenDefaultPayload()
return p
}
// NumOfBytes returns a number of bytes with specified size in MB or KB
func NumOfBytes(size string) int {
unitRegex, _ := regexp.Compile("[a-zA-Z]+")
numRegex, _ := regexp.Compile("[0-9]+")
num := unitRegex.ReplaceAllString(size, "")
unit := numRegex.ReplaceAllString(size, "")
bytes, err := strconv.Atoi(num)
if err != nil {
return 0
}
switch strings.ToLower(unit) {
case "mb", "megabytes", "megabyte", "megab":
return bytes * 1024 * 1024
case "kb", "kilobytes", "kilobyte", "kilob":
return bytes * 1024
default:
return bytes
}
}
// AllMsgPayloads generates a series of payloads based on
// specified payload sizes or the number of messages
func AllMsgPayloads(prefix string, payloadSizes []string, numOfMsg int) ([][]byte, int) {
maxPayloadSize := len(prefix)
actualNumOfMsg := 1 //default minimun one message
specifiedSizes := len(payloadSizes)
if specifiedSizes > numOfMsg {
actualNumOfMsg = specifiedSizes
} else if numOfMsg > actualNumOfMsg {
actualNumOfMsg = numOfMsg
}
if specifiedSizes == 0 {
payloadSizes = append(payloadSizes, "0")
specifiedSizes = 1
}
// for large number of messages we keep a map of payload to speed up random payload generation
// payloadTemplates := make(map[string]Payload)
payloads := make([][]byte, actualNumOfMsg)
for i := 0; i < actualNumOfMsg; i++ {
specifiedIndex := specifiedSizes - 1
if i < specifiedSizes {
specifiedIndex = i
}
pre := fmt.Sprintf("%s-%d-", prefix, i)
size := 0
payloads[i], size = GenPayload(pre, payloadSizes[specifiedIndex])
maxPayloadSize = int(math.Max(float64(maxPayloadSize), float64(size)))
payloads[i] = []byte(fmt.Sprintf("%s%s", payloads[i], time.Now().String()))
}
return payloads, maxPayloadSize
}
// GetMessageID returns the message index by parsing the template payload string with a prefix.
func GetMessageID(prefix, str string) int {
parts := strings.Split(string(str), PrefixDelimiter)
if prefix != parts[0] {
return -1
}
index, err := strconv.Atoi(parts[1])
if err != nil {
return -2
}
return index
}