Skip to content

Commit

Permalink
Add photon::rand32() and photon::rand64()
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Sep 24, 2024
1 parent e9014d4 commit 2b19450
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 63 deletions.
4 changes: 2 additions & 2 deletions common/memory-stream/memory-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class FaultStream : public IStream

static inline bool gen()
{
if (rand() % 100 < 1)
if (photon::rand32() % 100 < 1)
{
static const uint16_t e[] = {
1, 2, 83, 111, 112,
Expand Down Expand Up @@ -163,7 +163,7 @@ class FaultStream : public IStream
1143, 1144, 1145, 1146, 1147,
1148, 1149, 1150, 1151, 1152,
};
errno = e[rand() % LEN(e)];
errno = e[photon::rand32() % LEN(e)];
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion common/memory-stream/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void* stream_writer(void* stream_)
auto p = STR;
while(count > 0)
{
size_t len = rand() % 8;
size_t len = photon::rand32() % 8;
len = min(len, count);
LOG_DEBUG("Begin write ", VALUE(len));
auto ret = s->write(p, len);
Expand Down
2 changes: 1 addition & 1 deletion common/metric-meter/test/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main() {
SCOPE_LATENCY(table.pread_latency);
SCOPE_LATENCY(table.pread_max);
table.maxv.put(photon::now % 10000000 / 1000000);
photon::thread_usleep(1000 + rand() % 1000);
photon::thread_usleep(1000 + photon::rand32() % 1000);
}
photon::thread_usleep(5000 * 1000);
}
4 changes: 2 additions & 2 deletions common/test/perf_rcuptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main() {
for (int i = 0; i < 100; i++) {
photon::thread_create11([&cntr, &ptr, &sem]() {
for (int j = 0; j < 1000; j++) {
auto x = rand() % 10;
auto x = photon::rand32() % 10;
if (x) { // 99% reader
// ptr.read_lock();
auto x = ptr->val;
Expand Down Expand Up @@ -92,7 +92,7 @@ int main() {
for (int i = 0; i < 100; i++) {
photon::thread_create11([&cntr, &ptr, &sem, &rwlock]() {
for (int j = 0; j < 1000; j++) {
auto x = rand() % 10;
auto x = photon::rand32() % 10;
if (x) { // 90% reader
photon::scoped_rwlock _(rwlock, photon::RLOCK);
(void)ptr.load()->val;
Expand Down
2 changes: 1 addition & 1 deletion common/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ TEST(Callback, virtual_function)

for (int i=0; i<100; ++i)
{
int x = rand();
int x = photon::rand32();
EXPECT_EQ(ca(x), RET + x);
EXPECT_EQ(cb(x), RET - x);
EXPECT_EQ(cc(x), RET + x*2);
Expand Down
8 changes: 4 additions & 4 deletions common/test/test_throttle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST(Throttle, basic) {
auto start = std::chrono::steady_clock::now();
while (total) {
// assume each step may consume about 4K ~ 1M
auto step = rand() % (1UL * 1024 * 1024 - 4096) + 4096;
auto step = photon::rand32() % (1UL * 1024 * 1024 - 4096) + 4096;
if (step > total) step = total;
total -= step;
}
Expand All @@ -36,7 +36,7 @@ TEST(Throttle, basic) {
start = std::chrono::steady_clock::now();
while (total) {
// assume each step may consume about 4K ~ 1M
auto step = rand() % (1UL * 1024 * 1024 - 4096) + 4096;
auto step = photon::rand32() % (1UL * 1024 * 1024 - 4096) + 4096;
if (step > total) step = total;
t.consume(step);
total -= step;
Expand All @@ -61,11 +61,11 @@ TEST(Throttle, restore) {
auto start = std::chrono::steady_clock::now();
while (total) {
// assume each step may consume about 4K ~ 1M
auto step = rand() % (1UL * 1024 * 1024 - 4096) + 4096;
auto step = photon::rand32() % (1UL * 1024 * 1024 - 4096) + 4096;
if (step > total) step = total;
submit += step;
t.consume(step);
if (rand() % 2) {
if (photon::rand32() % 2) {
// 1 of 2 chance to fail and restore consumed chance
t.restore(step);
restore += step;
Expand Down
26 changes: 26 additions & 0 deletions common/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

#include <sys/utsname.h>
#include <execinfo.h>
#include <random>
#include "utility.h"
#include "estring.h"
#include "alog.h"
Expand Down Expand Up @@ -70,3 +71,28 @@ void print_stacktrace() {
free(stacktrace);
}

namespace photon {

static std::random_device rd;
thread_local std::mt19937 gen32(rd());
thread_local std::mt19937_64 gen64(rd());

uint32_t rand32() {
return gen32();
}

uint32_t rand32_distribution(uint32_t min, uint32_t max) {
std::uniform_int_distribution<uint32_t> dist(min, max);
return dist(gen32);
}

uint64_t rand64() {
return gen64();
}

uint64_t rand64_distribution(uint64_t min, uint64_t max) {
std::uniform_int_distribution<uint64_t> dist(min, max);
return dist(gen64);
}

}
16 changes: 16 additions & 0 deletions common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ uint64_t sat_sub(uint64_t x, uint64_t y) {
return c ? 0 : z;
}

// Generate random number in [0, UINT32_MAX]
// The random engine is std::mt19937, which is even faster then rand() in C lib
uint32_t rand32();

// Generate random number, uniformly distributed on the closed interval [min, max]
// Equals to rand32() % (max - min + 1) + min, but slower than it
uint32_t rand32_distribution(uint32_t min, uint32_t max);

// Generate random number in [0, UINT64_MAX]
// The random engine is std::mt19937, which is even faster then rand() in C lib
uint64_t rand64();

// Generate random number, uniformly distributed on the closed interval [min, max]
// Equals to rand64() % (max - min + 1) + min, but slower than it
uint64_t rand64_distribution(uint64_t min, uint64_t max);

}


Expand Down
3 changes: 1 addition & 2 deletions examples/rpc/client_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void run_some_task(ExampleClient* client) {
IOVector iov;
char writebuf[] = "write data like pwrite";
iov.push_back(writebuf, sizeof(writebuf));
auto tmpfile = "/tmp/test_file_" + std::to_string(rand());
auto tmpfile = "/tmp/test_file_" + std::to_string(photon::rand32());
auto ret = client->RPCWrite(ep, tmpfile, iov.iovec(), iov.iovcnt());
LOG_INFO("Write to tmpfile ` ret=`", tmpfile, ret);

Expand All @@ -73,7 +73,6 @@ void handle_term(int) {

int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
srand(time(NULL));
photon::init();
DEFER(photon::fini());

Expand Down
21 changes: 10 additions & 11 deletions fs/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ TEST(range_split, range_split_aligned_case)
}

TEST(range_split, random_test) {
uint64_t begin=rand(), length=rand(), interval=rand();
uint64_t begin=photon::rand32(), length=photon::rand32(), interval=photon::rand32();
LOG_DEBUG("begin=", begin, " length=", length, " interval=", interval);
fs::range_split split(begin, length, interval);
EXPECT_EQ(split.begin, begin);
Expand Down Expand Up @@ -870,9 +870,9 @@ TEST(range_split_power2, basic) {

TEST(range_split_power2, random_test) {
uint64_t offset, length, interval;
offset = rand();
length = rand();
auto interval_shift = rand()%32 + 1;
offset = photon::rand32();
length = photon::rand32();
auto interval_shift = photon::rand32()%32 + 1;
interval = uint64_t(1) << interval_shift;
fs::range_split_power2 split(offset, length, interval);
EXPECT_EQ(offset, split.begin);
Expand Down Expand Up @@ -939,7 +939,7 @@ std::unique_ptr<char[]> random_block(uint64_t size) {
std::unique_ptr<char[]> buff(new char[size]);
char * p = buff.get();
while (size--) {
*(p++) = rand() % UCHAR_MAX;
*(p++) = photon::rand32() % UCHAR_MAX;
}
return buff;
}
Expand Down Expand Up @@ -1199,7 +1199,7 @@ TEST(XFile, error_stiuation) {

void fill_random_buff(char * buff, size_t length) {
for (size_t i = 0; i< length; i++) {
buff[i] = rand() % UCHAR_MAX;
buff[i] = photon::rand32() % UCHAR_MAX;
}
}

Expand All @@ -1210,8 +1210,8 @@ void pread_pwrite_test(IFile *target, IFile *standard) {
char data[max_piece_length];
char buff[max_piece_length];
for (int i = 0;i < test_round; i++) {
off_t off = rand() % max_file_size / getpagesize() * getpagesize();
size_t len = rand() % max_piece_length / getpagesize() * getpagesize();
off_t off = photon::rand32() % max_file_size / getpagesize() * getpagesize();
size_t len = photon::rand32() % max_piece_length / getpagesize() * getpagesize();
if (off+len > max_file_size) {
continue;
}
Expand All @@ -1228,8 +1228,8 @@ void pread_pwrite_test(IFile *target, IFile *standard) {
EXPECT_EQ(0, memcmp(data, buff, ret));
}
for (int i = 0;i < test_round; i++) {
off_t off = rand() % max_file_size;
size_t len = rand() % max_piece_length;
off_t off = photon::rand32() % max_file_size;
size_t len = photon::rand32() % max_piece_length;
if (off+len > max_file_size) {
len = max_file_size - off;
}
Expand Down Expand Up @@ -1373,7 +1373,6 @@ TEST(Walker, basic) {
}

int main(int argc, char **argv){
srand(time(nullptr));
::testing::InitGoogleTest(&argc, argv);
if (photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_NONE))
return -1;
Expand Down
1 change: 0 additions & 1 deletion io/test/test-iouring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ TEST_F(event_engine, cascading_one_shot) {
}

int main(int argc, char** arg) {
srand(time(nullptr));
set_log_output_level(ALOG_INFO);
testing::InitGoogleTest(&argc, arg);
testing::FLAGS_gtest_break_on_failure = true;
Expand Down
4 changes: 2 additions & 2 deletions io/test/test-syncio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void* rand_read(void* _args)

for (size_t i = 0; i < args->n; ++i)
{
off_t offset = rand() % length * alignment + args->start;
off_t offset = photon::rand32() % length * alignment + args->start;
ssize_t ret;
// LOG_DEBUG("rand_read round:`", i);
if (i&1)
Expand Down Expand Up @@ -89,7 +89,7 @@ void* rand_write(void* _args)

for (size_t i = 0; i < args->n; ++i)
{
off_t offset = rand() % length * alignment + args->start;
off_t offset = photon::rand32() % length * alignment + args->start;
ssize_t ret;
if (i&1)
{
Expand Down
3 changes: 1 addition & 2 deletions net/http/test/client_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ int chunked_handler_pt(void*, ISocketStream* sock) {
break;
}
auto max_seg = std::min(remain - 1024, 2 * 4 * 1024UL);
auto seg = 1024 + rand() % max_seg;
auto seg = 1024 + photon::rand32() % max_seg;
chunked_send(offset, seg, sock);
rec.push_back(seg);
offset += seg;
Expand Down Expand Up @@ -373,7 +373,6 @@ TEST(http_client, chunked) {
for (auto &c : std_data) {
c = '0' + ((++num) % 10);
}
srand(time(0));
server->set_handler({nullptr, &chunked_handler_pt});
for (auto tmp = 0; tmp < 20; tmp++) {
auto op_test = client->new_operation(Verb::GET, url);
Expand Down
4 changes: 1 addition & 3 deletions net/http/test/headers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class test_stream : public net::SocketStreamBase {
// assert(count > remain);
// LOG_DEBUG(remain);
if (remain > 200) {
auto len = rand() % 100 + 1;
auto len = photon::rand32() % 100 + 1;
// cout << string(ptr, len);
memcpy(buf, ptr, len);
ptr += len;
Expand Down Expand Up @@ -154,7 +154,6 @@ TEST(headers, resp_header) {

char rand_buf[64 * 1024 - 1];
Response rand_header(rand_buf, sizeof(rand_buf));
srand(time(0));
test_stream stream(2000);
do {
auto ret = rand_header.receive_bytes(&stream);
Expand All @@ -174,7 +173,6 @@ TEST(headers, resp_header) {

char exceed_buf[64 * 1024 - 1];
Response exceed_header(exceed_buf, sizeof(exceed_buf));
srand(time(0));
test_stream exceed_stream(3000);
do {
auto ret = exceed_header.receive_bytes(&exceed_stream);
Expand Down
4 changes: 1 addition & 3 deletions net/http/test/server_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int chunked_handler_pt(void*, net::ISocketStream* sock) {
break;
}
auto max_seg = std::min(remain - 1024, 2 * 4 * 1024UL);
auto seg = 1024 + rand() % max_seg;
auto seg = 1024 + photon::rand32() % max_seg;
chunked_send(offset, seg, sock);
rec.push_back(seg);
offset += seg;
Expand Down Expand Up @@ -271,7 +271,6 @@ TEST(http_server, proxy_handler_get) {
for (auto &c : std_data) {
c = '0' + ((++num) % 10);
}
srand(time(0));
//------------start source server---------------
auto source_server = net::new_tcp_socket_server();
DEFER({ delete source_server; });
Expand Down Expand Up @@ -459,7 +458,6 @@ TEST(http_server, mux_handler) {
for (auto &c : std_data) {
c = '0' + ((++num) % 10);
}
srand(time(0));
//------------start source server---------------
auto source_server = net::new_tcp_socket_server();
DEFER({ delete source_server; });
Expand Down
6 changes: 3 additions & 3 deletions rpc/test/test-ooo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ TEST(OutOfOrder, heavy_test) {
}

inline int error_issue(void*, OutOfOrderContext* args) {
if ((rand()%2) == 0) {
if ((photon::rand32()%2) == 0) {
return -1;
}
return heavy_issue(nullptr, args);
Expand All @@ -196,7 +196,7 @@ inline int error_issue(void*, OutOfOrderContext* args) {
inline int error_complete(void*, OutOfOrderContext* args) {
while (processing_queue.empty()) { thread_yield_to(nullptr); } //waiting till something comming
args->tag = processing_queue.front();
if (rand()%2)
if (photon::rand32()%2)
return -1;
processing_queue.pop();
return 0;
Expand All @@ -207,7 +207,7 @@ inline int error_process() {
thread_yield_to(nullptr);
shuffle(issue_list.begin(), issue_list.end());
auto val = issue_list.back();
if (rand()%2)
if (photon::rand32()%2)
val = UINT64_MAX;
processing_queue.push(val);
issue_list.pop_back();
Expand Down
2 changes: 1 addition & 1 deletion thread/test/test-pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace photon;

void *func1(void *)
{
thread_sleep(rand()%5);
thread_sleep(photon::rand32()%5);
return nullptr;
}

Expand Down
Loading

0 comments on commit 2b19450

Please sign in to comment.