File: call_stack_profile_builder_unittest.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 (621 lines) | stat: -rw-r--r-- 25,376 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 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/metrics/call_stacks/call_stack_profile_builder.h"

#include <memory>

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/profiler/module_cache.h"
#include "base/profiler/stack_sampling_profiler_test_util.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/sampling_profiler/call_stack_profile_params.h"
#include "components/sampling_profiler/process_type.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/sampled_profile.pb.h"

namespace metrics {

namespace {

constexpr sampling_profiler::CallStackProfileParams kProfileParams = {
    sampling_profiler::ProfilerProcessType::kBrowser,
    sampling_profiler::ProfilerThreadType::kMain,
    sampling_profiler::CallStackProfileParams::Trigger::kProcessStartup};

class TestingCallStackProfileBuilder : public CallStackProfileBuilder {
 public:
  explicit TestingCallStackProfileBuilder(
      const sampling_profiler::CallStackProfileParams& profile_params,
      const WorkIdRecorder* work_id_recorder = nullptr,
      base::OnceClosure completed_callback = base::OnceClosure());

  ~TestingCallStackProfileBuilder() override;

  base::TimeTicks test_profile_start_time() const {
    return test_profile_start_time_;
  }

  const SampledProfile& test_sampled_profile() const {
    return test_sampled_profile_;
  }

 protected:
  // Overridden for testing.
  void PassProfilesToMetricsProvider(base::TimeTicks profile_start_time,
                                     SampledProfile sampled_profile) override;

 private:
  // The start time and completed profile.
  base::TimeTicks test_profile_start_time_;
  SampledProfile test_sampled_profile_;
};

TestingCallStackProfileBuilder::TestingCallStackProfileBuilder(
    const sampling_profiler::CallStackProfileParams& profile_params,
    const WorkIdRecorder* work_id_recorder,
    base::OnceClosure completed_callback)
    : CallStackProfileBuilder(profile_params,
                              work_id_recorder,
                              std::move(completed_callback)) {}

TestingCallStackProfileBuilder::~TestingCallStackProfileBuilder() = default;

void TestingCallStackProfileBuilder::PassProfilesToMetricsProvider(
    base::TimeTicks profile_start_time,
    SampledProfile sampled_profile) {
  test_profile_start_time_ = profile_start_time;
  test_sampled_profile_ = std::move(sampled_profile);
}

}  // namespace

TEST(CallStackProfileBuilderTest, ProfilingCompleted) {
  // Set up a mock completed callback which will be run once.
  base::MockCallback<base::OnceClosure> mock_closure;
  EXPECT_CALL(mock_closure, Run()).Times(1);

  auto profile_builder = std::make_unique<TestingCallStackProfileBuilder>(
      kProfileParams, nullptr, mock_closure.Get());
  base::MetadataRecorder metadata_recorder;

#if BUILDFLAG(IS_WIN)
  uint64_t module_md5 = 0x46C3E4166659AC02ULL;
  base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
#else
  uint64_t module_md5 = 0x554838A8451AC36CULL;
  base::FilePath module_path("/some/path/to/chrome");
#endif

  const uintptr_t module_base_address1 = 0x1000;
  base::TestModule module1(module_base_address1);
  module1.set_id("1");
  module1.set_debug_basename(module_path);
  base::Frame frame1 = {module_base_address1 + 0x10, &module1};

  const uintptr_t module_base_address2 = 0x1100;
  base::TestModule module2(module_base_address2);
  module2.set_id("2");
  module2.set_debug_basename(module_path);
  base::Frame frame2 = {module_base_address2 + 0x10, &module2};

  const uintptr_t module_base_address3 = 0x1010;
  base::TestModule module3(module_base_address3);
  module3.set_id("3");
  module3.set_debug_basename(module_path);
  base::Frame frame3 = {module_base_address3 + 0x10, &module3};

  std::vector<base::Frame> frames1 = {frame1, frame2};
  std::vector<base::Frame> frames2 = {frame3};

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames1, base::TimeTicks());
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames2, base::TimeTicks());
  profile_builder->OnProfileCompleted(base::Milliseconds(500),
                                      base::Milliseconds(100));

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_process());
  ASSERT_EQ(BROWSER_PROCESS, proto.process());
  ASSERT_TRUE(proto.has_thread());
  ASSERT_EQ(MAIN_THREAD, proto.thread());
  ASSERT_TRUE(proto.has_trigger_event());
  ASSERT_EQ(SampledProfile::PROCESS_STARTUP, proto.trigger_event());

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  ASSERT_EQ(2, profile.stack_size());
  ASSERT_EQ(2, profile.stack(0).frame_size());
  ASSERT_TRUE(profile.stack(0).frame(0).has_module_id_index());
  EXPECT_EQ(0, profile.stack(0).frame(0).module_id_index());
  ASSERT_TRUE(profile.stack(0).frame(1).has_module_id_index());
  EXPECT_EQ(1, profile.stack(0).frame(1).module_id_index());
  ASSERT_EQ(1, profile.stack(1).frame_size());
  ASSERT_TRUE(profile.stack(1).frame(0).has_module_id_index());
  EXPECT_EQ(2, profile.stack(1).frame(0).module_id_index());

  ASSERT_EQ(3, profile.module_id().size());
  ASSERT_TRUE(profile.module_id(0).has_build_id());
  EXPECT_EQ("1", profile.module_id(0).build_id());
  ASSERT_TRUE(profile.module_id(0).has_name_md5_prefix());
  EXPECT_EQ(module_md5, profile.module_id(0).name_md5_prefix());
  ASSERT_TRUE(profile.module_id(1).has_build_id());
  EXPECT_EQ("2", profile.module_id(1).build_id());
  ASSERT_TRUE(profile.module_id(1).has_name_md5_prefix());
  EXPECT_EQ(module_md5, profile.module_id(1).name_md5_prefix());
  ASSERT_TRUE(profile.module_id(2).has_build_id());
  EXPECT_EQ("3", profile.module_id(2).build_id());
  ASSERT_TRUE(profile.module_id(2).has_name_md5_prefix());
  EXPECT_EQ(module_md5, profile.module_id(2).name_md5_prefix());

  ASSERT_EQ(2, profile.stack_sample_size());
  EXPECT_EQ(0, profile.stack_sample(0).stack_index());
  EXPECT_FALSE(profile.stack_sample(0).has_continued_work());
  EXPECT_FALSE(profile.stack_sample(0).has_weight());
  EXPECT_EQ(1, profile.stack_sample(1).stack_index());
  EXPECT_FALSE(profile.stack_sample(1).has_continued_work());
  EXPECT_FALSE(profile.stack_sample(1).has_weight());

  ASSERT_TRUE(profile.has_profile_duration_ms());
  EXPECT_EQ(500, profile.profile_duration_ms());
  ASSERT_TRUE(profile.has_sampling_period_ms());
  EXPECT_EQ(100, profile.sampling_period_ms());
}

