Skip to content

Instantly share code, notes, and snippets.

@freemandealer
Created July 6, 2020 08:03
Show Gist options
  • Save freemandealer/a5e667fa85d29fa905826e8b3cd6feb7 to your computer and use it in GitHub Desktop.
Save freemandealer/a5e667fa85d29fa905826e8b3cd6feb7 to your computer and use it in GitHub Desktop.
inline void create_bytes_random(char *data_buf, int size) {
int data = 0;
int i = 0;
char *data_ptr = data_buf;
char data_char;
for ( i = 0; i < size/2; i ++) {
data = rand();
data_char = (char)data;
if (data_char ==0)
data_char ++;
memset(data_ptr, data_char, 1);
data_ptr ++;
}
data_char = 'a';
for ( i = 0; i < size/2; i ++) {
memset(data_ptr, data_char, 1);
data_ptr ++;
data_char = (data_char + 1) % ('z' - 'a') + 'a';
}
}
int BenchmarkVolume::run_io() {
size_t io_size = FLAGS_io_size;
uint64_t current_us = base::monotonic_time_us();
uint64_t end_us = current_us + 1000000L * FLAGS_run_time_s;
char *data_buf = (char *)malloc(2 * io_size);
memset(data_buf, 0, 2 * io_size);
off_t write_offset = 0;
off_t read_offset = 0;
base::IOBuf buf;
//bool rd_or_write = 0; // 0 for write 1 for read
char *checksumbuf = (char *)malloc(2 * io_size);
uint64_t volume_size = cds_get_volume_size(_volume, false);
SynchronizedClosure done;
while (current_us <= end_us && !_stopped.load(boost::memory_order_acquire)) {
done.reset();
buf.clear();
create_bytes_random(data_buf, io_size);
buf.append(data_buf);
CDS_VLOG << "rd_or_write: " << 0 << " write volume ptr: " << _volume
<< " offset: " << write_offset << " length: " << io_size
<< " buf size:" << buf.size()
<< " checksum: " << raft::crc32(data_buf, io_size);
write_volume(_volume, &buf, write_offset,
benchmark_rw_done,
(void *)&done);
if (FLAGS_io_pattern == 1) {
write_offset = base::fast_rand_less_than(volume_size - io_size);
} else {
write_offset += io_size;
if (write_offset + io_size > volume_size) {
write_offset = 0;
}
}
if (FLAGS_run_time_s > 0) {
current_us = base::monotonic_time_us();
}
done.wait();
}
std::cout << "WRITE VOLUME DONE" << std::endl;
current_us = base::monotonic_time_us();
end_us = current_us + 1000000L * FLAGS_run_time_s;
while (current_us <= end_us && !_stopped.load(boost::memory_order_acquire)) {
done.reset();
read_volume(_volume, &buf, read_offset,
io_size, benchmark_rw_done,
(void *)&done);
buf.cutn(checksumbuf, io_size);
CDS_VLOG << "rd_or_write: " << 1 << " read volume ptr: " << _volume
<< " offset: " << read_offset << " length: " << io_size
<< " checksum: " << raft::crc32(checksumbuf, io_size);
if (FLAGS_io_pattern == 1) {
read_offset = base::fast_rand_less_than(volume_size - io_size);
} else {
read_offset += io_size;
if (read_offset + io_size > volume_size) {
read_offset = 0;
}
}
if (FLAGS_run_time_s > 0) {
current_us = base::monotonic_time_us();
}
done.wait();
}
std::cout << "READ VOLUME DONE" << std::endl;
free(data_buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment