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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
|
#include <mbgl/map/camera.hpp>
#include <mbgl/map/map_observer.hpp>
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/renderer/renderer_observer.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/light.hpp>
#include <mbgl/style/rapidjson_conversion.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/projection.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/tile_cover.hpp>
#include <mapbox/pixelmatch.hpp>
#include <../expression-test/test_runner_common.hpp>
#include "allocation_index.hpp"
#include "file_source.hpp"
#include "metadata.hpp"
#include "parser.hpp"
#include "runner.hpp"
#include <algorithm>
#include <cassert>
#include <utility>
#include <sstream>
using namespace mbgl;
using namespace TestOperationNames;
GfxProbe::GfxProbe(const mbgl::gfx::RenderingStats& stats, const GfxProbe& prev)
: numBuffers(stats.numBuffers),
numDrawCalls(stats.numDrawCalls),
numFrameBuffers(stats.numFrameBuffers),
numTextures(stats.numActiveTextures),
memIndexBuffers(stats.memIndexBuffers, std::max(stats.memIndexBuffers, prev.memIndexBuffers.peak)),
memVertexBuffers(stats.memVertexBuffers, std::max(stats.memVertexBuffers, prev.memVertexBuffers.peak)),
memTextures(stats.memTextures, std::max(stats.memTextures, prev.memTextures.peak)) {}
// static
gfx::HeadlessBackend::SwapBehaviour swapBehavior(MapMode mode) {
return mode == MapMode::Continuous ? gfx::HeadlessBackend::SwapBehaviour::Flush
: gfx::HeadlessBackend::SwapBehaviour::NoFlush;
}
std::string simpleDiff(const Value& result, const Value& expected) {
std::vector<std::string> resultTokens{tokenize(toJSON(result, 2, false))};
std::vector<std::string> expectedTokens{tokenize(toJSON(expected, 2, false))};
std::size_t maxLength = std::max(resultTokens.size(), expectedTokens.size());
std::ostringstream diff;
diff << "<pre>" << std::endl;
const auto flush =
[](const std::vector<std::string>& vec, std::size_t pos, std::ostringstream& out, std::string separator) {
for (std::size_t j = pos; j < vec.size(); ++j) {
out << separator << vec[j] << std::endl;
}
};
for (std::size_t i = 0; i < maxLength; ++i) {
if (resultTokens.size() <= i) {
flush(expectedTokens, i, diff, "-");
break;
}
if (expectedTokens.size() <= i) {
flush(resultTokens, i, diff, "+");
break;
}
if (!deepEqual(resultTokens[i], expectedTokens[i])) {
diff << "<b>"
<< "-" << expectedTokens[i] << "</b>" << std::endl;
diff << "<b>"
<< "+" << resultTokens[i] << "</b>" << std::endl;
} else {
diff << resultTokens[i] << std::endl;
}
}
diff << "</pre>" << std::endl;
return diff.str();
}
TestRunner::TestRunner(Manifest manifest_, UpdateResults updateResults_)
: manifest(std::move(manifest_)), updateResults(updateResults_) {}
const Manifest& TestRunner::getManifest() const {
return manifest;
}
void TestRunner::doShuffle(uint32_t seed) {
manifest.doShuffle(seed);
}
void TestRunner::checkQueryTestResults(mbgl::PremultipliedImage&& actualImage,
std::vector<mbgl::Feature>&& features,
TestMetadata& metadata) {
const std::string& base = metadata.paths.defaultExpectations();
const std::vector<mbgl::filesystem::path>& expectations = metadata.paths.expectations;
metadata.actual = mbgl::encodePNG(actualImage);
if (actualImage.size.isEmpty()) {
metadata.errorMessage = "Invalid size for actual image";
metadata.renderErrored++;
return;
}
metadata.actualJson = toJSON(features, 2, false);
if (metadata.actualJson.empty()) {
metadata.errorMessage = "Invalid size for actual JSON";
metadata.renderErrored++;
return;
}
if (updateResults == UpdateResults::PLATFORM) {
mbgl::filesystem::create_directories(expectations.back());
mbgl::util::write_file(expectations.back().string() + "/expected.json", metadata.actualJson);
metadata.renderErrored++;
return;
} else if (updateResults == UpdateResults::DEFAULT) {
mbgl::util::write_file(base + "/expected.json", metadata.actualJson);
metadata.renderErrored++;
return;
}
mbgl::util::write_file(base + "/actual.json", metadata.actualJson);
std::vector<std::string> expectedJsonPaths;
mbgl::filesystem::path expectedMetricsPath;
for (auto rit = expectations.rbegin(); rit != expectations.rend(); ++rit) {
if (mbgl::filesystem::exists(*rit)) {
expectedJsonPaths = readExpectedJSONEntries(*rit);
if (!expectedJsonPaths.empty()) break;
}
}
if (expectedJsonPaths.empty()) {
metadata.errorMessage = "Failed to find expectations for: " + metadata.paths.stylePath.string();
metadata.renderErrored++;
return;
}
for (const auto& entry : expectedJsonPaths) {
auto maybeExpectedJson = readJson(entry);
if (maybeExpectedJson.is<mbgl::JSDocument>()) {
auto& expected = maybeExpectedJson.get<mbgl::JSDocument>();
mbgl::JSDocument actual;
actual.Parse<0>(metadata.actualJson);
if (actual.HasParseError()) {
metadata.errorMessage = "Error parsing actual JSON for: " + metadata.paths.stylePath.string();
metadata.renderErrored++;
return;
}
auto actualVal = mapbox::geojson::convert<mapbox::geojson::value>(actual);
auto expectedVal = mapbox::geojson::convert<mapbox::geojson::value>(expected);
bool equal = deepEqual(actualVal, expectedVal);
metadata.difference = !equal;
if (equal) {
metadata.diff = "Match";
} else {
metadata.diff = simpleDiff(actualVal, expectedVal);
metadata.renderFailed++;
}
} else {
metadata.errorMessage = "Failed to load expected JSON " + entry;
metadata.renderErrored++;
return;
}
}
}
void TestRunner::checkRenderTestResults(mbgl::PremultipliedImage&& actualImage, TestMetadata& metadata) {
const std::string& base = metadata.paths.defaultExpectations();
const std::vector<mbgl::filesystem::path>& expectations = metadata.paths.expectations;
if (metadata.outputsImage) {
metadata.actual = mbgl::encodePNG(actualImage);
if (actualImage.size.isEmpty()) {
metadata.errorMessage = "Invalid size for actual image";
metadata.renderErrored++;
return;
}
if (updateResults == UpdateResults::PLATFORM) {
mbgl::filesystem::create_directories(expectations.back());
mbgl::util::write_file(expectations.back().string() + "/expected.png", mbgl::encodePNG(actualImage));
metadata.renderErrored++;
return;
} else if (updateResults == UpdateResults::DEFAULT) {
mbgl::util::write_file(base + "/expected.png", mbgl::encodePNG(actualImage));
metadata.renderErrored++;
return;
}
mbgl::util::write_file(base + "/actual.png", metadata.actual);
mbgl::PremultipliedImage expectedImage{actualImage.size};
mbgl::PremultipliedImage imageDiff{actualImage.size};
double pixels = 0.0;
std::vector<std::string> expectedImagesPaths;
for (auto rit = expectations.rbegin(); rit != expectations.rend(); ++rit) {
if (mbgl::filesystem::exists(*rit)) {
expectedImagesPaths = readExpectedImageEntries(*rit);
if (!expectedImagesPaths.empty()) break;
}
}
if (expectedImagesPaths.empty()) {
metadata.errorMessage = "Failed to find expectations for: " + metadata.paths.stylePath.string();
metadata.renderErrored++;
return;
}
for (const auto& entry : expectedImagesPaths) {
mbgl::optional<std::string> maybeExpectedImage = mbgl::util::readFile(entry);
if (!maybeExpectedImage) {
metadata.errorMessage = "Failed to load expected image " + entry;
metadata.renderErrored = true;
return;
}
metadata.expected = *maybeExpectedImage;
expectedImage = mbgl::decodeImage(*maybeExpectedImage);
pixels = // implicitly converting from uint64_t
mapbox::pixelmatch(actualImage.data.get(),
expectedImage.data.get(),
expectedImage.size.width,
expectedImage.size.height,
imageDiff.data.get(),
0.1285); // Defined in GL JS
metadata.diff = mbgl::encodePNG(imageDiff);
mbgl::util::write_file(base + "/diff.png", metadata.diff);
metadata.difference = pixels / expectedImage.size.area();
if (metadata.difference <= metadata.allowed) {
break;
}
}
if (metadata.difference > metadata.allowed) {
metadata.renderFailed++;
}
}
}
void TestRunner::checkProbingResults(TestMetadata& resultMetadata) {
if (resultMetadata.metrics.isEmpty()) return;
const auto writeMetrics = [&resultMetadata](const mbgl::filesystem::path& path,
const std::string& message = std::string()) {
mbgl::filesystem::create_directories(path);
mbgl::util::write_file(path / "metrics.json", serializeMetrics(resultMetadata.metrics));
resultMetadata.errorMessage += message;
};
const std::vector<mbgl::filesystem::path>& expectedMetrics = resultMetadata.paths.expectedMetrics;
if (updateResults == UpdateResults::METRICS) {
writeMetrics(expectedMetrics.back(), " Updated expected metrics.");
resultMetadata.metricsErrored++;
return;
}
// Check the possible paths in reverse order, so that the default path with the test style will only be checked in
// the very end.
std::vector<std::string> expectedMetricsPaths;
for (auto rit = expectedMetrics.rbegin(); rit != expectedMetrics.rend(); ++rit) {
if (mbgl::filesystem::exists(*rit)) {
expectedMetricsPaths = readExpectedMetricEntries(*rit);
if (!expectedMetricsPaths.empty()) break;
}
}
// In case no metrics.json is found, skip assigning the expectedMetrics to metadata, otherwise, take the first found
// metrics.
for (const auto& entry : expectedMetricsPaths) {
auto maybeExpectedMetrics = readExpectedMetrics(entry);
if (maybeExpectedMetrics.isEmpty()) {
resultMetadata.errorMessage = "Failed to load expected metrics " + entry;
resultMetadata.metricsErrored++;
return;
}
resultMetadata.expectedMetrics = maybeExpectedMetrics;
break;
}
if (resultMetadata.expectedMetrics.isEmpty()) {
resultMetadata.errorMessage =
"Failed to find metric expectations for: " + resultMetadata.paths.stylePath.string();
if (updateResults == UpdateResults::REBASELINE && !resultMetadata.ignoredTest) {
writeMetrics(expectedMetrics.back(), ". Created baseline for missing metrics.");
}
resultMetadata.metricsErrored++;
return;
}
// Check file size metrics.
auto checkFileSize = [](TestMetadata& metadata) {
if (metadata.metrics.fileSize.empty()) return;
for (const auto& expected : metadata.expectedMetrics.fileSize) {
auto actual = metadata.metrics.fileSize.find(expected.first);
if (actual == metadata.metrics.fileSize.end()) {
metadata.errorMessage = "Failed to find fileSize probe: " + expected.first;
metadata.metricsErrored++;
return;
}
if (actual->second.path != expected.second.path) {
std::stringstream ss;
ss << "Comparing different files at probe \"" << expected.first << "\": " << actual->second.path
<< ", expected is " << expected.second.path << ".";
metadata.errorMessage = ss.str();
metadata.metricsErrored++;
return;
}
auto result = checkValue(expected.second.size, actual->second.size, actual->second.tolerance);
if (!std::get<bool>(result)) {
std::stringstream ss;
ss << "File size does not match at probe \"" << expected.first << "\" for file \""
<< expected.second.path << "\": " << actual->second.size << ", expected is " << expected.second.size
<< ".";
metadata.errorMessage += metadata.errorMessage.empty() ? ss.str() : "\n" + ss.str();
metadata.metricsFailed++;
}
}
};
auto checkMemory = [](TestMetadata& metadata) {
if (metadata.metrics.memory.empty()) return;
#if !defined(SANITIZE)
// Check memory metrics.
for (const auto& expected : metadata.expectedMetrics.memory) {
auto actual = metadata.metrics.memory.find(expected.first);
if (actual == metadata.metrics.memory.end()) {
metadata.errorMessage = "Failed to find memory probe: " + expected.first;
metadata.metricsErrored++;
return;
}
bool passed{false};
float delta{0.0f};
std::stringstream errorStream;
std::tie(passed, delta) = MemoryProbe::checkPeak(expected.second, actual->second);
if (!passed) {
errorStream << "Allocated memory peak size at probe \"" << expected.first << "\" is "
<< actual->second.peak << " bytes, expected is " << expected.second.peak << "±" << delta
<< " bytes.";
metadata.metricsFailed++;
}
std::tie(passed, delta) = MemoryProbe::checkAllocations(expected.second, actual->second);
if (!passed) {
errorStream << "Number of allocations at probe \"" << expected.first << "\" is "
<< actual->second.allocations << ", expected is " << expected.second.allocations << "±"
<< std::round(delta) << " allocations.";
metadata.metricsFailed++;
}
metadata.errorMessage += metadata.errorMessage.empty() ? errorStream.str() : "\n" + errorStream.str();
}
#endif // !defined(SANITIZE)
};
// Check network metrics.
auto checkNetwork = [](TestMetadata& metadata) {
if (metadata.metrics.network.empty()) return;
#if !defined(SANITIZE)
for (const auto& expected : metadata.expectedMetrics.network) {
auto actual = metadata.metrics.network.find(expected.first);
if (actual == metadata.metrics.network.end()) {
metadata.errorMessage = "Failed to find network probe: " + expected.first;
metadata.metricsErrored++;
return;
}
std::stringstream ss;
if (actual->second.requests != expected.second.requests) {
ss << "Number of requests at probe \"" << expected.first << "\" is " << actual->second.requests
<< ", expected is " << expected.second.requests << ". ";
metadata.metricsFailed++;
}
if (actual->second.transferred != expected.second.transferred) {
ss << "Transferred data at probe \"" << expected.first << "\" is " << actual->second.transferred
<< " bytes, expected is " << expected.second.transferred << " bytes.";
metadata.metricsFailed++;
}
metadata.errorMessage += metadata.errorMessage.empty() ? ss.str() : "\n" + ss.str();
}
#endif // !defined(SANITIZE)
};
// Check fps metrics
auto checkFps = [](TestMetadata& metadata) {
if (metadata.metrics.fps.empty()) return;
for (const auto& expected : metadata.expectedMetrics.fps) {
auto actual = metadata.metrics.fps.find(expected.first);
if (actual == metadata.metrics.fps.end()) {
metadata.errorMessage = "Failed to find fps probe: " + expected.first;
metadata.metricsErrored++;
return;
}
auto result = checkValue(expected.second.average, actual->second.average, expected.second.tolerance);
std::stringstream ss;
if (!std::get<bool>(result)) {
ss << "Average fps at probe \"" << expected.first << "\" is " << actual->second.average
<< ", expected to be " << expected.second.average << " with tolerance of "
<< expected.second.tolerance;
metadata.metricsFailed++;
}
result = checkValue(expected.second.minOnePc, actual->second.minOnePc, expected.second.tolerance);
if (!std::get<bool>(result)) {
ss << "Minimum(1%) fps at probe \"" << expected.first << "\" is " << actual->second.minOnePc
<< ", expected to be " << expected.second.minOnePc << " with tolerance of "
<< expected.second.tolerance;
metadata.metricsFailed++;
}
metadata.errorMessage += metadata.errorMessage.empty() ? ss.str() : "\n" + ss.str();
}
};
// Check gfx metrics
auto checkGfx = [](TestMetadata& metadata) {
if (metadata.metrics.gfx.empty()) return;
for (const auto& expected : metadata.expectedMetrics.gfx) {
auto actual = metadata.metrics.gfx.find(expected.first);
if (actual == metadata.metrics.gfx.end()) {
metadata.errorMessage = "Failed to find gfx probe: " + expected.first;
metadata.metricsErrored++;
return;
}
const auto& probeName = expected.first;
const auto& expectedValue = expected.second;
const auto& actualValue = actual->second;
std::stringstream ss;
if (expectedValue.numDrawCalls != actualValue.numDrawCalls) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Number of draw calls at probe\"" << probeName << "\" is " << actualValue.numDrawCalls
<< ", expected is " << expectedValue.numDrawCalls;
metadata.metricsFailed++;
}
if (expectedValue.numTextures != actualValue.numTextures) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Number of textures at probe \"" << probeName << "\" is " << actualValue.numTextures
<< ", expected is " << expectedValue.numTextures;
metadata.metricsFailed++;
}
if (expectedValue.numBuffers != actualValue.numBuffers) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Number of vertex and index buffers at probe \"" << probeName << "\" is "
<< actualValue.numBuffers << ", expected is " << expectedValue.numBuffers;
metadata.metricsFailed++;
}
if (expectedValue.numFrameBuffers != actualValue.numFrameBuffers) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Number of frame buffers at probe \"" << probeName << "\" is " << actualValue.numFrameBuffers
<< ", expected is " << expectedValue.numFrameBuffers;
metadata.metricsFailed++;
}
if (expectedValue.memTextures.peak != actualValue.memTextures.peak) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Allocated texture memory peak size at probe \"" << probeName << "\" is "
<< actualValue.memTextures.peak << " bytes, expected is " << expectedValue.memTextures.peak
<< " bytes";
metadata.metricsFailed++;
}
if (expectedValue.memIndexBuffers.peak != actualValue.memIndexBuffers.peak) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Allocated index buffer memory peak size at probe \"" << probeName << "\" is "
<< actualValue.memIndexBuffers.peak << " bytes, expected is " << expectedValue.memIndexBuffers.peak
<< " bytes";
metadata.metricsFailed++;
}
if (expectedValue.memVertexBuffers.peak != actualValue.memVertexBuffers.peak) {
if (!metadata.errorMessage.empty()) ss << std::endl;
ss << "Allocated vertex buffer memory peak size at probe \"" << probeName << "\" is "
<< actualValue.memVertexBuffers.peak << " bytes, expected is " << expectedValue.memVertexBuffers.peak
<< " bytes";
metadata.metricsFailed++;
}
metadata.errorMessage += metadata.errorMessage.empty() ? ss.str() : "\n" + ss.str();
}
};
checkFileSize(resultMetadata);
checkMemory(resultMetadata);
checkNetwork(resultMetadata);
checkFps(resultMetadata);
checkGfx(resultMetadata);
if (resultMetadata.ignoredTest) {
return;
}
if ((resultMetadata.metricsErrored || resultMetadata.metricsFailed) && updateResults == UpdateResults::REBASELINE) {
writeMetrics(expectedMetrics.back(), " Rebaselined expected metric for failed test.");
}
}
namespace {
TestOperation unsupportedOperation(const std::string& operation) {
return [operation](TestContext& ctx) {
ctx.getMetadata().errorMessage = std::string("Unsupported operation: ") + operation;
return false;
};
}
TestOperations getBeforeOperations(const Manifest& manifest) {
static const std::string mark = " - default - start";
TestOperations result;
for (const std::string& probe : manifest.getProbes()) {
if (memoryProbeOp == probe) {
result.emplace_back([](TestContext& ctx) {
assert(!AllocationIndex::isActive());
AllocationIndex::setActive(true);
ctx.getMetadata().metrics.memory.emplace(std::piecewise_construct,
std::forward_as_tuple(memoryProbeOp + mark),
std::forward_as_tuple(AllocationIndex::getAllocatedSizePeak(),
AllocationIndex::getAllocationsCount()));
return true;
});
continue;
}
if (gfxProbeOp == probe) {
result.emplace_back([](TestContext& ctx) {
assert(!ctx.gfxProbeActive);
ctx.gfxProbeActive = true;
ctx.baselineGfxProbe = ctx.activeGfxProbe;
return true;
});
continue;
}
if (networkProbeOp == probe) {
result.emplace_back([](TestContext& ctx) {
assert(!ProxyFileSource::isTrackingActive());
ProxyFileSource::setTrackingActive(true);
ctx.getMetadata().metrics.network.emplace(
std::piecewise_construct,
std::forward_as_tuple(networkProbeOp + mark),
std::forward_as_tuple(ProxyFileSource::getRequestCount(), ProxyFileSource::getTransferredSize()));
return true;
});
continue;
}
result.emplace_back(unsupportedOperation(probe));
}
return result;
}
TestOperations getAfterOperations(const Manifest& manifest) {
static const std::string mark = " - default - end";
TestOperations result;
for (const std::string& probe : manifest.getProbes()) {
if (memoryProbeOp == probe) {
result.emplace_back([](TestContext& ctx) {
assert(AllocationIndex::isActive());
auto emplaced = ctx.getMetadata().metrics.memory.emplace(
std::piecewise_construct,
std::forward_as_tuple(memoryProbeOp + mark),
std::forward_as_tuple(AllocationIndex::getAllocatedSizePeak(),
AllocationIndex::getAllocationsCount()));
assert(emplaced.second);
// TODO: Improve tolerance handling for memory tests.
emplaced.first->second.tolerance = 0.2f;
AllocationIndex::setActive(false);
AllocationIndex::reset();
return true;
});
continue;
}
if (gfxProbeOp == probe) {
result.emplace_back([](TestContext& ctx) {
// Compare memory allocations to the baseline probe
GfxProbe metricProbe = ctx.activeGfxProbe;
metricProbe.memIndexBuffers.peak -= ctx.baselineGfxProbe.memIndexBuffers.peak;
metricProbe.memVertexBuffers.peak -= ctx.baselineGfxProbe.memVertexBuffers.peak;
metricProbe.memTextures.peak -= ctx.baselineGfxProbe.memTextures.peak;
ctx.getMetadata().metrics.gfx.insert({gfxProbeOp + mark, metricProbe});
ctx.gfxProbeActive = false;
return true;
});
continue;
}
if (networkProbeOp == probe) {
result.emplace_back([](TestContext& ctx) {
assert(ProxyFileSource::isTrackingActive());
ctx.getMetadata().metrics.network.emplace(
std::piecewise_construct,
std::forward_as_tuple(networkProbeOp + mark),
std::forward_as_tuple(ProxyFileSource::getRequestCount(), ProxyFileSource::getTransferredSize()));
ProxyFileSource::setTrackingActive(false);
return true;
});
continue;
}
result.emplace_back(unsupportedOperation(probe));
}
return result;
}
void resetContext(const TestMetadata& metadata, TestContext& ctx) {
ctx.getFrontend().setSize(metadata.size);
auto& map = ctx.getMap();
map.setSize(metadata.size);
map.setProjectionMode(mbgl::ProjectionMode()
.withAxonometric(metadata.axonometric)
.withXSkew(metadata.xSkew)
.withYSkew(metadata.ySkew));
map.setDebug(metadata.debug);
map.getStyle().loadJSON(serializeJsonValue(metadata.document));
}
LatLng getTileCenterCoordinates(const UnwrappedTileID& tileId) {
double scale = (1 << tileId.canonical.z);
Point<double> tileCenter{(tileId.canonical.x + 0.5) * util::tileSize, (tileId.canonical.y + 0.5) * util::tileSize};
return Projection::unproject(tileCenter, scale);
}
constexpr auto kTileSizeUint = uint32_t(util::tileSize);
uint32_t getImageTileOffset(const std::set<uint32_t>& dims, uint32_t dim) {
auto it = dims.find(dim);
if (it == dims.end()) {
assert(false);
return 0;
}
return std::distance(dims.begin(), it) * kTileSizeUint;
}
} // namespace
TestRunner::Impl::Impl(const TestMetadata& metadata, const mbgl::ResourceOptions& resourceOptions)
: observer(std::make_unique<TestRunnerMapObserver>()),
frontend(metadata.size, metadata.pixelRatio, swapBehavior(metadata.mapMode)),
fileSource(mbgl::FileSource::getSharedFileSource(resourceOptions)),
map(frontend,
*observer.get(),
mbgl::MapOptions()
.withMapMode(metadata.mapMode)
.withSize(metadata.size)
.withPixelRatio(metadata.pixelRatio)
.withCrossSourceCollisions(metadata.crossSourceCollisions),
resourceOptions) {}
TestRunner::Impl::~Impl() {}
void TestRunner::run(TestMetadata& metadata) {
AllocationIndex::setActive(false);
AllocationIndex::reset();
ProxyFileSource::setTrackingActive(false);
struct ContextImpl final : public TestContext {
ContextImpl(TestMetadata& metadata_) : metadata(metadata_) {}
HeadlessFrontend& getFrontend() override {
assert(runnerImpl);
return runnerImpl->frontend;
}
Map& getMap() override {
assert(runnerImpl);
return runnerImpl->map;
}
FileSource& getFileSource() override {
assert(runnerImpl);
return *runnerImpl->fileSource;
}
TestRunnerMapObserver& getObserver() override {
assert(runnerImpl);
return *runnerImpl->observer;
}
TestMetadata& getMetadata() override { return metadata; }
TestRunner::Impl* runnerImpl = nullptr;
TestMetadata& metadata;
};
ContextImpl ctx(metadata);
if (!metadata.ignoredTest) {
for (const auto& operation : getBeforeOperations(manifest)) {
if (!operation(ctx)) return;
}
}
std::string key = mbgl::util::toString(uint32_t(metadata.mapMode)) + "/" +
mbgl::util::toString(metadata.pixelRatio) + "/" +
mbgl::util::toString(uint32_t(metadata.crossSourceCollisions));
if (maps.find(key) == maps.end()) {
maps[key] = std::make_unique<TestRunner::Impl>(
metadata,
mbgl::ResourceOptions().withCachePath(manifest.getCachePath()).withAccessToken(manifest.getAccessToken()));
}
ctx.runnerImpl = maps[key].get();
auto& frontend = ctx.getFrontend();
auto& map = ctx.getMap();
resetContext(metadata, ctx);
auto camera = map.getStyle().getDefaultCamera();
HeadlessFrontend::RenderResult result{};
if (metadata.mapMode == MapMode::Tile) {
assert(camera.zoom);
assert(camera.center);
auto tileIds = util::tileCover(map.latLngBoundsForCamera(camera), *camera.zoom);
assert(!tileIds.empty());
std::set<uint32_t> xDims;
std::set<uint32_t> yDims;
for (const auto& tileId : tileIds) {
xDims.insert(tileId.canonical.x);
yDims.insert(tileId.canonical.y);
assert(tileId.canonical.z == uint8_t(*camera.zoom));
}
result.image =
PremultipliedImage({uint32_t(xDims.size()) * kTileSizeUint, uint32_t(yDims.size()) * kTileSizeUint});
for (const auto& tileId : tileIds) {
resetContext(metadata, ctx);
auto cameraForTile{camera};
cameraForTile.withCenter(getTileCenterCoordinates(tileId));
map.jumpTo(cameraForTile);
auto resultForTile = runTest(metadata, ctx);
if (!resultForTile.image.valid()) {
metadata.errorMessage = "Failed rendering tile: " + util::toString(tileId);
return;
}
auto xOffset = getImageTileOffset(xDims, tileId.canonical.x);
auto yOffset = getImageTileOffset(yDims, tileId.canonical.y);
PremultipliedImage::copy(
resultForTile.image, result.image, {0, 0}, {xOffset, yOffset}, resultForTile.image.size);
result.stats += resultForTile.stats;
}
} else {
map.jumpTo(camera);
result = runTest(metadata, ctx);
}
if (!metadata.ignoredTest) {
ctx.activeGfxProbe = GfxProbe(result.stats, ctx.activeGfxProbe);
for (const auto& operation : getAfterOperations(manifest)) {
if (!operation(ctx)) return;
}
}
if (metadata.renderTest) {
checkProbingResults(metadata);
checkRenderTestResults(std::move(result.image), metadata);
} else {
std::vector<mbgl::Feature> features;
assert(metadata.document["metadata"]["test"]["queryGeometry"].IsArray());
if (metadata.document["metadata"]["test"]["queryGeometry"][0].IsNumber() &&
metadata.document["metadata"]["test"]["queryGeometry"][1].IsNumber()) {
features = frontend.getRenderer()->queryRenderedFeatures(metadata.queryGeometry, metadata.queryOptions);
} else {
features = frontend.getRenderer()->queryRenderedFeatures(metadata.queryGeometryBox, metadata.queryOptions);
}
checkQueryTestResults(std::move(result.image), std::move(features), metadata);
}
}
mbgl::HeadlessFrontend::RenderResult TestRunner::runTest(TestMetadata& metadata, TestContext& ctx) {
HeadlessFrontend::RenderResult result{};
for (const auto& operation : parseTestOperations(metadata)) {
if (!operation(ctx)) return result;
}
try {
if (metadata.outputsImage) result = ctx.getFrontend().render(ctx.getMap());
} catch (const std::exception& e) {
ctx.getMetadata().errorMessage = std::string("Renering raised an exception: ") + e.what();
}
return result;
}
void TestRunner::reset() {
maps.clear();
}
|