TEST(CallStackProfileBuilderTest, CustomWeightsAndCounts) {
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams);

  base::TestModule module1;
  base::Frame frame1 = {0x10, &module1};
  std::vector<base::Frame> frames = {frame1};

  profile_builder->OnSampleCompleted(frames, base::TimeTicks(), 42, 3);
  profile_builder->OnSampleCompleted(frames, base::TimeTicks(), 1, 1);
  profile_builder->OnSampleCompleted(frames, base::TimeTicks());
  profile_builder->OnProfileCompleted(base::TimeDelta(), base::TimeDelta());

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();
  ASSERT_EQ(3, profile.stack_sample_size());
  EXPECT_TRUE(profile.stack_sample(0).has_weight());
  EXPECT_TRUE(profile.stack_sample(0).has_count());
  EXPECT_EQ(42, profile.stack_sample(0).weight());
  EXPECT_EQ(3, profile.stack_sample(0).count());
  EXPECT_FALSE(profile.stack_sample(1).has_weight());
  EXPECT_FALSE(profile.stack_sample(1).has_count());
  EXPECT_FALSE(profile.stack_sample(2).has_weight());
  EXPECT_FALSE(profile.stack_sample(2).has_count());
}

TEST(CallStackProfileBuilderTest, StacksDeduped) {
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams);
  base::MetadataRecorder metadata_recorder;

  base::TestModule module1;
  base::Frame frame1 = {0x10, &module1};

  base::TestModule module2;
  base::Frame frame2 = {0x20, &module2};

  std::vector<base::Frame> frames = {frame1, frame2};

  // Two stacks are completed with the same frames therefore they are deduped
  // to one.
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames, base::TimeTicks());
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames, base::TimeTicks());

  profile_builder->OnProfileCompleted(base::TimeDelta(), base::TimeDelta());

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_process());
  ASSERT_EQ(BROWSER_PROCESS, proto.process());
  ASSERT_TRUE(proto.has_thread());
  ASSERT_EQ(MAIN_THREAD, proto.thread());
  ASSERT_TRUE(proto.has_trigger_event());
  ASSERT_EQ(SampledProfile::PROCESS_STARTUP, proto.trigger_event());

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();
  ASSERT_EQ(1, profile.stack_size());
  ASSERT_EQ(2, profile.stack_sample_size());
  EXPECT_EQ(0, profile.stack_sample(0).stack_index());
  EXPECT_EQ(0, profile.stack_sample(1).stack_index());
}

