0xDEADBEEF

RSS odkazy

seq-random-reads.d

17. 8. 2021 #kód
import core.sys.linux.unistd : pread;
import std.datetime.stopwatch;
import std.file;
import std.random;
import std.stdio;

void main(string[] args) {
  testSmallRandomReads(args[1], args[2] != "uring");
}

extern(C) int gather_reads(int fd, ulong* lengths, ulong* offsets, ulong n, void* output);

void testSmallRandomReads(string fileName, bool seq) {
  auto f = File(fileName);
  int fd = f.fileno;
  auto fileSize = getSize(fileName);

  enum readLength = 32;

  int n = 4;
  ulong[] lengths = new ulong[n];
  lengths[] = readLength;
  ulong[] offsets = new ulong[n];
  ubyte[1024] output;
  auto rand = Xorshift32(1338);

  foreach (iter; 0 .. 100) {
    foreach (i; 0 .. n) {
      offsets[i] = uniform(0, fileSize - readLength, rand);
    }

    auto sw = StopWatch(AutoStart.yes);
    if (seq) {
      gather_reads_seq(fd, lengths.ptr, offsets.ptr, n, output.ptr);
    } else {
      gather_reads(fd, lengths.ptr, offsets.ptr, n, output.ptr);
    }
    writeln(sw.peek.total!"usecs");
  }

  f.close();
}

void gather_reads_seq(int fd, ulong* lengths, ulong* offsets, ulong n, void* output) {
  foreach (i; 0 .. n) {
    pread(fd, output, lengths[i], offsets[i]);
    output += lengths[i];
  }
}
píše k47 (@kaja47, k47)