summaryrefslogtreecommitdiffstats
path: root/src/test/common/Readahead.cc
blob: 9b4e612849fe7f0c726dc05b5841e526bdfc5486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 Adam Crume <adamcrume@gmail.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "common/Readahead.h"
#include "gtest/gtest.h"
#include <stdint.h>
#include <boost/foreach.hpp>
#include <cstdarg>


#define ASSERT_RA(expected_offset, expected_length, ra) \
  do { \
    Readahead::extent_t e = ra; \
    ASSERT_EQ((uint64_t)expected_length, e.second);	\
    if (expected_length) { \
      ASSERT_EQ((uint64_t)expected_offset, e.first); \
    } \
  } while(0)

TEST(Readahead, random_access) {
  Readahead r;
  r.set_trigger_requests(2);
  ASSERT_RA(0, 0, r.update(1000, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1010, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1030, 20, r.update(1020, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1040, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1060, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1080, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1100, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1200, 10, Readahead::NO_LIMIT));
}

TEST(Readahead, min_size_limit) {
  Readahead r;
  r.set_trigger_requests(2);
  r.set_min_readahead_size(40);
  ASSERT_RA(0, 0, r.update(1000, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1010, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1030, 40, r.update(1020, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1030, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1070, 80, r.update(1040, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1050, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1060, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1070, 10, Readahead::NO_LIMIT));
}

TEST(Readahead, max_size_limit) {
  Readahead r;
  r.set_trigger_requests(2);
  r.set_max_readahead_size(50);
  ASSERT_RA(0, 0, r.update(1000, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1010, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1030, 20, r.update(1020, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1050, 40, r.update(1030, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1040, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1050, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1090, 50, r.update(1060, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1070, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1080, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1090, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1100, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1140, 50, r.update(1110, 10, Readahead::NO_LIMIT));
}

TEST(Readahead, limit) {
  Readahead r;
  r.set_trigger_requests(2);
  r.set_max_readahead_size(50);
  uint64_t limit = 1100;
  ASSERT_RA(0, 0, r.update(1000, 10, limit));
  ASSERT_RA(0, 0, r.update(1010, 10, limit));
  ASSERT_RA(1030, 20, r.update(1020, 10, limit));
  ASSERT_RA(1050, 40, r.update(1030, 10, limit));
  ASSERT_RA(0, 0, r.update(1040, 10, limit));
  ASSERT_RA(0, 0, r.update(1050, 10, limit));
  ASSERT_RA(1090, 10, r.update(1060, 10, limit));
  ASSERT_RA(0, 0, r.update(1070, 10, limit));
  ASSERT_RA(0, 0, r.update(1080, 10, limit));
  ASSERT_RA(0, 0, r.update(1090, 10, limit));
}

TEST(Readahead, alignment) {
  Readahead r;
  r.set_trigger_requests(2);
  vector<uint64_t> alignment;
  alignment.push_back(100);
  r.set_alignments(alignment);
  ASSERT_RA(0, 0, r.update(1000, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1010, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1030, 20, r.update(1020, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1050, 50, r.update(1030, 10, Readahead::NO_LIMIT)); // internal readahead size 40
  ASSERT_RA(0, 0, r.update(1040, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1050, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1060, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1100, 100, r.update(1070, 10, Readahead::NO_LIMIT)); // internal readahead size 80
  ASSERT_RA(0, 0, r.update(1080, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1090, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1100, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1110, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1120, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1130, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1200, 200, r.update(1140, 10, Readahead::NO_LIMIT)); // internal readahead size 160
  ASSERT_RA(0, 0, r.update(1150, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1160, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1170, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1180, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1190, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1200, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1210, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1220, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1230, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1240, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1250, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1260, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1270, 10, Readahead::NO_LIMIT));
  ASSERT_RA(0, 0, r.update(1280, 10, Readahead::NO_LIMIT));
  ASSERT_RA(1400, 300, r.update(1290, 10, Readahead::NO_LIMIT)); // internal readahead size 320
  ASSERT_RA(0, 0, r.update(1300, 10, Readahead::NO_LIMIT));
}