TEST(CallStackProfileBuilderTest, StacksNotDeduped) {
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams);
  base::MetadataRecorder metadata_recorder;

  base::TestModule module1;
  base::Frame frame1 = {0x10, &module1};

  base::TestModule module2;
  base::Frame frame2 = {0x20, &module2};

  std::vector<base::Frame> frames1 = {frame1};
  std::vector<base::Frame> frames2 = {frame2};

  // Two stacks are completed with the different frames therefore not deduped.
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames1, base::TimeTicks());
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames2, base::TimeTicks());

  profile_builder->OnProfileCompleted(base::TimeDelta(), base::TimeDelta());

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_process());
  ASSERT_EQ(BROWSER_PROCESS, proto.process());
  ASSERT_TRUE(proto.has_thread());
  ASSERT_EQ(MAIN_THREAD, proto.thread());
  ASSERT_TRUE(proto.has_trigger_event());
  ASSERT_EQ(SampledProfile::PROCESS_STARTUP, proto.trigger_event());

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();
  ASSERT_EQ(2, profile.stack_size());
  ASSERT_EQ(2, profile.stack_sample_size());
  EXPECT_EQ(0, profile.stack_sample(0).stack_index());
  EXPECT_EQ(1, profile.stack_sample(1).stack_index());
}

TEST(CallStackProfileBuilderTest, Modules) {
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams);
  base::MetadataRecorder metadata_recorder;

  // A frame with no module.
  base::Frame frame1 = {0x1010, nullptr};

  const uintptr_t module_base_address2 = 0x1100;
#if BUILDFLAG(IS_WIN)
  uint64_t module_md5 = 0x46C3E4166659AC02ULL;
  base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
#else
  uint64_t module_md5 = 0x554838A8451AC36CULL;
  base::FilePath module_path("/some/path/to/chrome");
#endif
  base::TestModule module2(module_base_address2);
  module2.set_id("2");
  module2.set_debug_basename(module_path);
  base::Frame frame2 = {module_base_address2 + 0x10, &module2};

  std::vector<base::Frame> frames = {frame1, frame2};

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames, base::TimeTicks());
  profile_builder->OnProfileCompleted(base::TimeDelta(), base::TimeDelta());

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  ASSERT_EQ(1, profile.stack_sample_size());
  EXPECT_EQ(0, profile.stack_sample(0).stack_index());

  ASSERT_EQ(1, profile.stack_size());
  ASSERT_EQ(2, profile.stack(0).frame_size());

  ASSERT_FALSE(profile.stack(0).frame(0).has_module_id_index());
  ASSERT_FALSE(profile.stack(0).frame(0).has_address());

  ASSERT_TRUE(profile.stack(0).frame(1).has_module_id_index());
  EXPECT_EQ(0, profile.stack(0).frame(1).module_id_index());
  ASSERT_TRUE(profile.stack(0).frame(1).has_address());
  EXPECT_EQ(0x10ULL, profile.stack(0).frame(1).address());

  ASSERT_EQ(1, profile.module_id().size());
  ASSERT_TRUE(profile.module_id(0).has_build_id());
  EXPECT_EQ("2", profile.module_id(0).build_id());
  ASSERT_TRUE(profile.module_id(0).has_name_md5_prefix());
  EXPECT_EQ(module_md5, profile.module_id(0).name_md5_prefix());
}

TEST(CallStackProfileBuilderTest, DedupModules) {
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams);
  base::MetadataRecorder metadata_recorder;

  const uintptr_t module_base_address = 0x1000;

#if BUILDFLAG(IS_WIN)
  uint64_t module_md5 = 0x46C3E4166659AC02ULL;
  base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
#else
  uint64_t module_md5 = 0x554838A8451AC36CULL;
  base::FilePath module_path("/some/path/to/chrome");
