forked from facebook/CacheLib
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStressor.cpp
More file actions
213 lines (190 loc) · 8.76 KB
/
Stressor.cpp
File metadata and controls
213 lines (190 loc) · 8.76 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cachelib/cachebench/runner/Stressor.h"
#include "cachelib/allocator/CacheAllocator.h"
#include "cachelib/cachebench/runner/AsyncCacheStressor.h"
#include "cachelib/cachebench/runner/CacheStressor.h"
#include "cachelib/cachebench/runner/FastShutdown.h"
#include "cachelib/cachebench/runner/IntegrationStressor.h"
#include "cachelib/cachebench/workload/BlockChunkReplayGenerator.h"
#include "cachelib/cachebench/workload/BinaryKVReplayGenerator.h"
#include "cachelib/cachebench/workload/KVReplayGenerator.h"
#include "cachelib/cachebench/workload/OnlineGenerator.h"
#include "cachelib/cachebench/workload/PieceWiseReplayGenerator.h"
#include "cachelib/cachebench/workload/WorkloadGenerator.h"
#include "cachelib/common/Utils.h"
namespace facebook {
namespace cachelib {
namespace cachebench {
ThroughputStats& ThroughputStats::operator+=(const ThroughputStats& other) {
set += other.set;
setFailure += other.setFailure;
get += other.get;
getMiss += other.getMiss;
del += other.del;
update += other.update;
updateMiss += other.updateMiss;
delNotFound += other.delNotFound;
addChained += other.addChained;
addChainedFailure += other.addChainedFailure;
couldExistOp += other.couldExistOp;
couldExistOpFalse += other.couldExistOpFalse;
ops += other.ops;
return *this;
}
void ThroughputStats::render(uint64_t elapsedTimeNs, std::ostream& out) const {
const double elapsedSecs = elapsedTimeNs / static_cast<double>(1e9);
const uint64_t setPerSec = util::narrow_cast<uint64_t>(set / elapsedSecs);
const double setSuccessRate =
set == 0 ? 0.0 : 100.0 * (set - setFailure) / set;
const uint64_t getPerSec = util::narrow_cast<uint64_t>(get / elapsedSecs);
const double getSuccessRate = get == 0 ? 0.0 : 100.0 * (get - getMiss) / get;
const uint64_t delPerSec = util::narrow_cast<uint64_t>(del / elapsedSecs);
const double delSuccessRate =
del == 0 ? 0.0 : 100.0 * (del - delNotFound) / del;
const uint64_t updatePerSec =
util::narrow_cast<uint64_t>(update / elapsedSecs);
const double updateSuccessRate =
update == 0 ? 0.0 : 100.0 * (update - updateMiss) / update;
const uint64_t addChainedPerSec =
util::narrow_cast<uint64_t>(addChained / elapsedSecs);
const double addChainedSuccessRate =
addChained == 0 ? 0.0
: 100.0 * (addChained - addChainedFailure) / addChained;
const uint64_t couldExistPerSec =
util::narrow_cast<uint64_t>(couldExistOp / elapsedSecs);
const double couldExistSuccessRate =
couldExistOp == 0
? 0.0
: 100.0 * (couldExistOp - couldExistOpFalse) / couldExistOp;
out << std::fixed;
out << folly::sformat("{:10}: {:.2f} million", "Total Ops", ops / 1e6)
<< std::endl;
out << folly::sformat("{:10}: {:,}", "Total sets", set) << std::endl;
auto outFn = [&out](folly::StringPiece k1, uint64_t v1, folly::StringPiece k2,
double v2) {
out << folly::sformat("{:10}: {:9,}/s, {:10}: {:6.2f}%", k1, v1, k2, v2)
<< std::endl;
};
outFn("get", getPerSec, "success", getSuccessRate);
outFn("couldExist", couldExistPerSec, "success", couldExistSuccessRate);
outFn("set", setPerSec, "success", setSuccessRate);
outFn("del", delPerSec, "found", delSuccessRate);
if (update > 0) {
outFn("update", updatePerSec, "success", updateSuccessRate);
}
if (addChained > 0) {
outFn("addChained", addChainedPerSec, "success", addChainedSuccessRate);
}
}
void ThroughputStats::render(uint64_t elapsedTimeNs,
folly::UserCounters& counters) const {
const double elapsedSecs = elapsedTimeNs / static_cast<double>(1e9);
const uint64_t setPerSec = util::narrow_cast<uint64_t>(set / elapsedSecs);
const double setSuccessRate =
set == 0 ? 0.0 : 100.0 * (set - setFailure) / set;
const uint64_t getPerSec = util::narrow_cast<uint64_t>(get / elapsedSecs);
const double getSuccessRate = get == 0 ? 0.0 : 100.0 * (get - getMiss) / get;
const uint64_t delPerSec = util::narrow_cast<uint64_t>(del / elapsedSecs);
const double delSuccessRate =
del == 0 ? 0.0 : 100.0 * (del - delNotFound) / del;
const uint64_t addChainedPerSec =
util::narrow_cast<uint64_t>(addChained / elapsedSecs);
const double addChainedSuccessRate =
addChained == 0 ? 0.0
: 100.0 * (addChained - addChainedFailure) / addChained;
counters["ops"] = ops;
counters["total_sets"] = set;
counters["get_per_sec"] = getPerSec;
counters["get_suc_rate"] = util::narrow_cast<uint64_t>(getSuccessRate);
counters["set_per_sec"] = setPerSec;
counters["set_suc_rate"] = util::narrow_cast<uint64_t>(setSuccessRate);
counters["del_per_sec"] = delPerSec;
counters["del_suc_rate"] = util::narrow_cast<uint64_t>(delSuccessRate);
counters["addChain_per_sec"] = addChainedPerSec;
counters["addChain_suc_rate"] =
util::narrow_cast<uint64_t>(addChainedSuccessRate);
}
namespace {
std::unique_ptr<GeneratorBase> makeGenerator(const StressorConfig& config) {
if (config.generator == "piecewise-replay") {
return std::make_unique<PieceWiseReplayGenerator>(config);
} else if (config.generator == "replay") {
return std::make_unique<KVReplayGenerator>(config);
} else if (config.generator == "block-replay") {
return std::make_unique<BlockChunkReplayGenerator>(config);
} else if (config.generator == "binary-replay") {
return std::make_unique<BinaryKVReplayGenerator>(config);
} else if (config.generator.empty() || config.generator == "workload") {
// TODO: Remove the empty() check once we label workload-based configs
// properly
return std::make_unique<WorkloadGenerator>(config);
} else if (config.generator == "online") {
return std::make_unique<OnlineGenerator>(config);
} else {
throw std::invalid_argument(fmt::format(
"Invalid config: unsupported generator {}", config.generator));
}
}
} // namespace
std::unique_ptr<Stressor> Stressor::makeStressor(
const CacheConfig& cacheConfig, const StressorConfig& stressorConfig) {
if (stressorConfig.name == "high_refcount") {
return std::make_unique<HighRefcountStressor>(cacheConfig,
stressorConfig.numOps);
} else if (stressorConfig.name == "cachelib_map") {
return std::make_unique<CachelibMapStressor>(cacheConfig,
stressorConfig.numOps);
} else if (stressorConfig.name == "cachelib_range_map") {
return std::make_unique<CachelibRangeMapStressor>(cacheConfig,
stressorConfig.numOps);
} else if (stressorConfig.name == "fast_shutdown") {
return std::make_unique<FastShutdownStressor>(cacheConfig,
stressorConfig.numOps);
} else if (stressorConfig.name == "async") {
if (stressorConfig.generator != "workload" &&
!stressorConfig.generator.empty()) {
// async model has not been tested with other generators
throw std::invalid_argument(folly::sformat(
"Async cache stressor only works with workload generator currently. "
"generator: {}",
stressorConfig.generator));
}
auto generator = makeGenerator(stressorConfig);
if (cacheConfig.allocator == "LRU") {
// default allocator is LRU, other allocator types should be added here
return std::make_unique<AsyncCacheStressor<LruAllocator>>(
cacheConfig, stressorConfig, std::move(generator));
} else if (cacheConfig.allocator == "LRU2Q") {
return std::make_unique<AsyncCacheStressor<Lru2QAllocator>>(
cacheConfig, stressorConfig, std::move(generator));
}
} else {
auto generator = makeGenerator(stressorConfig);
if (cacheConfig.allocator == "LRU") {
// default allocator is LRU, other allocator types should be added here
return std::make_unique<CacheStressor<LruAllocator>>(
cacheConfig, stressorConfig, std::move(generator));
} else if (cacheConfig.allocator == "LRU2Q") {
return std::make_unique<CacheStressor<Lru2QAllocator>>(
cacheConfig, stressorConfig, std::move(generator));
}
}
throw std::invalid_argument("Invalid config");
}
} // namespace cachebench
} // namespace cachelib
} // namespace facebook