File: arc_system_stat_collector_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 2,951 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/tracing/arc_system_stat_collector.h"

#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/json/json_reader.h"
#include "base/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace arc {

using ArcSystemStatCollectorTest = testing::Test;

namespace {

base::ScopedFD OpenTestStatFile(const std::string& name) {
  base::FilePath base_path;
  base::PathService::Get(chrome::DIR_TEST_DATA, &base_path);
  const base::FilePath path =
      base_path.Append("arc_overview_tracing").Append(name);
  base::ScopedFD result(open(path.value().c_str(), O_RDONLY));
  DCHECK_GE(result.get(), 0);
  return result;
}

}  // namespace

TEST_F(ArcSystemStatCollectorTest, Parse) {
  int64_t zram_values[3];
  EXPECT_TRUE(ParseStatFile(OpenTestStatFile("zram_stat").get(),
                            ArcSystemStatCollector::kZramStatColumns,
                            zram_values));
  EXPECT_EQ(2384, zram_values[0]);
  EXPECT_EQ(56696, zram_values[1]);
  EXPECT_EQ(79, zram_values[2]);

  int64_t mem_values[2];
  EXPECT_TRUE(ParseStatFile(OpenTestStatFile("proc_meminfo").get(),
                            ArcSystemStatCollector::kMemInfoColumns,
                            mem_values));
  EXPECT_EQ(8058940, mem_values[0]);
  EXPECT_EQ(2714260, mem_values[1]);

  int64_t gem_values[2];
  EXPECT_TRUE(ParseStatFile(OpenTestStatFile("gem_objects").get(),
                            ArcSystemStatCollector::kGemInfoColumns,
                            gem_values));
  EXPECT_EQ(853, gem_values[0]);
  EXPECT_EQ(458256384, gem_values[1]);

  int64_t cpu_temp;
  EXPECT_TRUE(ParseStatFile(OpenTestStatFile("temp1_input").get(),
                            ArcSystemStatCollector::kOneValueColumns,
                            &cpu_temp));
  EXPECT_EQ(52000, cpu_temp);
}

TEST_F(ArcSystemStatCollectorTest, Serialize) {
  base::FilePath base_path;
  base::PathService::Get(chrome::DIR_TEST_DATA, &base_path);
  const base::FilePath path =
      base_path.Append("arc_overview_tracing").Append("system_stat_collector");
  std::string json_content;
  ASSERT_TRUE(base::ReadFileToString(path, &json_content));
  ArcSystemStatCollector collector;
  ASSERT_TRUE(collector.LoadFromJson(json_content));
  const std::string json_content_restored = collector.SerializeToJson();
  ASSERT_TRUE(!json_content_restored.empty());
  std::optional<base::Value> root = base::JSONReader::Read(json_content);
  ASSERT_TRUE(root);
  std::optional<base::Value> root_restored =
      base::JSONReader::Read(json_content_restored);
  ASSERT_TRUE(root_restored);
  EXPECT_EQ(*root, *root_restored);
}

}  // namespace arc