#endif

  base::TestModule module(module_base_address);
  module.set_id("1");
  module.set_debug_basename(module_path);
  base::Frame frame1 = {module_base_address + 0x10, &module};
  base::Frame frame2 = {module_base_address + 0x20, &module};

  std::vector<base::Frame> frames = {frame1, frame2};

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(frames, base::TimeTicks());
  profile_builder->OnProfileCompleted(base::TimeDelta(), base::TimeDelta());

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  ASSERT_EQ(1, profile.stack_sample_size());
  EXPECT_EQ(0, profile.stack_sample(0).stack_index());

  ASSERT_EQ(1, profile.stack_size());
  ASSERT_EQ(2, profile.stack(0).frame_size());

  // The two frames share the same module, which should be deduped in the
  // output.
  ASSERT_TRUE(profile.stack(0).frame(0).has_module_id_index());
  EXPECT_EQ(0, profile.stack(0).frame(0).module_id_index());
  ASSERT_TRUE(profile.stack(0).frame(0).has_address());
  EXPECT_EQ(0x10ULL, profile.stack(0).frame(0).address());

  ASSERT_TRUE(profile.stack(0).frame(1).has_module_id_index());
  EXPECT_EQ(0, profile.stack(0).frame(1).module_id_index());
  ASSERT_TRUE(profile.stack(0).frame(1).has_address());
  EXPECT_EQ(0x20ULL, profile.stack(0).frame(1).address());

  ASSERT_EQ(1, profile.module_id().size());
  ASSERT_TRUE(profile.module_id(0).has_build_id());
  EXPECT_EQ("1", profile.module_id(0).build_id());
  ASSERT_TRUE(profile.module_id(0).has_name_md5_prefix());
  EXPECT_EQ(module_md5, profile.module_id(0).name_md5_prefix());
}

TEST(CallStackProfileBuilderTest, WorkIds) {
  class TestWorkIdRecorder : public WorkIdRecorder {
   public:
    unsigned int RecordWorkId() const override { return current_id; }

    unsigned int current_id = 0;
  };

  TestWorkIdRecorder work_id_recorder;
  auto profile_builder = std::make_unique<TestingCallStackProfileBuilder>(
      kProfileParams, &work_id_recorder);
  base::MetadataRecorder metadata_recorder;

  base::TestModule module;
  base::Frame frame = {0x10, &module};

  // Id 0 means the message loop hasn't been started yet, so the sample should
  // not have continued_work set.
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, base::TimeTicks());

  // The second sample with the same id should have continued_work set.
  work_id_recorder.current_id = 1;
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, base::TimeTicks());
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, base::TimeTicks());

  // Ids are in general non-contiguous across multiple samples.
  work_id_recorder.current_id = 10;
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, base::TimeTicks());
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, base::TimeTicks());

  profile_builder->OnProfileCompleted(base::Milliseconds(500),
                                      base::Milliseconds(100));

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  ASSERT_EQ(5, profile.stack_sample_size());
  EXPECT_FALSE(profile.stack_sample(0).has_continued_work());
  EXPECT_FALSE(profile.stack_sample(1).has_continued_work());
  EXPECT_TRUE(profile.stack_sample(2).continued_work());
  EXPECT_FALSE(profile.stack_sample(3).has_continued_work());
  EXPECT_TRUE(profile.stack_sample(4).continued_work());
}

TEST(CallStackProfileBuilderTest, ProfileStartTime) {
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams);

  base::TestModule module;
  const base::Frame frame = {0x10, &module};
  const base::TimeTicks first_sample_time = base::TimeTicks::UnixEpoch();

  profile_builder->OnSampleCompleted({frame}, first_sample_time);
  profile_builder->OnSampleCompleted({frame},
                                     first_sample_time + base::Seconds(1));
  profile_builder->OnProfileCompleted(base::Seconds(1), base::Seconds(1));

  EXPECT_EQ(first_sample_time, profile_builder->test_profile_start_time());
}

// A basic test of RecordMetadata at the level of the
// CallStackProfileBuilder. The underlying implementation in
// CallStackProfileMetadata is tested independently.
TEST(CallStackProfileBuilderTest, RecordMetadata) {
  base::MetadataRecorder metadata_recorder;
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams, nullptr);

  base::TestModule module;
  base::Frame frame = {0x10, &module};

  metadata_recorder.Set(100, std::nullopt, std::nullopt, 10);
  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, base::TimeTicks());

  profile_builder->OnProfileCompleted(base::Milliseconds(500),
                                      base::Milliseconds(100));

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  ASSERT_EQ(1, profile.metadata_name_hash_size());
  EXPECT_EQ(100u, profile.metadata_name_hash(0));

  ASSERT_EQ(1, profile.stack_sample_size());

  auto sample = profile.stack_sample(0);
  ASSERT_EQ(1, sample.metadata_size());
  EXPECT_EQ(0, sample.metadata(0).name_hash_index());
  EXPECT_FALSE(sample.metadata(0).has_key());
  EXPECT_EQ(10, sample.metadata(0).value());
}

