Skip to content

Commit 0fe60ad

Browse files
authored
feat(dfly_bench): Generate value with random length (#5743)
Flag can be used to set fixed value length or used as range `min:max` where length of value will be randomly selected. Signed-off-by: mkaruza <[email protected]>
1 parent 4750f8d commit 0fe60ad

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/server/dfly_bench.cc

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ ABSL_FLAG(int32_t, qps, 20,
4545

4646
ABSL_FLAG(uint32_t, n, 1000, "Number of requests to send per connection");
4747
ABSL_FLAG(uint32_t, test_time, 0, "Testing time in seconds");
48-
ABSL_FLAG(uint32_t, d, 16, "Value size in bytes ");
48+
ABSL_FLAG(string, d, "16",
49+
"Specify value size as single number for fixed length or use min:max to generate random "
50+
"value length between min and max.");
4951
ABSL_FLAG(string, h, "localhost", "server hostname/ip");
5052
ABSL_FLAG(uint64_t, key_minimum, 0, "Min value for keys used");
5153
ABSL_FLAG(uint64_t, key_maximum, 50'000'000, "Max value for keys used");
@@ -258,14 +260,19 @@ class CommandGenerator {
258260
string FillSet(string_view key);
259261
string FillGet(string_view key);
260262

263+
bool IsRandomValueLen() const {
264+
return value_len_min_ != value_len_max_;
265+
}
266+
261267
KeyGenerator* keygen_;
262268
uint32_t ratio_set_ = 0, ratio_get_ = 0;
263269
string command_;
264270

265271
using CmdPart = variant<string_view, TemplateType>;
266272
vector<CmdPart> cmd_parts_;
267273

268-
string value_;
274+
string fixed_len_value_; // used for fixed value string
275+
int32_t value_len_min_ = 0, value_len_max_ = 0;
269276
bool might_hit_ = false;
270277
bool noreply_ = false;
271278
bool is_ascii_ = true;
@@ -274,7 +281,25 @@ class CommandGenerator {
274281
CommandGenerator::CommandGenerator(KeyGenerator* keygen) : keygen_(keygen) {
275282
command_ = GetFlag(FLAGS_command);
276283
is_ascii_ = GetFlag(FLAGS_ascii);
277-
value_ = string(GetFlag(FLAGS_d), is_ascii_ ? 'a' : char(130));
284+
285+
pair<string, string> value_len_str = absl::StrSplit(GetFlag(FLAGS_d), ':');
286+
CHECK(absl::SimpleAtoi(value_len_str.first, &value_len_min_));
287+
if (!value_len_str.second.empty()) {
288+
CHECK(absl::SimpleAtoi(value_len_str.second, &value_len_max_));
289+
} else {
290+
value_len_max_ = value_len_min_;
291+
}
292+
293+
if ((value_len_min_ < 0) || (value_len_max_ < 0) || (value_len_min_ > value_len_max_)) {
294+
LOG(ERROR) << "Invalid `-d " << GetFlag(FLAGS_d)
295+
<< "` argument. Min and max values should be bigger than 0 and min value should "
296+
"be smaller or equal to max. Setting to default (16).";
297+
value_len_max_ = value_len_min_ = 16;
298+
}
299+
300+
if (!IsRandomValueLen()) {
301+
fixed_len_value_ = string(value_len_min_, is_ascii_ ? 'a' : char(130));
302+
}
278303

279304
if (command_.empty()) {
280305
pair<string, string> ratio_str = absl::StrSplit(GetFlag(FLAGS_ratio), ':');
@@ -335,9 +360,13 @@ string CommandGenerator::Next(SlotRange range) {
335360
case KEY:
336361
str = (*keygen_)(slot_id, slot_id);
337362
break;
338-
case VALUE:
339-
str = GetRandomHex(value_.size(), is_ascii_);
363+
case VALUE: {
364+
size_t value_len = IsRandomValueLen()
365+
? absl::Uniform(bit_gen, value_len_min_, value_len_max_)
366+
: fixed_len_value_.size();
367+
str = GetRandomHex(value_len, is_ascii_);
340368
break;
369+
}
341370
case SCORE: {
342371
uniform_real_distribution<double> uniform(0, 1);
343372
str = absl::StrCat(uniform(bit_gen));
@@ -352,19 +381,26 @@ string CommandGenerator::Next(SlotRange range) {
352381

353382
string CommandGenerator::FillSet(string_view key) {
354383
string res;
384+
string_view value = fixed_len_value_;
385+
string random_len_value;
386+
387+
if (IsRandomValueLen()) {
388+
random_len_value = GetRandomHex(absl::Uniform(bit_gen, value_len_min_, value_len_max_), true);
389+
value = random_len_value;
390+
}
355391

356392
if (protocol == RESP) {
357393
absl::StrAppend(&res, "*3\r\n$3\r\nset\r\n$", key.size(), "\r\n", key);
358-
absl::StrAppend(&res, "\r\n$", value_.size(), "\r\n", value_, "\r\n");
394+
absl::StrAppend(&res, "\r\n$", value.size(), "\r\n", value, "\r\n");
359395
} else {
360396
DCHECK_EQ(protocol, MC_TEXT);
361-
absl::StrAppend(&res, "set ", key, " 0 0 ", value_.size());
397+
absl::StrAppend(&res, "set ", key, " 0 0 ", value.size());
362398
if (GetFlag(FLAGS_noreply)) {
363399
absl::StrAppend(&res, " noreply");
364400
noreply_ = true;
365401
}
366402

367-
absl::StrAppend(&res, "\r\n", value_, "\r\n");
403+
absl::StrAppend(&res, "\r\n", value, "\r\n");
368404
}
369405
return res;
370406
}

0 commit comments

Comments
 (0)