summaryrefslogtreecommitdiffstats
path: root/base/test/trace_event_analyzer_unittest.cc
blob: 96f804534f817f8640d261f8f2bca4859697bb40 (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/test/trace_event_analyzer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace trace_analyzer {

namespace {

class TraceEventAnalyzerTest : public testing::Test {
 public:
  void ManualSetUp();
  void OnTraceDataCollected(
      scoped_refptr<base::debug::TraceLog::RefCountedString> json_events_str);
  void BeginTracing();
  void EndTracing();

  base::debug::TraceResultBuffer::SimpleOutput output_;
  base::debug::TraceResultBuffer buffer_;
};

void TraceEventAnalyzerTest::ManualSetUp() {
  base::debug::TraceLog::Resurrect();
  base::debug::TraceLog* tracelog = base::debug::TraceLog::GetInstance();
  ASSERT_TRUE(tracelog);
  tracelog->SetOutputCallback(
    base::Bind(&TraceEventAnalyzerTest::OnTraceDataCollected,
               base::Unretained(this)));
  buffer_.SetOutputCallback(output_.GetCallback());
  output_.json_output.clear();
}

void TraceEventAnalyzerTest::OnTraceDataCollected(
    scoped_refptr<base::debug::TraceLog::RefCountedString> json_events_str) {
  buffer_.AddFragment(json_events_str->data);
}

void TraceEventAnalyzerTest::BeginTracing() {
  output_.json_output.clear();
  buffer_.Start();
  base::debug::TraceLog::GetInstance()->SetEnabled(true);
}

void TraceEventAnalyzerTest::EndTracing() {
  base::debug::TraceLog::GetInstance()->SetEnabled(false);
  buffer_.Finish();
}

}  // namespace

TEST_F(TraceEventAnalyzerTest, NoEvents) {
  using namespace trace_analyzer;
  ManualSetUp();

  // Create an empty JSON event string:
  buffer_.Start();
  buffer_.Finish();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());

  // Search for all events and verify that nothing is returned.
  TraceAnalyzer::TraceEventVector found;
  analyzer->FindEvents(Query::Bool(true), &found);
  EXPECT_EQ(0u, found.size());
}

TEST_F(TraceEventAnalyzerTest, TraceEvent) {
  using namespace trace_analyzer;
  ManualSetUp();

  int int_num = 2;
  double double_num = 3.5;
  const char* str = "the string";

  TraceEvent event;
  event.arg_numbers["false"] = 0.0;
  event.arg_numbers["true"] = 1.0;
  event.arg_numbers["int"] = static_cast<double>(int_num);
  event.arg_numbers["double"] = double_num;
  event.arg_strings["string"] = str;

  ASSERT_TRUE(event.HasNumberArg("false"));
  ASSERT_TRUE(event.HasNumberArg("true"));
  ASSERT_TRUE(event.HasNumberArg("int"));
  ASSERT_TRUE(event.HasNumberArg("double"));
  ASSERT_TRUE(event.HasStringArg("string"));
  ASSERT_FALSE(event.HasNumberArg("notfound"));
  ASSERT_FALSE(event.HasStringArg("notfound"));

  EXPECT_FALSE(event.GetKnownArgAsBool("false"));
  EXPECT_TRUE(event.GetKnownArgAsBool("true"));
  EXPECT_EQ(int_num, event.GetKnownArgAsInt("int"));
  EXPECT_EQ(double_num, event.GetKnownArgAsDouble("double"));
  EXPECT_STREQ(str, event.GetKnownArgAsString("string").c_str());
}

TEST_F(TraceEventAnalyzerTest, QueryEventMember) {
  using namespace trace_analyzer;
  ManualSetUp();

  TraceEvent event;
  event.thread.process_id = 3;
  event.thread.thread_id = 4;
  event.timestamp = 1.5;
  event.phase = TRACE_EVENT_PHASE_BEGIN;
  event.category = "category";
  event.name = "name";
  event.id = "1";
  event.arg_numbers["num"] = 7.0;
  event.arg_strings["str"] = "the string";

  // Other event with all different members:
  TraceEvent other;
  other.thread.process_id = 5;
  other.thread.thread_id = 6;
  other.timestamp = 2.5;
  other.phase = TRACE_EVENT_PHASE_END;
  other.category = "category2";
  other.name = "name2";
  other.id = "2";
  other.arg_numbers["num2"] = 8.0;
  other.arg_strings["str2"] = "the string 2";

  event.other_event = &other;
  ASSERT_TRUE(event.has_other_event());
  double duration = event.GetAbsTimeToOtherEvent();

  Query event_pid = (Query(EVENT_PID) == Query::Int(event.thread.process_id));
  Query event_tid = (Query(EVENT_TID) == Query::Int(event.thread.thread_id));
  Query event_time = (Query(EVENT_TIME) == Query::Double(event.timestamp));
  Query event_duration = (Query(EVENT_DURATION) == Query::Double(duration));
  Query event_phase = (Query(EVENT_PHASE) == Query::Phase(event.phase));
  Query event_category =
      (Query(EVENT_CATEGORY) == Query::String(event.category));
  Query event_name = (Query(EVENT_NAME) == Query::String(event.name));
  Query event_id = (Query(EVENT_ID) == Query::String(event.id));
  Query event_has_arg1 = Query(EVENT_HAS_NUMBER_ARG, "num");
  Query event_has_arg2 = Query(EVENT_HAS_STRING_ARG, "str");
  Query event_arg1 =
      (Query(EVENT_ARG, "num") == Query::Double(event.arg_numbers["num"]));
  Query event_arg2 =
      (Query(EVENT_ARG, "str") == Query::String(event.arg_strings["str"]));
  Query event_has_other = Query(EVENT_HAS_OTHER);
  Query other_pid = (Query(OTHER_PID) == Query::Int(other.thread.process_id));
  Query other_tid = (Query(OTHER_TID) == Query::Int(other.thread.thread_id));
  Query other_time = (Query(OTHER_TIME) == Query::Double(other.timestamp));
  Query other_phase = (Query(OTHER_PHASE) == Query::Phase(other.phase));
  Query other_category =
      (Query(OTHER_CATEGORY) == Query::String(other.category));
  Query other_name = (Query(OTHER_NAME) == Query::String(other.name));
  Query other_id = (Query(OTHER_ID) == Query::String(other.id));
  Query other_has_arg1 = Query(OTHER_HAS_NUMBER_ARG, "num2");
  Query other_has_arg2 = Query(OTHER_HAS_STRING_ARG, "str2");
  Query other_arg1 =
      (Query(OTHER_ARG, "num2") == Query::Double(other.arg_numbers["num2"]));
  Query other_arg2 =
      (Query(OTHER_ARG, "str2") == Query::String(other.arg_strings["str2"]));

  EXPECT_TRUE(event_pid.Evaluate(event));
  EXPECT_TRUE(event_tid.Evaluate(event));
  EXPECT_TRUE(event_time.Evaluate(event));
  EXPECT_TRUE(event_duration.Evaluate(event));
  EXPECT_TRUE(event_phase.Evaluate(event));
  EXPECT_TRUE(event_category.Evaluate(event));
  EXPECT_TRUE(event_name.Evaluate(event));
  EXPECT_TRUE(event_id.Evaluate(event));
  EXPECT_TRUE(event_has_arg1.Evaluate(event));
  EXPECT_TRUE(event_has_arg2.Evaluate(event));
  EXPECT_TRUE(event_arg1.Evaluate(event));
  EXPECT_TRUE(event_arg2.Evaluate(event));
  EXPECT_TRUE(event_has_other.Evaluate(event));
  EXPECT_TRUE(other_pid.Evaluate(event));
  EXPECT_TRUE(other_tid.Evaluate(event));
  EXPECT_TRUE(other_time.Evaluate(event));
  EXPECT_TRUE(other_phase.Evaluate(event));
  EXPECT_TRUE(other_category.Evaluate(event));
  EXPECT_TRUE(other_name.Evaluate(event));
  EXPECT_TRUE(other_id.Evaluate(event));
  EXPECT_TRUE(other_has_arg1.Evaluate(event));
  EXPECT_TRUE(other_has_arg2.Evaluate(event));
  EXPECT_TRUE(other_arg1.Evaluate(event));
  EXPECT_TRUE(other_arg2.Evaluate(event));

  // Evaluate event queries against other to verify the queries fail when the
  // event members are wrong.
  EXPECT_FALSE(event_pid.Evaluate(other));
  EXPECT_FALSE(event_tid.Evaluate(other));
  EXPECT_FALSE(event_time.Evaluate(other));
  EXPECT_FALSE(event_duration.Evaluate(other));
  EXPECT_FALSE(event_phase.Evaluate(other));
  EXPECT_FALSE(event_category.Evaluate(other));
  EXPECT_FALSE(event_name.Evaluate(other));
  EXPECT_FALSE(event_id.Evaluate(other));
  EXPECT_FALSE(event_has_arg1.Evaluate(other));
  EXPECT_FALSE(event_has_arg2.Evaluate(other));
  EXPECT_FALSE(event_arg1.Evaluate(other));
  EXPECT_FALSE(event_arg2.Evaluate(other));
  EXPECT_FALSE(event_has_other.Evaluate(other));
}

TEST_F(TraceEventAnalyzerTest, BooleanOperators) {
  using namespace trace_analyzer;
  ManualSetUp();

  BeginTracing();
  {
    TRACE_EVENT_INSTANT1("cat1", "name1", "num", 1);
    TRACE_EVENT_INSTANT1("cat1", "name2", "num", 2);
    TRACE_EVENT_INSTANT1("cat2", "name3", "num", 3);
    TRACE_EVENT_INSTANT1("cat2", "name4", "num", 4);
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(!!analyzer.get());

  TraceAnalyzer::TraceEventVector found;

  // ==

  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1"), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name2", found[1]->name.c_str());

  analyzer->FindEvents(Query(EVENT_ARG, "num") == Query::Int(2), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("name2", found[0]->name.c_str());

  // !=

  analyzer->FindEvents(Query(EVENT_CATEGORY) != Query::String("cat1"), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name3", found[0]->name.c_str());
  EXPECT_STREQ("name4", found[1]->name.c_str());

  analyzer->FindEvents(Query(EVENT_ARG, "num") != Query::Int(2), &found);
  ASSERT_EQ(3u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name3", found[1]->name.c_str());
  EXPECT_STREQ("name4", found[2]->name.c_str());

  // <
  analyzer->FindEvents(Query(EVENT_ARG, "num") < Query::Int(2), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());

  // <=
  analyzer->FindEvents(Query(EVENT_ARG, "num") <= Query::Int(2), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name2", found[1]->name.c_str());

  // >
  analyzer->FindEvents(Query(EVENT_ARG, "num") > Query::Int(3), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("name4", found[0]->name.c_str());

  // >=
  analyzer->FindEvents(Query(EVENT_ARG, "num") >= Query::Int(4), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("name4", found[0]->name.c_str());

  // &&
  analyzer->FindEvents(Query(EVENT_NAME) != Query::String("name1") &&
                       Query(EVENT_ARG, "num") < Query::Int(3), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("name2", found[0]->name.c_str());

  // ||
  analyzer->FindEvents(Query(EVENT_NAME) == Query::String("name1") ||
                       Query(EVENT_ARG, "num") == Query::Int(3), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name3", found[1]->name.c_str());

  // !
  analyzer->FindEvents(!(Query(EVENT_NAME) == Query::String("name1") ||
                         Query(EVENT_ARG, "num") == Query::Int(3)), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name2", found[0]->name.c_str());
  EXPECT_STREQ("name4", found[1]->name.c_str());
}

TEST_F(TraceEventAnalyzerTest, ArithmeticOperators) {
  using namespace trace_analyzer;
  ManualSetUp();

  BeginTracing();
  {
    // These events are searched for:
    TRACE_EVENT_INSTANT2("cat1", "math1", "a", 10, "b", 5);
    TRACE_EVENT_INSTANT2("cat1", "math2", "a", 10, "b", 10);
    // Extra events that never match, for noise:
    TRACE_EVENT_INSTANT2("noise", "math3", "a", 1,  "b", 3);
    TRACE_EVENT_INSTANT2("noise", "math4", "c", 10, "d", 5);
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());

  TraceAnalyzer::TraceEventVector found;

  // Verify that arithmetic operators function:

  // +
  analyzer->FindEvents(Query(EVENT_ARG, "a") + Query(EVENT_ARG, "b") ==
                       Query::Int(20), &found);
  EXPECT_EQ(1u, found.size());
  EXPECT_STREQ("math2", found.front()->name.c_str());

  // -
  analyzer->FindEvents(Query(EVENT_ARG, "a") - Query(EVENT_ARG, "b") ==
                       Query::Int(5), &found);
  EXPECT_EQ(1u, found.size());
  EXPECT_STREQ("math1", found.front()->name.c_str());

  // *
  analyzer->FindEvents(Query(EVENT_ARG, "a") * Query(EVENT_ARG, "b") ==
                       Query::Int(50), &found);
  EXPECT_EQ(1u, found.size());
  EXPECT_STREQ("math1", found.front()->name.c_str());

  // /
  analyzer->FindEvents(Query(EVENT_ARG, "a") / Query(EVENT_ARG, "b") ==
                       Query::Int(2), &found);
  EXPECT_EQ(1u, found.size());
  EXPECT_STREQ("math1", found.front()->name.c_str());

  // %
  analyzer->FindEvents(Query(EVENT_ARG, "a") % Query(EVENT_ARG, "b") ==
                       Query::Int(0), &found);
  EXPECT_EQ(2u, found.size());

  // - (negate)
  analyzer->FindEvents(-Query(EVENT_ARG, "b") == Query::Int(-10), &found);
  EXPECT_EQ(1u, found.size());
  EXPECT_STREQ("math2", found.front()->name.c_str());
}

TEST_F(TraceEventAnalyzerTest, StringPattern) {
  using namespace trace_analyzer;
  ManualSetUp();

  BeginTracing();
  {
    TRACE_EVENT_INSTANT0("cat1", "name1");
    TRACE_EVENT_INSTANT0("cat1", "name2");
    TRACE_EVENT_INSTANT0("cat1", "no match");
    TRACE_EVENT_INSTANT0("cat1", "name3x");
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());

  TraceAnalyzer::TraceEventVector found;

  analyzer->FindEvents(Query(EVENT_NAME) == Query::Pattern("name?"), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name2", found[1]->name.c_str());

  analyzer->FindEvents(Query(EVENT_NAME) == Query::Pattern("name*"), &found);
  ASSERT_EQ(3u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name2", found[1]->name.c_str());
  EXPECT_STREQ("name3x", found[2]->name.c_str());

  analyzer->FindEvents(Query(EVENT_NAME) != Query::Pattern("name*"), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("no match", found[0]->name.c_str());
}

// Test that duration queries work.
TEST_F(TraceEventAnalyzerTest, Duration) {
  using namespace trace_analyzer;
  ManualSetUp();

  int sleep_time_us = 200000;
  // We will search for events that have a duration of greater than 90% of the
  // sleep time, so that there is no flakiness.
  int duration_cutoff_us = (sleep_time_us * 9) / 10;

  BeginTracing();
  {
    TRACE_EVENT0("cat1", "name1"); // found by duration query
    TRACE_EVENT0("noise", "name2"); // not searched for, just noise
    {
      TRACE_EVENT0("cat2", "name3"); // found by duration query
      TRACE_EVENT_INSTANT0("noise", "name4"); // not searched for, just noise
      base::PlatformThread::Sleep(sleep_time_us / 1000);
      TRACE_EVENT0("cat2", "name5"); // not found (duration too short)
    }
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());
  analyzer->AssociateBeginEndEvents();

  TraceAnalyzer::TraceEventVector found;
  analyzer->FindEvents(Query::MatchBeginWithEnd() &&
                      Query(EVENT_DURATION) > Query::Int(duration_cutoff_us) &&
                      (Query(EVENT_CATEGORY) == Query::String("cat1") ||
                       Query(EVENT_CATEGORY) == Query::String("cat2") ||
                       Query(EVENT_CATEGORY) == Query::String("cat3")), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STREQ("name1", found[0]->name.c_str());
  EXPECT_STREQ("name3", found[1]->name.c_str());
}

// Test AssociateBeginEndEvents
TEST_F(TraceEventAnalyzerTest, BeginEndAssocations) {
  using namespace trace_analyzer;
  ManualSetUp();

  BeginTracing();
  {
    TRACE_EVENT_END0("cat1", "name1"); // does not match out of order begin
    TRACE_EVENT0("cat1", "name2");
    TRACE_EVENT_INSTANT0("cat1", "name3");
    TRACE_EVENT_BEGIN0("cat1", "name1");
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());
  analyzer->AssociateBeginEndEvents();

  TraceAnalyzer::TraceEventVector found;
  analyzer->FindEvents(Query::MatchBeginWithEnd(), &found);
  ASSERT_EQ(1u, found.size());
  EXPECT_STREQ("name2", found[0]->name.c_str());
}

// Test AssociateStartFinishEvents
TEST_F(TraceEventAnalyzerTest, StartFinishAssocations) {
  using namespace trace_analyzer;
  ManualSetUp();

  BeginTracing();
  {
    TRACE_EVENT_FINISH0("cat1", "name1", 0xA); // does not match / out of order
    TRACE_EVENT_START0("cat1", "name1", 0xB);
    TRACE_EVENT_START0("cat1", "name1", 0xC);
    TRACE_EVENT_INSTANT0("cat1", "name1"); // noise
    TRACE_EVENT0("cat1", "name1"); // noise
    TRACE_EVENT_FINISH0("cat1", "name1", 0xB);
    TRACE_EVENT_FINISH0("cat1", "name1", 0xC);
    TRACE_EVENT_START0("cat1", "name1", 0xA); // does not match / out of order
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());
  analyzer->AssociateStartFinishEvents();

  TraceAnalyzer::TraceEventVector found;
  analyzer->FindEvents(Query::MatchStartWithFinish(), &found);
  ASSERT_EQ(2u, found.size());
  EXPECT_STRCASEEQ("B", found[0]->id.c_str());
  EXPECT_STRCASEEQ("C", found[1]->id.c_str());
}

// Test that the TraceAnalyzer custom associations work.
TEST_F(TraceEventAnalyzerTest, CustomAssociations) {
  using namespace trace_analyzer;
  ManualSetUp();

  // Add events that begin/end in pipelined ordering with unique ID parameter
  // to match up the begin/end pairs.
  BeginTracing();
  {
    TRACE_EVENT_INSTANT1("cat1", "end", "id", 1);   // no begin match
    TRACE_EVENT_INSTANT1("cat2", "begin", "id", 2); // end is cat4
    TRACE_EVENT_INSTANT1("cat3", "begin", "id", 3); // end is cat5
    TRACE_EVENT_INSTANT1("cat4", "end", "id", 2);
    TRACE_EVENT_INSTANT1("cat5", "end", "id", 3);
    TRACE_EVENT_INSTANT1("cat6", "begin", "id", 1); // no end match
  }
  EndTracing();

  scoped_ptr<TraceAnalyzer>
      analyzer(TraceAnalyzer::Create(output_.json_output));
  ASSERT_TRUE(analyzer.get());

  // begin, end, and match queries to find proper begin/end pairs.
  Query begin(Query(EVENT_NAME) == Query::String("begin"));
  Query end(Query(EVENT_NAME) == Query::String("end"));
  Query match(Query(EVENT_ARG, "id") == Query(OTHER_ARG, "id"));
  analyzer->AssociateEvents(begin, end, match);

  TraceAnalyzer::TraceEventVector found;

  // cat1 has no other_event.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1") &&
                       Query(EVENT_HAS_OTHER), &found);
  EXPECT_EQ(0u, found.size());

  // cat1 has no other_event.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1") &&
                       !Query(EVENT_HAS_OTHER), &found);
  EXPECT_EQ(1u, found.size());

  // cat6 has no other_event.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat6") &&
                       !Query(EVENT_HAS_OTHER), &found);
  EXPECT_EQ(1u, found.size());

  // cat2 and cat4 are a associated.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat2") &&
                       Query(OTHER_CATEGORY) == Query::String("cat4"), &found);
  EXPECT_EQ(1u, found.size());

  // cat4 and cat2 are a associated.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat4") &&
                       Query(OTHER_CATEGORY) == Query::String("cat2"), &found);
  EXPECT_EQ(1u, found.size());

  // cat3 and cat5 are a associated.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat3") &&
                       Query(OTHER_CATEGORY) == Query::String("cat5"), &found);
  EXPECT_EQ(1u, found.size());

  // cat5 and cat3 are a associated.
  analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat5") &&
                       Query(OTHER_CATEGORY) == Query::String("cat3"), &found);
  EXPECT_EQ(1u, found.size());
}

// Verify that Query literals and types are properly casted.
TEST_F(TraceEventAnalyzerTest, Literals) {
  using namespace trace_analyzer;
  ManualSetUp();

  // Since these queries don't refer to the event data, the dummy event below
  // will never be accessed.
  TraceEvent dummy;
  char char_num = 5;
  short short_num = -5;
  EXPECT_TRUE((Query::Double(5.0) == Query::Int(char_num)).Evaluate(dummy));
  EXPECT_TRUE((Query::Double(-5.0) == Query::Int(short_num)).Evaluate(dummy));
  EXPECT_TRUE((Query::Double(1.0) == Query::Uint(1u)).Evaluate(dummy));
  EXPECT_TRUE((Query::Double(1.0) == Query::Int(1)).Evaluate(dummy));
  EXPECT_TRUE((Query::Double(-1.0) == Query::Int(-1)).Evaluate(dummy));
  EXPECT_TRUE((Query::Double(1.0) == Query::Double(1.0f)).Evaluate(dummy));
  EXPECT_TRUE((Query::Bool(true) == Query::Int(1)).Evaluate(dummy));
  EXPECT_TRUE((Query::Bool(false) == Query::Int(0)).Evaluate(dummy));
  EXPECT_TRUE((Query::Bool(true) == Query::Double(1.0f)).Evaluate(dummy));
  EXPECT_TRUE((Query::Bool(false) == Query::Double(0.0f)).Evaluate(dummy));
}

// Test GetRateStats.
TEST_F(TraceEventAnalyzerTest, RateStats) {
  using namespace trace_analyzer;

  std::vector<TraceEvent> events;
  events.reserve(100);
  TraceAnalyzer::TraceEventVector event_ptrs;
  TraceEvent event;
  event.timestamp = 0.0;
  double little_delta = 1.0;
  double big_delta = 10.0;
  TraceAnalyzer::Stats stats;

  for (int i = 0; i < 10; ++i) {
    event.timestamp += little_delta;
    events.push_back(event);
    event_ptrs.push_back(&events.back());
  }

  ASSERT_TRUE(TraceAnalyzer::GetRateStats(event_ptrs, &stats));
  EXPECT_EQ(little_delta, stats.mean_us);
  EXPECT_EQ(little_delta, stats.min_us);
  EXPECT_EQ(little_delta, stats.max_us);
  EXPECT_EQ(0.0, stats.standard_deviation_us);

  event.timestamp += big_delta;
  events.push_back(event);
  event_ptrs.push_back(&events.back());

  ASSERT_TRUE(TraceAnalyzer::GetRateStats(event_ptrs, &stats));
  EXPECT_LT(little_delta, stats.mean_us);
  EXPECT_EQ(little_delta, stats.min_us);
  EXPECT_EQ(big_delta, stats.max_us);
  EXPECT_LT(0.0, stats.standard_deviation_us);

  TraceAnalyzer::TraceEventVector few_event_ptrs;
  few_event_ptrs.push_back(&event);
  few_event_ptrs.push_back(&event);
  ASSERT_FALSE(TraceAnalyzer::GetRateStats(few_event_ptrs, &stats));
  few_event_ptrs.push_back(&event);
  ASSERT_TRUE(TraceAnalyzer::GetRateStats(few_event_ptrs, &stats));
}


}  // namespace trace_analyzer