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 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
|
/* vim:set ts=2 sw=2 sts=0 et: */
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
#include "gtest/gtest.h"
#include "js/Conversions.h"
#include "mozilla/Telemetry.h"
#include "TelemetryFixture.h"
#include "TelemetryTestHelpers.h"
using namespace mozilla;
using namespace TelemetryTestHelpers;
TEST_F(TelemetryTestFixture, AccumulateCountHistogram) {
const uint32_t kExpectedValue = 200;
AutoJSContextWithGlobal cx(mCleanGlobal);
const char* telemetryTestCountName =
Telemetry::GetHistogramName(Telemetry::TELEMETRY_TEST_COUNT);
ASSERT_STREQ(telemetryTestCountName, "TELEMETRY_TEST_COUNT")
<< "The histogram name is wrong";
GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
false);
// Accumulate in the histogram
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_COUNT, kExpectedValue / 2);
Telemetry::Accumulate("TELEMETRY_TEST_COUNT", kExpectedValue / 2);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, telemetryTestCountName, &snapshot,
false);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), telemetryTestCountName, snapshot, &histogram);
// Get "sum" property from histogram
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
// Check that the "sum" stored in the histogram matches with |kExpectedValue|
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, kExpectedValue)
<< "The histogram is not returning expected value";
}
TEST_F(TelemetryTestFixture, AccumulateKeyedCountHistogram) {
const uint32_t kExpectedValue = 100;
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_COUNT"_ns, true);
// Accumulate data in the provided key within the histogram
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_COUNT, "sample"_ns,
kExpectedValue);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_COUNT",
&snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_COUNT", snapshot,
&histogram);
// Get "sample" property from histogram
JS::Rooted<JS::Value> expectedKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sample", histogram, &expectedKeyData);
// Get "sum" property from keyed data
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", expectedKeyData, &sum);
// Check that the sum stored in the histogram matches with |kExpectedValue|
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, kExpectedValue)
<< "The histogram is not returning expected sum";
}
TEST_F(TelemetryTestFixture, TestKeyedKeysHistogram) {
AutoJSContextWithGlobal cx(mCleanGlobal);
JS::Rooted<JS::Value> testHistogram(cx.GetJSContext());
JS::Rooted<JS::Value> rval(cx.GetJSContext());
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_KEYS"_ns, true);
// Test the accumulation on both the allowed and unallowed keys, using
// the API that accepts histogram IDs.
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_KEYS, "not-allowed"_ns,
1);
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_KEYS, "testkey"_ns, 0);
// Do the same, using the API that accepts the histogram name as a string.
Telemetry::Accumulate("TELEMETRY_TEST_KEYED_KEYS", "not-allowed"_ns, 1);
Telemetry::Accumulate("TELEMETRY_TEST_KEYED_KEYS", "CommonKey"_ns, 1);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_KEYS",
&snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_KEYS", snapshot,
&histogram);
// Get "testkey" property from histogram and check that it stores the correct
// data.
JS::Rooted<JS::Value> expectedKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "testkey", histogram, &expectedKeyData);
ASSERT_TRUE(!expectedKeyData.isUndefined())
<< "Cannot find the expected key in the histogram data";
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", expectedKeyData, &sum);
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, 0U)
<< "The histogram is not returning expected sum for 'testkey'";
// Do the same for the "CommonKey" property.
GetProperty(cx.GetJSContext(), "CommonKey", histogram, &expectedKeyData);
ASSERT_TRUE(!expectedKeyData.isUndefined())
<< "Cannot find the expected key in the histogram data";
GetProperty(cx.GetJSContext(), "sum", expectedKeyData, &sum);
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, 1U)
<< "The histogram is not returning expected sum for 'CommonKey'";
GetProperty(cx.GetJSContext(), "not-allowed", histogram, &expectedKeyData);
ASSERT_TRUE(expectedKeyData.isUndefined())
<< "Unallowed keys must not be recorded in the histogram data";
// The 'not-allowed' key accumulation for 'TELEMETRY_TESTED_KEYED_KEYS' was
// attemtped twice, so we expect the count of
// 'telemetry.accumulate_unknown_histogram_keys' to be 2
const uint32_t expectedAccumulateUnknownCount = 2;
JS::Rooted<JS::Value> scalarsSnapshot(cx.GetJSContext());
GetScalarsSnapshot(true, cx.GetJSContext(), &scalarsSnapshot);
CheckKeyedUintScalar("telemetry.accumulate_unknown_histogram_keys",
"TELEMETRY_TEST_KEYED_KEYS", cx.GetJSContext(),
scalarsSnapshot, expectedAccumulateUnknownCount);
}
TEST_F(TelemetryTestFixture, AccumulateCategoricalHistogram) {
const uint32_t kExpectedValue = 2;
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_CATEGORICAL"_ns, false);
// Accumulate one unit into the categorical histogram with label
// Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel
Telemetry::AccumulateCategorical(
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel);
// Accumulate another unit into the same categorical histogram using a string
// label
Telemetry::AccumulateCategorical(Telemetry::TELEMETRY_TEST_CATEGORICAL,
"CommonLabel"_ns);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_CATEGORICAL",
&snapshot, false);
// Get our histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_CATEGORICAL", snapshot,
&histogram);
// Get values object from histogram. Each entry in the object maps to a label
// in the histogram.
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", histogram, &values);
// Get the value for the label we care about
JS::Rooted<JS::Value> value(cx.GetJSContext());
GetElement(cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel),
values, &value);
// Check that the value stored in the histogram matches with |kExpectedValue|
uint32_t uValue = 0;
JS::ToUint32(cx.GetJSContext(), value, &uValue);
ASSERT_EQ(uValue, kExpectedValue)
<< "The histogram is not returning expected value";
}
TEST_F(TelemetryTestFixture, AccumulateKeyedCategoricalHistogram) {
const uint32_t kSampleExpectedValue = 2;
const uint32_t kOtherSampleExpectedValue = 1;
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_CATEGORICAL"_ns, true);
// Accumulate one unit into the categorical histogram with label
// Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel
Telemetry::AccumulateCategoricalKeyed(
"sample"_ns,
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel);
// Accumulate another unit into the same categorical histogram
Telemetry::AccumulateCategoricalKeyed(
"sample"_ns,
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel);
// Accumulate another unit into a different categorical histogram
Telemetry::AccumulateCategoricalKeyed(
"other-sample"_ns,
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_CATEGORICAL", &snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_CATEGORICAL", snapshot,
&histogram);
// Check that the sample histogram contains the values we expect
JS::Rooted<JS::Value> sample(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sample", histogram, &sample);
// Get values object from sample. Each entry in the object maps to a label in
// the histogram.
JS::Rooted<JS::Value> sampleValues(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", sample, &sampleValues);
// Get the value for the label we care about
JS::Rooted<JS::Value> sampleValue(cx.GetJSContext());
GetElement(
cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel),
sampleValues, &sampleValue);
// Check that the value stored in the histogram matches with
// |kSampleExpectedValue|
uint32_t uSampleValue = 0;
JS::ToUint32(cx.GetJSContext(), sampleValue, &uSampleValue);
ASSERT_EQ(uSampleValue, kSampleExpectedValue)
<< "The sample histogram is not returning expected value";
// Check that the other-sample histogram contains the values we expect
JS::Rooted<JS::Value> otherSample(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "other-sample", histogram, &otherSample);
// Get values object from the other-sample. Each entry in the object maps to a
// label in the histogram.
JS::Rooted<JS::Value> otherValues(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", otherSample, &otherValues);
// Get the value for the label we care about
JS::Rooted<JS::Value> otherValue(cx.GetJSContext());
GetElement(
cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel),
otherValues, &otherValue);
// Check that the value stored in the histogram matches with
// |kOtherSampleExpectedValue|
uint32_t uOtherValue = 0;
JS::ToUint32(cx.GetJSContext(), otherValue, &uOtherValue);
ASSERT_EQ(uOtherValue, kOtherSampleExpectedValue)
<< "The other-sample histogram is not returning expected value";
}
TEST_F(TelemetryTestFixture, AccumulateCountHistogram_MultipleSamples) {
nsTArray<uint32_t> samples({4, 4, 4});
const uint32_t kExpectedSum = 12;
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
false);
// Accumulate in histogram
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_COUNT, samples);
// Get a snapshot of all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT", &snapshot,
false);
// Get histogram from snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_COUNT", snapshot, &histogram);
// Get "sum" from histogram
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
// Check that sum matches with aValue
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, kExpectedSum)
<< "This histogram is not returning expected value";
}
TEST_F(TelemetryTestFixture, AccumulateLinearHistogram_MultipleSamples) {
nsTArray<uint32_t> samples({4, 4, 4});
const uint32_t kExpectedCount = 3;
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_LINEAR"_ns, false);
// Accumulate in the histogram
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_LINEAR, samples);
// Get a snapshot of all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_LINEAR",
&snapshot, false);
// Get histogram from snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_LINEAR", snapshot, &histogram);
// Get "values" object from histogram
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", histogram, &values);
// Index 0 is only for values less than 'low'. Values within range start at
// index 1
JS::Rooted<JS::Value> count(cx.GetJSContext());
const uint32_t index = 1;
GetElement(cx.GetJSContext(), index, values, &count);
// Check that this count matches with nSamples
uint32_t uCount = 0;
JS::ToUint32(cx.GetJSContext(), count, &uCount);
ASSERT_EQ(uCount, kExpectedCount)
<< "The histogram did not accumulate the correct number of values";
}
TEST_F(TelemetryTestFixture, AccumulateLinearHistogram_DifferentSamples) {
nsTArray<uint32_t> samples(
{4, 8, 2147483646, uint32_t(INT_MAX) + 1, UINT32_MAX});
AutoJSContextWithGlobal cx(mCleanGlobal);
mTelemetry->ClearScalars();
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_LINEAR"_ns, false);
// Accumulate in histogram
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_LINEAR, samples);
// Get a snapshot of all histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_LINEAR",
&snapshot, false);
// Get histogram from snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_LINEAR", snapshot, &histogram);
// Get values object from histogram
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", histogram, &values);
// Get values in first and last buckets
JS::Rooted<JS::Value> countFirst(cx.GetJSContext());
JS::Rooted<JS::Value> countLast(cx.GetJSContext());
const uint32_t firstIndex = 1;
// Buckets are indexed by their start value
const uint32_t lastIndex = INT32_MAX - 1;
GetElement(cx.GetJSContext(), firstIndex, values, &countFirst);
GetElement(cx.GetJSContext(), lastIndex, values, &countLast);
// Check that the values match
uint32_t uCountFirst = 0;
uint32_t uCountLast = 0;
JS::ToUint32(cx.GetJSContext(), countFirst, &uCountFirst);
JS::ToUint32(cx.GetJSContext(), countLast, &uCountLast);
const uint32_t kExpectedCountFirst = 2;
// We expect 2147483646 to be in the last bucket, as well the two samples
// above 2^31 (prior to bug 1438335, values between INT_MAX and UINT32_MAX
// would end up as 0s)
const uint32_t kExpectedCountLast = 3;
ASSERT_EQ(uCountFirst, kExpectedCountFirst)
<< "The first bucket did not accumulate the correct number of values";
ASSERT_EQ(uCountLast, kExpectedCountLast)
<< "The last bucket did not accumulate the correct number of values";
// We accumulated two values that had to be clamped. We expect the count in
// 'telemetry.accumulate_clamped_values' to be 2 (only one storage).
const uint32_t expectedAccumulateClampedCount = 2;
JS::Rooted<JS::Value> scalarsSnapshot(cx.GetJSContext());
GetScalarsSnapshot(true, cx.GetJSContext(), &scalarsSnapshot);
CheckKeyedUintScalar("telemetry.accumulate_clamped_values",
"TELEMETRY_TEST_LINEAR", cx.GetJSContext(),
scalarsSnapshot, expectedAccumulateClampedCount);
}
TEST_F(TelemetryTestFixture, AccumulateKeyedCountHistogram_MultipleSamples) {
const nsTArray<uint32_t> samples({5, 10, 15});
const uint32_t kExpectedSum = 5 + 10 + 15;
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_COUNT"_ns, true);
// Accumulate data in the provided key within the histogram
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_COUNT, "sample"_ns,
samples);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_COUNT",
&snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_COUNT", snapshot,
&histogram);
// Get "sample" property from histogram
JS::Rooted<JS::Value> expectedKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sample", histogram, &expectedKeyData);
// Get "sum" property from keyed data
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", expectedKeyData, &sum);
// Check that the sum stored in the histogram matches with |kExpectedSum|
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, kExpectedSum)
<< "The histogram is not returning expected sum";
}
TEST_F(TelemetryTestFixture, TestKeyedLinearHistogram_MultipleSamples) {
AutoJSContextWithGlobal cx(mCleanGlobal);
mTelemetry->ClearScalars();
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_LINEAR"_ns, true);
const nsTArray<uint32_t> samples({1, 5, 250000, UINT_MAX});
// Test the accumulation on the key 'testkey', using
// the API that accepts histogram IDs.
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_LINEAR, "testkey"_ns,
samples);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_LINEAR",
&snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_LINEAR", snapshot,
&histogram);
// Get "testkey" property from histogram.
JS::Rooted<JS::Value> expectedKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "testkey", histogram, &expectedKeyData);
ASSERT_TRUE(!expectedKeyData.isUndefined())
<< "Cannot find the expected key in the histogram data";
// Get values object from 'testkey' histogram.
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", expectedKeyData, &values);
// Get values in first and last buckets.
JS::Rooted<JS::Value> countFirst(cx.GetJSContext());
JS::Rooted<JS::Value> countLast(cx.GetJSContext());
const uint32_t firstIndex = 1;
// Buckets are indexed by their start value
const uint32_t lastIndex = 250000;
GetElement(cx.GetJSContext(), firstIndex, values, &countFirst);
GetElement(cx.GetJSContext(), lastIndex, values, &countLast);
// Check that the values match.
uint32_t uCountFirst = 0;
uint32_t uCountLast = 0;
JS::ToUint32(cx.GetJSContext(), countFirst, &uCountFirst);
JS::ToUint32(cx.GetJSContext(), countLast, &uCountLast);
const uint32_t kExpectedCountFirst = 2;
const uint32_t kExpectedCountLast = 2;
ASSERT_EQ(uCountFirst, kExpectedCountFirst)
<< "The first bucket did not accumulate the correct number of values for "
"key 'testkey'";
ASSERT_EQ(uCountLast, kExpectedCountLast)
<< "The last bucket did not accumulate the correct number of values for "
"key 'testkey'";
// We accumulated one keyed values that had to be clamped. We expect the
// count in 'telemetry.accumulate_clamped_values' to be 1
const uint32_t expectedAccumulateClampedCount = 1;
JS::Rooted<JS::Value> scalarsSnapshot(cx.GetJSContext());
GetScalarsSnapshot(true, cx.GetJSContext(), &scalarsSnapshot);
CheckKeyedUintScalar("telemetry.accumulate_clamped_values",
"TELEMETRY_TEST_KEYED_LINEAR", cx.GetJSContext(),
scalarsSnapshot, expectedAccumulateClampedCount);
}
TEST_F(TelemetryTestFixture, TestKeyedKeysHistogram_MultipleSamples) {
AutoJSContextWithGlobal cx(mCleanGlobal);
mTelemetry->ClearScalars();
const nsTArray<uint32_t> samples({false, false, true, 32, true});
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_KEYS"_ns, true);
// Test the accumulation on both the allowed and unallowed keys, using
// the API that accepts histogram IDs.
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_KEYS, "not-allowed"_ns,
samples);
Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_KEYS, "testkey"_ns,
samples);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_KEYS",
&snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_KEYS", snapshot,
&histogram);
// Get "testkey" property from histogram and check that it stores the correct
// data.
JS::Rooted<JS::Value> testKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "testkey", histogram, &testKeyData);
ASSERT_TRUE(!testKeyData.isUndefined())
<< "Cannot find the key 'testkey' in the histogram data";
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", testKeyData, &values);
// Get values in buckets 0,1,2
const uint32_t falseIndex = 0;
const uint32_t trueIndex = 1;
const uint32_t otherIndex = 2;
JS::Rooted<JS::Value> countFalse(cx.GetJSContext());
JS::Rooted<JS::Value> countTrue(cx.GetJSContext());
JS::Rooted<JS::Value> countOther(cx.GetJSContext());
GetElement(cx.GetJSContext(), falseIndex, values, &countFalse);
GetElement(cx.GetJSContext(), trueIndex, values, &countTrue);
GetElement(cx.GetJSContext(), otherIndex, values, &countOther);
uint32_t uCountFalse = 0;
uint32_t uCountTrue = 0;
uint32_t uCountOther = 0;
JS::ToUint32(cx.GetJSContext(), countFalse, &uCountFalse);
JS::ToUint32(cx.GetJSContext(), countTrue, &uCountTrue);
JS::ToUint32(cx.GetJSContext(), countOther, &uCountOther);
const uint32_t kExpectedCountFalse = 2;
const uint32_t kExpectedCountTrue = 3;
const uint32_t kExpectedCountOther = 0;
ASSERT_EQ(uCountFalse, kExpectedCountFalse)
<< "The histogram did not accumulate the correct number of 'false' "
"booleans for key 'testkey'";
ASSERT_EQ(uCountTrue, kExpectedCountTrue)
<< "The histogram did not accumulate the correct number of 'true' "
"booleans for key 'testkey'";
ASSERT_EQ(uCountOther, kExpectedCountOther)
<< "The histogram did not accumulate the correct number of undefined "
"values for key 'testkey'";
// Here we check that we are not accumulating to a different (but still
// 'allowed') key. Get "CommonKey" property from histogram and check that it
// has no data. Since we accumulated no data to it, commonKeyData should be
// undefined.
JS::Rooted<JS::Value> commonKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "CommonKey", histogram, &commonKeyData);
ASSERT_TRUE(commonKeyData.isUndefined())
<< "Found data in key 'CommonKey' even though we accumulated no data to "
"it";
// Here we check that our function does not allow accumulation into unallowed
// keys. Get 'not-allowed' property from histogram and check that this also
// has no data. This should contain no data because this key is not allowed.
JS::Rooted<JS::Value> notAllowedKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "not-allowed", histogram, ¬AllowedKeyData);
ASSERT_TRUE(notAllowedKeyData.isUndefined())
<< "Found data in key 'not-allowed' even though accumuling data to it is "
"not allowed";
// The 'not-allowed' key accumulation for 'TELEMETRY_TESTED_KEYED_KEYS' was
// attemtped once, so we expect the count of
// 'telemetry.accumulate_unknown_histogram_keys' to be 1
const uint32_t expectedAccumulateUnknownCount = 1;
JS::Rooted<JS::Value> scalarsSnapshot(cx.GetJSContext());
GetScalarsSnapshot(true, cx.GetJSContext(), &scalarsSnapshot);
CheckKeyedUintScalar("telemetry.accumulate_unknown_histogram_keys",
"TELEMETRY_TEST_KEYED_KEYS", cx.GetJSContext(),
scalarsSnapshot, expectedAccumulateUnknownCount);
}
TEST_F(TelemetryTestFixture,
AccumulateCategoricalHistogram_MultipleStringLabels) {
const uint32_t kExpectedValue = 2;
const nsTArray<nsCString> labels({"CommonLabel"_ns, "CommonLabel"_ns});
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_CATEGORICAL"_ns, false);
// Accumulate the units into a categorical histogram using a string label
Telemetry::AccumulateCategorical(Telemetry::TELEMETRY_TEST_CATEGORICAL,
labels);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_CATEGORICAL",
&snapshot, false);
// Get our histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_CATEGORICAL", snapshot,
&histogram);
// Get values object from histogram. Each entry in the object maps to a label
// in the histogram.
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", histogram, &values);
// Get the value for the label we care about
JS::Rooted<JS::Value> value(cx.GetJSContext());
GetElement(cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel),
values, &value);
// Check that the value stored in the histogram matches with |kExpectedValue|
uint32_t uValue = 0;
JS::ToUint32(cx.GetJSContext(), value, &uValue);
ASSERT_EQ(uValue, kExpectedValue)
<< "The histogram is not returning expected value";
// Now we check for no accumulation when a bad label is present in the array.
//
// The 'values' property is not initialized unless data is accumulated so
// keeping another test to check for this case alone is wasteful as we will
// have to accumulate some data anyway.
const nsTArray<nsCString> badLabelArray({"CommonLabel"_ns, "BadLabel"_ns});
// Try to accumulate the array into the histogram.
Telemetry::AccumulateCategorical(Telemetry::TELEMETRY_TEST_CATEGORICAL,
badLabelArray);
// Get snapshot of all the histograms
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_CATEGORICAL",
&snapshot, false);
// Get our histogram from the snapshot
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_CATEGORICAL", snapshot,
&histogram);
// Get values array from histogram
GetProperty(cx.GetJSContext(), "values", histogram, &values);
// Get the value for the label we care about
GetElement(cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel),
values, &value);
// Check that the value stored in the histogram matches with |kExpectedValue|
uValue = 0;
JS::ToUint32(cx.GetJSContext(), value, &uValue);
ASSERT_EQ(uValue, kExpectedValue)
<< "The histogram accumulated data when it should not have";
}
TEST_F(TelemetryTestFixture,
AccumulateCategoricalHistogram_MultipleEnumValues) {
const uint32_t kExpectedValue = 2;
const nsTArray<Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL> enumLabels(
{Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel,
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel});
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_CATEGORICAL"_ns, false);
// Accumulate the units into a categorical histogram using the enumLabels
// array
Telemetry::AccumulateCategorical<
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL>(enumLabels);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_CATEGORICAL",
&snapshot, false);
// Get our histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_CATEGORICAL", snapshot,
&histogram);
// Get values object from histogram. Each entry in the object maps to a label
// in the histogram.
JS::Rooted<JS::Value> values(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", histogram, &values);
// Get the value for the label we care about
JS::Rooted<JS::Value> value(cx.GetJSContext());
GetElement(cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_CATEGORICAL::CommonLabel),
values, &value);
// Check that the value stored in the histogram matches with |kExpectedValue|
uint32_t uValue = 0;
JS::ToUint32(cx.GetJSContext(), value, &uValue);
ASSERT_EQ(uValue, kExpectedValue)
<< "The histogram is not returning expected value";
}
TEST_F(TelemetryTestFixture,
AccumulateKeyedCategoricalHistogram_MultipleEnumValues) {
const uint32_t kExpectedCommonLabel = 2;
const uint32_t kExpectedLabel2 = 1;
const nsTArray<Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL> enumLabels(
{Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel,
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel,
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::Label2});
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_CATEGORICAL"_ns, true);
// Accumulate the array into the categorical keyed histogram
Telemetry::AccumulateCategoricalKeyed("sampleKey"_ns, enumLabels);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_CATEGORICAL", &snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_CATEGORICAL", snapshot,
&histogram);
// Check that the sampleKey histogram contains correct number of CommonLabel
// samples
JS::Rooted<JS::Value> sample(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sampleKey", histogram, &sample);
// Get values object from the sample. Each entry in the object maps to a label
// in the histogram.
JS::Rooted<JS::Value> sampleKeyValues(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "values", sample, &sampleKeyValues);
// Get the count of CommonLabel
JS::Rooted<JS::Value> commonLabelValue(cx.GetJSContext());
GetElement(
cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::CommonLabel),
sampleKeyValues, &commonLabelValue);
// Check that the value stored in the histogram matches with
// |kExpectedCommonLabel|
uint32_t uCommonLabelValue = 0;
JS::ToUint32(cx.GetJSContext(), commonLabelValue, &uCommonLabelValue);
ASSERT_EQ(uCommonLabelValue, kExpectedCommonLabel)
<< "The sampleKey histogram did not accumulate the correct number of "
"CommonLabel samples";
// Check that the sampleKey histogram contains the correct number of Label2
// values Get the count of Label2
JS::Rooted<JS::Value> label2Value(cx.GetJSContext());
GetElement(cx.GetJSContext(),
static_cast<uint32_t>(
Telemetry::LABELS_TELEMETRY_TEST_KEYED_CATEGORICAL::Label2),
sampleKeyValues, &label2Value);
// Check that the value stored in the histogram matches with |kExpectedLabel2|
uint32_t uLabel2Value = 0;
JS::ToUint32(cx.GetJSContext(), label2Value, &uLabel2Value);
ASSERT_EQ(uLabel2Value, kExpectedLabel2)
<< "The sampleKey histogram did not accumulate the correct number of "
"Label2 samples";
}
TEST_F(TelemetryTestFixture, AccumulateTimeDelta) {
const uint32_t kExpectedValue = 100;
const TimeStamp start = TimeStamp::Now();
const TimeDuration delta = TimeDuration::FromMilliseconds(50);
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
false);
// Accumulate in the histogram
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_COUNT, start - delta,
start);
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_COUNT, start - delta,
start);
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_COUNT, start, start);
// end > start timestamp gives zero contribution
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_COUNT, start + delta,
start);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT", &snapshot,
false);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_COUNT", snapshot, &histogram);
// Get "sum" property from histogram
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
// Check that the "sum" stored in the histogram matches with |kExpectedValue|
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, kExpectedValue)
<< "The histogram is not returning expected value";
}
TEST_F(TelemetryTestFixture, AccumulateKeyedTimeDelta) {
const uint32_t kExpectedValue = 100;
const TimeStamp start = TimeStamp::Now();
const TimeDuration delta = TimeDuration::FromMilliseconds(50);
AutoJSContextWithGlobal cx(mCleanGlobal);
GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
"TELEMETRY_TEST_KEYED_COUNT"_ns, true);
// Accumulate time delta in the provided key within the histogram
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_KEYED_COUNT,
"sample"_ns, start - delta, start);
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_KEYED_COUNT,
"sample"_ns, start - delta, start);
// end > start timestamp gives zero contribution
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_KEYED_COUNT,
"sample"_ns, start + delta, start);
Telemetry::AccumulateTimeDelta(Telemetry::TELEMETRY_TEST_KEYED_COUNT,
"sample"_ns, start, start);
// Get a snapshot for all the histograms
JS::Rooted<JS::Value> snapshot(cx.GetJSContext());
GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_COUNT",
&snapshot, true);
// Get the histogram from the snapshot
JS::Rooted<JS::Value> histogram(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_COUNT", snapshot,
&histogram);
// Get "sample" property from histogram
JS::Rooted<JS::Value> expectedKeyData(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sample", histogram, &expectedKeyData);
// Get "sum" property from keyed data
JS::Rooted<JS::Value> sum(cx.GetJSContext());
GetProperty(cx.GetJSContext(), "sum", expectedKeyData, &sum);
// Check that the sum stored in the histogram matches with |kExpectedValue|
uint32_t uSum = 0;
JS::ToUint32(cx.GetJSContext(), sum, &uSum);
ASSERT_EQ(uSum, kExpectedValue)
<< "The histogram is not returning expected sum";
}
|