Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
metrics_test.cc
Go to the documentation of this file.
1// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/assert.h"
6
7#include "include/dart_api.h"
9
10#include "vm/dart_api_impl.h"
11#include "vm/dart_api_state.h"
12#include "vm/globals.h"
13#include "vm/json_stream.h"
14#include "vm/metrics.h"
15#include "vm/unit_test.h"
16// #include "vm/heap.h"
17
18namespace dart {
19
20#if !defined(PRODUCT)
21VM_UNIT_TEST_CASE(Metric_Simple) {
23 {
24 Metric metric;
25
26 // Initialize metric.
27 metric.InitInstance(Isolate::Current(), "a.b.c", "foobar",
29 EXPECT_EQ(0, metric.value());
30 metric.increment();
31 EXPECT_EQ(1, metric.value());
32 metric.set_value(44);
33 EXPECT_EQ(44, metric.value());
34 }
36}
37
38class MyMetric : public Metric {
39 protected:
40 int64_t Value() const {
41 // 99 bytes.
42 return 99;
43 }
44
45 public:
46 // Just used for testing.
47 int64_t LeakyValue() const { return Value(); }
48};
49
50VM_UNIT_TEST_CASE(Metric_OnDemand) {
52 {
53 Thread* thread = Thread::Current();
54 TransitionNativeToVM transition(thread);
55 StackZone zone(thread);
56 MyMetric metric;
57
58 metric.InitInstance(Isolate::Current(), "a.b.c", "foobar", Metric::kByte);
59 // value is still the default value.
60 EXPECT_EQ(0, metric.value());
61 // Call LeakyValue to confirm that Value returns constant 99.
62 EXPECT_EQ(99, metric.LeakyValue());
63
64 // Serialize to JSON.
65 JSONStream js;
66 metric.PrintJSON(&js);
67 const char* json = js.ToCString();
68 EXPECT_STREQ(
69 "{\"type\":\"Counter\",\"name\":\"a.b.c\",\"description\":"
70 "\"foobar\",\"unit\":\"byte\","
71 "\"fixedId\":true,\"id\":\"metrics\\/native\\/a.b.c\""
72 ",\"value\":99.0}",
73 json);
74 }
76}
77#endif // !defined(PRODUCT)
78
79ISOLATE_UNIT_TEST_CASE(Metric_EmbedderAPI) {
80 {
81 TransitionVMToNative transition(thread);
82
83 const char* kScript = "void main() {}";
85 kScript, /*resolver=*/nullptr, RESOLVED_USER_TEST_URI);
86 EXPECT_VALID(api_lib);
87 }
88
89 // Ensure we've done new/old GCs to ensure max metrics are initialized.
90 String::New("<land-in-new-space>", Heap::kNew);
91 thread->heap()->CollectGarbage(thread, GCType::kScavenge,
93 thread->heap()->CollectGarbage(thread, GCType::kMarkCompact,
95
96 // Ensure we've something live in new space.
97 String::New("<land-in-new-space2>", Heap::kNew);
98
99 EXPECT(thread->isolate_group()->GetHeapOldUsedMaxMetric()->Value() > 0);
100 EXPECT(thread->isolate_group()->GetHeapOldCapacityMaxMetric()->Value() > 0);
101 EXPECT(thread->isolate_group()->GetHeapNewUsedMaxMetric()->Value() > 0);
102 EXPECT(thread->isolate_group()->GetHeapNewCapacityMaxMetric()->Value() > 0);
103 EXPECT(thread->isolate_group()->GetHeapGlobalUsedMetric()->Value() > 0);
104 EXPECT(thread->isolate_group()->GetHeapGlobalUsedMaxMetric()->Value() > 0);
105
106 {
107 TransitionVMToNative transition(thread);
108
110 EXPECT(Dart_IsolateGroupHeapOldUsedMetric(isolate_group) > 0);
112 EXPECT(Dart_IsolateGroupHeapNewUsedMetric(isolate_group) > 0);
114 }
115}
116
117} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
@ kNew
Definition heap.h:38
static Isolate * Current()
Definition isolate.h:939
void PrintJSON(JSONStream *stream)
Definition metrics.cc:76
void set_value(int64_t value)
Definition metrics.h:96
void increment()
Definition metrics.h:98
void InitInstance(Isolate *isolate, const char *name, const char *description, Unit unit)
Definition metrics.cc:39
int64_t value() const
Definition metrics.h:95
int64_t LeakyValue() const
int64_t Value() const
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition object.cc:23777
static Dart_Handle LoadTestScript(const char *script, Dart_NativeEntryResolver resolver, const char *lib_uri=RESOLVED_USER_TEST_URI, bool finalize=true, bool allow_compile_errors=false)
Definition unit_test.cc:422
static Dart_Isolate CreateTestIsolate(const char *name=nullptr, void *isolate_group_data=nullptr, void *isolate_data=nullptr)
Definition unit_test.cc:139
static Thread * Current()
Definition thread.h:361
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
struct _Dart_IsolateGroup * Dart_IsolateGroup
Definition dart_api.h:89
DART_EXPORT int64_t Dart_IsolateGroupHeapNewUsedMetric(Dart_IsolateGroup group)
DART_EXPORT int64_t Dart_IsolateGroupHeapOldCapacityMetric(Dart_IsolateGroup group)
DART_EXPORT int64_t Dart_IsolateGroupHeapNewCapacityMetric(Dart_IsolateGroup group)
DART_EXPORT int64_t Dart_IsolateGroupHeapOldUsedMetric(Dart_IsolateGroup group)
DART_EXPORT Dart_IsolateGroup Dart_CurrentIsolateGroup()
DART_EXPORT void Dart_ShutdownIsolate()
#define VM_UNIT_TEST_CASE(name)
Definition unit_test.h:33
#define ISOLATE_UNIT_TEST_CASE(name)
Definition unit_test.h:64
#define RESOLVED_USER_TEST_URI
Definition unit_test.h:317
#define EXPECT_VALID(handle)
Definition unit_test.h:650