|
1 | 1 | #include "log.h" |
| 2 | +#include <util/random/entropy.h> |
| 3 | +#include <util/random/mersenne.h> |
2 | 4 | #include <ydb/public/api/protos/ydb_formats.pb.h> |
3 | 5 | #include <library/cpp/json/json_value.h> |
4 | 6 | #include <library/cpp/resource/resource.h> |
@@ -252,6 +254,11 @@ class TRandomLogGenerator { |
252 | 254 | return result.str(); |
253 | 255 | } |
254 | 256 |
|
| 257 | + TInstant UniformInstant(ui64 from, ui64 to) const { |
| 258 | + TMersenne<ui64> rnd(Seed()); |
| 259 | + return TInstant::FromValue(rnd.Uniform(from, to)); |
| 260 | + } |
| 261 | + |
255 | 262 | TInstant RandomInstant() const { |
256 | 263 | auto result = TInstant::Now() - TDuration::Seconds(Params.TimestampSubtract); |
257 | 264 | i64 millisecondsDiff = 60 * 1000 * NormalRandom<double>(0., Params.TimestampStandardDeviationMinutes); |
@@ -279,7 +286,7 @@ class TRandomLogGenerator { |
279 | 286 | for (size_t row = 0; row < count; ++row) { |
280 | 287 | result.emplace_back(); |
281 | 288 | result.back().LogId = CreateGuidAsString().c_str(); |
282 | | - result.back().Ts = RandomInstant(); |
| 289 | + result.back().Ts = Params.TimestampDateFrom.has_value() ? UniformInstant(*Params.TimestampDateFrom, *Params.TimestampDateTo) : RandomInstant(); |
283 | 290 | result.back().Level = RandomNumber<ui32>(10); |
284 | 291 | result.back().ServiceName = RandomWord(false); |
285 | 292 | result.back().Component = RandomWord(true); |
@@ -419,7 +426,23 @@ void TLogWorkloadParams::ConfigureOpts(NLastGetopt::TOpts& opts, const ECommandT |
419 | 426 | opts.AddLongOption("rows", "Number of rows to upsert") |
420 | 427 | .DefaultValue(RowsCnt).StoreResult(&RowsCnt); |
421 | 428 | opts.AddLongOption("timestamp_deviation", "Standard deviation. For each timestamp, a random variable with a specified standard deviation in minutes is added.") |
422 | | - .DefaultValue(TimestampStandardDeviationMinutes).StoreResult(&TimestampStandardDeviationMinutes); |
| 429 | + // .DefaultValue(TimestampStandardDeviationMinutes) |
| 430 | + .Optional() |
| 431 | + .StoreResult(&TimestampStandardDeviationMinutes); |
| 432 | + // TODO: maybe it shoudn't be optional |
| 433 | + |
| 434 | + opts.AddLongOption("date-from", "Left boundary of the interval to generate " |
| 435 | + "timestamp uniformly from specified interval. Presents as seconds since epoch. Once this option passed, 'date-to' " |
| 436 | + "should be passed as well. This option is mutually exclusive with 'timestamp_deviation'") |
| 437 | + .Optional().StoreResult(&TimestampDateFrom); |
| 438 | + opts.AddLongOption("date-to", "Right boundary of the interval to generate " |
| 439 | + "timestamp uniformly from specified interval. Presents as seconds since epoch. Once this option passed, 'date-from' " |
| 440 | + "should be passed as well. This option is mutually exclusive with 'timestamp_deviation'") |
| 441 | + .Optional().StoreResult(&TimestampDateTo); |
| 442 | + |
| 443 | + // opts.MutuallyExclusive("timestamp_deviation", "date-from"); |
| 444 | + // opts.MutuallyExclusive("timestamp_deviation", "date-to"); |
| 445 | + |
423 | 446 | opts.AddLongOption("timestamp_subtract", "Value in seconds to subtract from timestamp. For each timestamp, this value in seconds is subtracted") |
424 | 447 | .DefaultValue(0).StoreResult(&TimestampSubtract); |
425 | 448 | opts.AddLongOption("null-percent", "Percent of nulls in generated data") |
@@ -450,6 +473,21 @@ void TLogWorkloadParams::ConfigureOpts(NLastGetopt::TOpts& opts, const ECommandT |
450 | 473 | } |
451 | 474 | } |
452 | 475 |
|
| 476 | +void TLogWorkloadParams::Parse(NYdb::NConsoleClient::TClientCommand::TConfig& config) { |
| 477 | + // TODO: should I check if it's a specific command type: run ? |
| 478 | + auto timestamp_dev_passed = config.ParseResult->Has("timestamp_deviation"); |
| 479 | + auto date_from_passed = config.ParseResult->Has("date-from"); |
| 480 | + auto date_to_passed = config.ParseResult->Has("date-to"); |
| 481 | + |
| 482 | + if (timestamp_dev_passed && (date_from_passed || date_to_passed)) { |
| 483 | + throw yexception() << "The `timestamp_deviation` and `date_from`, `date_to` are mutually exclusive and shouldn't be passed at once"; |
| 484 | + } |
| 485 | + |
| 486 | + if ((date_from_passed && !date_to_passed) || (!date_from_passed && date_to_passed)) { |
| 487 | + throw yexception() << "The `date_from` and `date_to` parameters must be provided together to specify the interval for uniform PK generation"; |
| 488 | + } |
| 489 | +} |
| 490 | + |
453 | 491 | THolder<IWorkloadQueryGenerator> TLogWorkloadParams::CreateGenerator() const { |
454 | 492 | return MakeHolder<TLogGenerator>(this); |
455 | 493 | } |
|
0 commit comments