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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
//===- CorePerfTests.mm ---------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#import "llbuild/Commands/Commands.h"
#import "llbuild/Basic/ExecutionQueue.h"
#import "llbuild/Core/BuildEngine.h"
#import <XCTest/XCTest.h>
#import <functional>
using namespace llbuild;
using namespace llbuild::core;
@interface CorePerfTests : XCTestCase
@end
#pragma mark - Support Classes
namespace {
static int32_t IntFromValue(const core::ValueType& Value) {
assert(Value.size() == 4);
return ((Value[0] << 0) |
(Value[1] << 8) |
(Value[2] << 16) |
(Value[3] << 24));
}
static core::ValueType IntToValue(int32_t Value) {
std::vector<uint8_t> Result(4);
Result[0] = (Value >> 0) & 0xFF;
Result[1] = (Value >> 8) & 0xFF;
Result[2] = (Value >> 16) & 0xFF;
Result[3] = (Value >> 24) & 0xFF;
return Result;
}
// Simple task implementation which takes a fixed set of dependencies, evaluates
// them all, and then provides the output.
//
// FIXME: This is copied from the Core BuildEngine unittest, we should figure
// out if it should be shared in a common build engine support library at some
// point.
class SimpleTask : public Task {
public:
typedef std::function<int(const std::vector<int>&)> ComputeFnType;
private:
std::vector<KeyType> Inputs;
std::vector<int> InputValues;
ComputeFnType Compute;
public:
SimpleTask(const std::vector<KeyType>& Inputs, ComputeFnType Compute)
: Inputs(Inputs), Compute(Compute)
{
InputValues.resize(Inputs.size());
}
virtual void start(TaskInterface ti) override {
// Request all of the inputs.
for (int i = 0, e = Inputs.size(); i != e; ++i) {
ti.request(Inputs[i], i);
}
}
virtual void provideValue(TaskInterface, uintptr_t InputID,
const KeyType& key, const ValueType& Value) override {
// Update the input values.
assert(InputID < InputValues.size());
InputValues[InputID] = IntFromValue(Value);
}
virtual void inputsAvailable(TaskInterface ti) override {
ti.complete(IntToValue(Compute(InputValues)));
}
};
// Helper function for creating a simple action.
typedef std::function<Task*(BuildEngine&)> ActionFn;
class SimpleRule: public Rule {
public:
typedef std::function<bool(const ValueType& value)> ValidFnType;
private:
SimpleTask::ComputeFnType compute;
std::vector<KeyType> inputs;
ValidFnType valid;
public:
SimpleRule(const KeyType& key, SimpleTask::ComputeFnType compute,
const std::vector<KeyType>& inputs, ValidFnType valid = nullptr)
: Rule(key), compute(compute), inputs(inputs), valid(valid) { }
Task* createTask(BuildEngine&) override { return new SimpleTask(inputs, compute); }
bool isResultValid(BuildEngine&, const ValueType& value) override {
if (!valid) return true;
return valid(value);
}
};
}
@implementation CorePerfTests
#pragma mark - "buildengine ack" Performance Tests
- (void)measurePerformance:(std::function<void()>)body {
[self measureBlock:^() {
body();
}];
}
- (void)testBuildEngineBasicPerf {
// Test the timing of 'buildengine ack 3 14'.
//
// This test uses ~300k rules, and is a good stress test for the core engine
// operation.
[self measureBlock:^{
llbuild::commands::executeBuildEngineCommand({
"ack", "3", "14" });
}];
}
- (void)testBuildEngineDependencyScanningCorePerf {
// Test the timing of 'buildengine ack 3 11', with a high recompute count.
//
// This test uses ~40k rules, but then recomputes the results multiple times,
// which is a stress test of the dependency scanning performance.
[self measureBlock:^{
llbuild::commands::executeBuildEngineCommand({
"ack", "--recompute", "100", "3", "11" });
}];
}
#pragma mark - Synthetic Graph Dependency Scanning Tests
- (void)testBuildEngineDependencyScanningOnLinearChain {
// Test the scanning performance on a deep linear build graph of M nodes::
//
// i1 -> i2 -> ... -> iM
int M = 1000000; // Use a graph of 1 million nodes.
// Set up the build rules.
struct LinearDelegate : public BuildEngineDelegate, public basic::ExecutionQueueDelegate {
virtual std::unique_ptr<core::Rule> lookupRule(const core::KeyType& Key) override {
// We never expect dynamic rule lookup.
fprintf(stderr, "error: unexpected rule lookup for \"%s\"\n",
Key.c_str());
abort();
return nullptr;
}
virtual void cycleDetected(const std::vector<core::Rule*>& Cycle) override {
// We never expect to find a cycle.
fprintf(stderr, "error: unexpected cycle\n");
abort();
}
virtual void error(const Twine& message) override {
fprintf(stderr, "error: %s\n", message.str().c_str());
abort();
}
void processStarted(basic::ProcessContext*, basic::ProcessHandle, llbuild_pid_t) override { }
void processHadError(basic::ProcessContext*, basic::ProcessHandle, const Twine&) override { }
void processHadOutput(basic::ProcessContext*, basic::ProcessHandle, StringRef) override { }
void processFinished(basic::ProcessContext*, basic::ProcessHandle, const basic::ProcessResult&) override { }
void queueJobStarted(basic::JobDescriptor*) override { }
void queueJobFinished(basic::JobDescriptor*) override { }
std::unique_ptr<basic::ExecutionQueue> createExecutionQueue() override {
return createSerialQueue(*this, nullptr);
}
} Delegate;
core::BuildEngine Engine(Delegate);
int LastInputValue = 0;
for (int i = 1; i <= M; ++i) {
char Name[32];
snprintf(Name, sizeof(Name), "i%d", i);
if (i != M) {
char InputName[32];
snprintf(InputName, sizeof(InputName), "i%d", i+1);
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(Name,
[] (const std::vector<int>& Inputs) {
return Inputs[0];
},
{ InputName })));
} else {
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(Name,
[&] (const std::vector<int>& Inputs) {
return LastInputValue; }, {},
[&](const ValueType& value) {
return LastInputValue == IntFromValue(value);
})));
}
}
// Build the first result.
LastInputValue = 42;
auto Result = IntFromValue(Engine.build("i1"));
(void)Result;
assert(Result == LastInputValue);
// Run a single initial null build to try and warm the timings below.
Engine.build("i1");
// Measure the null build time.
[self measurePerformance: [&] {
auto Result = IntFromValue(Engine.build("i1"));
(void)Result;
assert(Result == LastInputValue);
}];
}
static int64_t i64pow(int64_t Value, int64_t Exponent) {
int64_t Result = 1;
for (int64_t i = 0; i != Exponent; ++i)
Result *= Value;
return Result;
}
- (void)testBuildEngineDependencyScanningOnNaryTree {
// Test the scanning performance on an M-height N-ary tree with no sharing::
//
// i1,1 ---> i2,1 ... ---> iM,1
// \-> i2,2 ...
// \-> i2,N iM,{N**(M-1)}
int M = 13, N = 3; // Use a graph of 797,161 nodes.
int NumTotalNodes = (i64pow(N, M) - 1) / (N - 1);
NSLog(@"running test with %d-ary tree of depth %d (%d nodes)\n",
N, M, NumTotalNodes);
// Set up the build rules.
struct NaryTreeDelegate : public BuildEngineDelegate, public basic::ExecutionQueueDelegate {
virtual std::unique_ptr<core::Rule> lookupRule(const core::KeyType& Key) override {
// We never expect dynamic rule lookup.
fprintf(stderr, "error: unexpected rule lookup for \"%s\"\n",
Key.c_str());
abort();
return nullptr;
}
virtual void cycleDetected(const std::vector<core::Rule*>& Cycle) override {
// We never expect to find a cycle.
fprintf(stderr, "error: unexpected cycle\n");
abort();
}
virtual void error(const Twine& message) override {
fprintf(stderr, "error: %s\n", message.str().c_str());
abort();
}
void processStarted(basic::ProcessContext*, basic::ProcessHandle, llbuild_pid_t) override { }
void processHadError(basic::ProcessContext*, basic::ProcessHandle, const Twine&) override { }
void processHadOutput(basic::ProcessContext*, basic::ProcessHandle, StringRef) override { }
void processFinished(basic::ProcessContext*, basic::ProcessHandle, const basic::ProcessResult&) override { }
void queueJobStarted(basic::JobDescriptor*) override { }
void queueJobFinished(basic::JobDescriptor*) override { }
std::unique_ptr<basic::ExecutionQueue> createExecutionQueue() override {
return createSerialQueue(*this, nullptr);
}
} Delegate;
core::BuildEngine Engine(Delegate);
int LastInputValue = 0;
for (int i = 1; i <= M; ++i) {
// Compute the total number of groups at this depth.
int NumNodes = i64pow(N, i - 1);
for (int j = 1; j <= NumNodes; ++j) {
char Name[32];
snprintf(Name, sizeof(Name), "i%d,%d", i, j);
if (i != M) {
std::vector<KeyType> Inputs;
for (int k = 1; k <= N; ++k) {
char InputName[32];
snprintf(InputName, sizeof(InputName), "i%d,%d", i+1, 1 + (j - 1)*N + (k - 1));
Inputs.push_back(InputName);
}
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(
Name, [] (const std::vector<int>& Inputs) {
return Inputs[0]; }, Inputs)));
} else {
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(
Name,
[&] (const std::vector<int>& Inputs) {
return LastInputValue; }, {},
[&](const ValueType& value) {
return LastInputValue == IntFromValue(value);
})));
}
}
}
// Build the first result.
LastInputValue = 42;
auto Result = IntFromValue(Engine.build("i1,1"));
(void)Result;
assert(Result == LastInputValue);
// Run a single initial null build to try and warm the timings below.
Engine.build("i1,1");
// Measure the null build time.
[self measurePerformance: [&] {
auto Result = IntFromValue(Engine.build("i1,1"));
(void)Result;
assert(Result == LastInputValue);
}];
}
- (void)testBuildEngineDependencyScanningOn2DMatrix {
// Test the scanning performance on a 2D {M+1}x{N+1} matrix where each node
// depends on nodes which are adjacent above or to the right::
//
// i1,N --> i2,N --> ... --> iM,N
// ^ ^ ... ^
// | | |
// i1,2 --> i2,2 --> ... --> iM,2
// ^ ^ ... ^
// | | |
// i1,1 --> i2,1 --> ... --> iM,1
//
// This is an easy to construct synthetic graph which is scalable and has
// sharing.
int M = 100, N = 100; // Use a graph of 1 million nodes.
// Set up the build rules.
struct MatrixDelegate : public BuildEngineDelegate, public basic::ExecutionQueueDelegate {
virtual std::unique_ptr<core::Rule> lookupRule(const core::KeyType& Key) override {
// We never expect dynamic rule lookup.
fprintf(stderr, "error: unexpected rule lookup for \"%s\"\n",
Key.c_str());
abort();
return nullptr;
}
virtual void cycleDetected(const std::vector<core::Rule*>& Cycle) override {
// We never expect to find a cycle.
fprintf(stderr, "error: unexpected cycle\n");
abort();
}
virtual void error(const Twine& message) override {
fprintf(stderr, "error: %s\n", message.str().c_str());
abort();
}
void processStarted(basic::ProcessContext*, basic::ProcessHandle, llbuild_pid_t) override { }
void processHadError(basic::ProcessContext*, basic::ProcessHandle, const Twine&) override { }
void processHadOutput(basic::ProcessContext*, basic::ProcessHandle, StringRef) override { }
void processFinished(basic::ProcessContext*, basic::ProcessHandle, const basic::ProcessResult&) override { }
void queueJobStarted(basic::JobDescriptor*) override { }
void queueJobFinished(basic::JobDescriptor*) override { }
std::unique_ptr<basic::ExecutionQueue> createExecutionQueue() override {
return createSerialQueue(*this, nullptr);
}
} Delegate;
core::BuildEngine Engine(Delegate);
int LastInputValue = 0;
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= N; ++j) {
char Name[32];
snprintf(Name, sizeof(Name), "i%d,%d", i, j);
if (i != M && j != N) {
// Nodes not on an edge.
char InputAName[32];
snprintf(InputAName, sizeof(InputAName), "i%d,%d", i+1, j);
char InputBName[32];
snprintf(InputBName, sizeof(InputBName), "i%d,%d", i, j+1);
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(
Name, [] (const std::vector<int>& Inputs) { return Inputs[0]; }, { InputAName, InputBName })));
} else if (i != M) {
// Top edge.
assert(j == N);
char InputName[32];
snprintf(InputName, sizeof(InputName), "i%d,%d", i+1, j);
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(
Name, [] (const std::vector<int>& Inputs) { return Inputs[0]; }, { InputName })));
} else if (j != N) {
// Right edge.
assert(i == M);
char InputName[32];
snprintf(InputName, sizeof(InputName), "i%d,%d", i, j+1);
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(
Name, [] (const std::vector<int>& Inputs) { return Inputs[0]; }, { InputName })));
} else {
// Top-right corner node.
assert(i == M && j == N);
Engine.addRule(std::unique_ptr<core::Rule>(new SimpleRule(
Name, [&] (const std::vector<int>& Inputs) { return LastInputValue; },
{},
[&](const ValueType& value) {
return LastInputValue == IntFromValue(value);
})));
}
}
}
// Build the first result.
LastInputValue = 42;
auto Result = IntFromValue(Engine.build("i1,1"));
(void)Result;
assert(Result == LastInputValue);
// Run a single initial null build to try and warm the timings below.
Engine.build("i1,1");
// Measure the null build time.
[self measurePerformance: [&] {
auto Result = IntFromValue(Engine.build("i1,1"));
(void)Result;
assert(Result == LastInputValue);
}];
}
@end
|