• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "icing/testing/recorder-test-utils.h"
16 
17 #include <list>
18 #include <unordered_map>
19 #include <unordered_set>
20 
21 #include "perftools/profiles/proto/profile.proto.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "icing/file/filesystem.h"
25 #include "util/gzip/gzipstring.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 namespace {
31 
32 // The implementation for these functions is shamelessly plagiarized from
33 // https://cs.corp.google.com/search/?q=heap/alloc_recorder_test.cc
ReadProfileFromFile(const std::string & filename,perftools::profiles::Profile * profile)34 void ReadProfileFromFile(const std::string& filename,
35                          perftools::profiles::Profile* profile) {
36   Filesystem filesystem;
37   ScopedFd sfd(filesystem.OpenForRead(filename.c_str()));
38   int size = filesystem.GetFileSize(sfd.get());
39   std::string str_profilez;
40   str_profilez.resize(size);
41   ASSERT_TRUE(filesystem.Read(sfd.get(), str_profilez.data(), size));
42 
43   std::string str_profile;
44   ASSERT_TRUE(GunzipString(str_profilez, &str_profile));
45 
46   ASSERT_TRUE(profile->ParseFromString(str_profile));
47 }
48 
49 }  // namespace
50 
SummarizeProfileProto(const std::string & filename)51 ProfileInfo SummarizeProfileProto(const std::string& filename) {
52   perftools::profiles::Profile profile;
53   ReadProfileFromFile(filename, &profile);
54   ProfileInfo profile_info{};
55   for (const auto &sample : profile.sample()) {
56     EXPECT_THAT(sample.value(), testing::SizeIs(6));
57     profile_info.peak_objects += sample.value(0);
58     profile_info.peak_bytes += sample.value(1);
59     profile_info.total_alloc_objects += sample.value(2);
60     profile_info.total_alloc_bytes += sample.value(3);
61     profile_info.inuse_objects += sample.value(4);
62     profile_info.inuse_bytes += sample.value(5);
63   }
64 
65   return profile_info;
66 }
67 
68 }  //  namespace lib
69 }  //  namespace icing
70 
71