File: privacy_math_perftest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (190 lines) | stat: -rw-r--r-- 5,966 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/attribution_reporting/privacy_math.h"

#include <stdint.h>

#include <limits>
#include <optional>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "base/timer/lap_timer.h"
#include "base/types/expected.h"
#include "components/attribution_reporting/attribution_scopes_data.h"
#include "components/attribution_reporting/attribution_scopes_set.h"
#include "components/attribution_reporting/event_report_windows.h"
#include "components/attribution_reporting/max_event_level_reports.h"
#include "components/attribution_reporting/source_type.mojom.h"
#include "components/attribution_reporting/test_utils.h"
#include "components/attribution_reporting/trigger_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_result_reporter.h"
#include "third_party/google_benchmark/src/include/benchmark/benchmark.h"

namespace attribution_reporting {
namespace {

struct TriggerConfig {
  int max_reports;
  int num_windows;
  int trigger_data_cardinality;
};

struct ScopesConfig {
  uint32_t attribution_scope_limit;
  uint32_t max_event_states;
};

using ScopedTriggerConfig =
    std::tuple<TriggerConfig, std::optional<ScopesConfig>>;

std::string StoryName(const ScopedTriggerConfig& p) {
  std::stringstream name;

  const TriggerConfig& tc = std::get<0>(p);
  name << tc.max_reports << "r_"  //
       << tc.num_windows << "w_"  //
       << tc.trigger_data_cardinality << "t";

  if (const std::optional<ScopesConfig>& scopes = std::get<1>(p)) {
    name << "_" << scopes->attribution_scope_limit << "s_"  //
         << scopes->max_event_states << "m_scoped";
  }

  return std::move(name).str();
}

constexpr TriggerConfig kTriggerConfigs[] = {
    // default navigation source
    {
        .max_reports = 3,
        .num_windows = 3,
        .trigger_data_cardinality = 8,
    },
    // default event source
    {
        .max_reports = 1,
        .num_windows = 1,
        .trigger_data_cardinality = 2,
    },
    {
        .max_reports = 20,
        .num_windows = 5,
        .trigger_data_cardinality = 8,
    },
    {
        .max_reports = 20,
        .num_windows = 5,
        .trigger_data_cardinality = 32,
    },
};

constexpr std::optional<ScopesConfig> kScopeConfigs[] = {
    // null scopes
    std::nullopt,
    // simple scopes
    ScopesConfig{
        .attribution_scope_limit = 3,
        .max_event_states = 3,
    },
    ScopesConfig{
        .attribution_scope_limit = 5,
        .max_event_states = std::numeric_limits<uint32_t>::max(),
    },
    ScopesConfig{
        .attribution_scope_limit = 10,
        .max_event_states = std::numeric_limits<uint32_t>::max(),
    },
    ScopesConfig{
        .attribution_scope_limit = 20,
        .max_event_states = std::numeric_limits<uint32_t>::max(),
    },
};

class PrivacyMathPerfTest
    : public testing::Test,
      public testing::WithParamInterface<ScopedTriggerConfig> {
 protected:
  template <typename Func>
  void Run(const std::string& metric_basename, Func&& func) const {
    const auto& [tc, sc] = GetParam();

    const EventReportWindows event_report_windows =
        EventReportWindowsWithCount(tc.num_windows);
    const TriggerDataSet trigger_data =
        TriggerDataSetWithCardinality(tc.trigger_data_cardinality);
    const MaxEventLevelReports max_event_level_reports(tc.max_reports);

    std::optional<AttributionScopesData> scopes;
    if (sc.has_value()) {
      scopes = AttributionScopesData::Create(AttributionScopesSet({"1"}),
                                             sc->attribution_scope_limit,
                                             sc->max_event_states);
      ASSERT_TRUE(scopes);
    }

    base::LapTimer timer;
    do {
      auto result = func(trigger_data, event_report_windows,
                         max_event_level_reports, scopes);
      ::benchmark::DoNotOptimize(result);
      timer.NextLap();
    } while (!timer.HasTimeLimitExpired());

    perf_test::PerfResultReporter reporter(metric_basename,
                                           StoryName(GetParam()));
    reporter.RegisterImportantMetric(".wall_time", "us");
    reporter.AddResult(".wall_time", timer.TimePerLap());
  }
};

TEST_P(PrivacyMathPerfTest, NumStates) {
  if (std::get<1>(GetParam())) {
    GTEST_SKIP();
  }
  Run("AttributionReporting.NumStates",
      [](const TriggerDataSet& trigger_data,
         const EventReportWindows& event_report_windows,
         const MaxEventLevelReports max_event_level_reports,
         const std::optional<AttributionScopesData>& scopes) {
        return GetNumStates(trigger_data, event_report_windows,
                            max_event_level_reports);
      });
}

TEST_P(PrivacyMathPerfTest, RandomizedResponse) {
  constexpr PrivacyMathConfig kConfig{
      .max_channel_capacity_navigation =
          std::numeric_limits<double>::infinity(),
      .max_channel_capacity_event = std::numeric_limits<double>::infinity(),
  };

  Run("AttributionReporting.RandomizedResponse",
      [&](const TriggerDataSet& trigger_data,
          const EventReportWindows& event_report_windows,
          const MaxEventLevelReports max_event_level_reports,
          const std::optional<AttributionScopesData>& scopes) {
        return DoRandomizedResponse(
            trigger_data, event_report_windows, max_event_level_reports,
            /*epsilon=*/0,
            /*source_type=*/mojom::SourceType::kNavigation, scopes, kConfig);
      });
}

INSTANTIATE_TEST_SUITE_P(
    ,
    PrivacyMathPerfTest,
    testing::Combine(testing::ValuesIn(kTriggerConfigs),
                     testing::ValuesIn(kScopeConfigs)),
    [](const testing::TestParamInfo<ScopedTriggerConfig>& info) {
      return StoryName(info.param);
    });

}  // namespace
}  // namespace attribution_reporting