// A basic test of ApplyMetadataRetrospectively at the level of the
// CallStackProfileBuilder. The underlying implementation in
// CallStackProfileMetadata is tested independently.
TEST(CallStackProfileBuilderTest, ApplyMetadataRetrospectively_Basic) {
  base::MetadataRecorder metadata_recorder;
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams, nullptr);

  base::TestModule module;
  base::Frame frame = {0x10, &module};
  base::TimeTicks profile_start_time = base::TimeTicks::UnixEpoch();
  base::TimeDelta sample_time_delta = base::Seconds(1);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, profile_start_time);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame},
                                     profile_start_time + sample_time_delta);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(
      {frame}, profile_start_time + 2 * sample_time_delta);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(
      {frame}, profile_start_time + 3 * sample_time_delta);

  // Apply the metadata from the second through third samples.
  profile_builder->ApplyMetadataRetrospectively(
      profile_start_time + sample_time_delta,
      profile_start_time + sample_time_delta * 2,
      base::MetadataRecorder::Item(3, 30, std::nullopt, 300));

  profile_builder->OnProfileCompleted(3 * sample_time_delta, sample_time_delta);

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  ASSERT_EQ(1, profile.metadata_name_hash_size());
  EXPECT_EQ(3u, profile.metadata_name_hash(0));

  EXPECT_EQ(4, profile.stack_sample_size());

  EXPECT_EQ(0, profile.stack_sample(0).metadata_size());

  ASSERT_EQ(1, profile.stack_sample(1).metadata_size());
  EXPECT_EQ(0, profile.stack_sample(1).metadata(0).name_hash_index());
  EXPECT_EQ(30, profile.stack_sample(1).metadata(0).key());
  EXPECT_EQ(300, profile.stack_sample(1).metadata(0).value());

  EXPECT_EQ(0, profile.stack_sample(2).metadata_size());

  ASSERT_EQ(1, profile.stack_sample(3).metadata_size());
  EXPECT_EQ(0, profile.stack_sample(3).metadata(0).name_hash_index());
  EXPECT_EQ(30, profile.stack_sample(3).metadata(0).key());
  EXPECT_FALSE(profile.stack_sample(3).metadata(0).has_value());
}

// Checks that ApplyMetadataRetrospectively doesn't apply metadata if the
// requested start time is before the profile start time.
TEST(CallStackProfileBuilderTest,
     ApplyMetadataRetrospectively_BeforeStartTime) {
  base::MetadataRecorder metadata_recorder;
  auto profile_builder =
      std::make_unique<TestingCallStackProfileBuilder>(kProfileParams, nullptr);

  base::TestModule module;
  base::Frame frame = {0x10, &module};
  base::TimeTicks profile_start_time = base::TimeTicks::UnixEpoch();
  base::TimeDelta sample_time_delta = base::Seconds(1);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame}, profile_start_time);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted({frame},
                                     profile_start_time + sample_time_delta);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(
      {frame}, profile_start_time + 2 * sample_time_delta);

  profile_builder->RecordMetadata(base::MetadataRecorder::MetadataProvider(
      &metadata_recorder, base::PlatformThread::CurrentId()));
  profile_builder->OnSampleCompleted(
      {frame}, profile_start_time + 3 * sample_time_delta);

  profile_builder->ApplyMetadataRetrospectively(
      profile_start_time - base::Microseconds(1),
      profile_start_time + sample_time_delta,
      base::MetadataRecorder::Item(3, 30, std::nullopt, 300));

  profile_builder->OnProfileCompleted(3 * sample_time_delta, sample_time_delta);

  const SampledProfile& proto = profile_builder->test_sampled_profile();

  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& profile = proto.call_stack_profile();

  EXPECT_EQ(0, profile.metadata_name_hash_size());
  EXPECT_EQ(4, profile.stack_sample_size());

  for (const CallStackProfile::StackSample& sample : profile.stack_sample())
    EXPECT_EQ(0, sample.metadata_size());
}

}  // namespace metrics