• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMIUMOS_WIDE_PROFILING_TEST_UTILS_H_
6 #define CHROMIUMOS_WIDE_PROFILING_TEST_UTILS_H_
7 
8 #include <stdint.h>
9 
10 #include <map>
11 #include <string>
12 #include <vector>
13 
14 #include "compat/string.h"
15 #include "compat/test.h"
16 #include "file_utils.h"
17 #include "perf_parser.h"
18 
19 namespace quipper {
20 
21 extern const char* kSupportedMetadata[];
22 
23 // Container for all the metadata from one perf report.  The key is the metadata
24 // type, as shown in |kSupportedMetadata|.  The value is a vector of all the
25 // occurrences of that type.  For some types, there is only one occurrence.
26 typedef std::map<string, std::vector<string> > MetadataSet;
27 
28 // Path to the perf executable.
29 string GetPerfPath();
30 
31 // Converts a perf data filename to the full path.
32 string GetTestInputFilePath(const string& filename);
33 
34 // Returns the size of a file in bytes.
35 int64_t GetFileSize(const string& filename);
36 
37 // Returns true if the contents of the two files are the same, false otherwise.
38 bool CompareFileContents(const string& filename1, const string& filename2);
39 
40 template <typename T>
CompareTextProtoFiles(const string & filename1,const string & filename2)41 void CompareTextProtoFiles(const string& filename1, const string& filename2) {
42   std::vector<char> file1_contents;
43   std::vector<char> file2_contents;
44   ASSERT_TRUE(FileToBuffer(filename1, &file1_contents));
45   ASSERT_TRUE(FileToBuffer(filename2, &file2_contents));
46 
47   ArrayInputStream arr1(file1_contents.data(), file1_contents.size());
48   ArrayInputStream arr2(file2_contents.data(), file2_contents.size());
49 
50   T proto1, proto2;
51   ASSERT_TRUE(TextFormat::Parse(&arr1, &proto1));
52   ASSERT_TRUE(TextFormat::Parse(&arr2, &proto2));
53 
54   EXPECT_TRUE(EqualsProto(proto1, proto2));
55 }
56 
57 // Given a perf data file, get the list of build ids and create a map from
58 // filenames to build ids.
59 bool GetPerfBuildIDMap(const string& filename,
60                        std::map<string, string>* output);
61 
62 bool CheckPerfDataAgainstBaseline(const string& filename);
63 
64 // Returns true if the perf buildid-lists are the same.
65 bool ComparePerfBuildIDLists(const string& file1, const string& file2);
66 
67 // Returns options suitable for correctness tests.
68 PerfParserOptions GetTestOptions();
69 
70 template <typename T>
EqualsProto(T actual,T expected)71 bool EqualsProto(T actual, T expected) {
72   MessageDifferencer differencer;
73   differencer.set_message_field_comparison(MessageDifferencer::EQUAL);
74   return differencer.Compare(expected, actual);
75 }
76 
77 template <typename T>
PartiallyEqualsProto(T actual,T expected)78 bool PartiallyEqualsProto(T actual, T expected) {
79   MessageDifferencer differencer;
80   differencer.set_message_field_comparison(MessageDifferencer::EQUAL);
81   differencer.set_scope(MessageDifferencer::PARTIAL);
82   return differencer.Compare(expected, actual);
83 }
84 
85 }  // namespace quipper
86 
87 #endif  // CHROMIUMOS_WIDE_PROFILING_TEST_UTILS_H_
88