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"
8#include "include/dart_tools_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
17namespace dart {
18
19#if !defined(PRODUCT)
20VM_UNIT_TEST_CASE(Metric_Simple) {
21 TestCase::CreateTestIsolate();
22 {
23 Metric metric;
24
25 // Initialize metric.
26 metric.InitInstance(Isolate::Current(), "a.b.c", "foobar",
27 Metric::kCounter);
28 EXPECT_EQ(0, metric.value());
29 metric.increment();
30 EXPECT_EQ(1, metric.value());
31 metric.set_value(44);
32 EXPECT_EQ(44, metric.value());
33 }
34 Dart_ShutdownIsolate();
35}
36
37class MyMetric : public Metric {
38 protected:
39 int64_t Value() const {
40 // 99 bytes.
41 return 99;
42 }
43
44 public:
45 // Just used for testing.
46 int64_t LeakyValue() const { return Value(); }
47};
48
49VM_UNIT_TEST_CASE(Metric_OnDemand) {
50 TestCase::CreateTestIsolate();
51 {
52 Thread* thread = Thread::Current();
53 TransitionNativeToVM transition(thread);
54 StackZone zone(thread);
55 HANDLESCOPE(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 }
75 Dart_ShutdownIsolate();
76}
77#endif // !defined(PRODUCT)
78
79ISOLATE_UNIT_TEST_CASE(Metric_EmbedderAPI) {
80 {
81 TransitionVMToNative transition(Thread::Current());
82
83 const char* kScript = "void main() {}";
84 Dart_Handle api_lib = TestCase::LoadTestScript(
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 Isolate::Current()->heap()->new_space()->Scavenge();
92 Isolate::Current()->heap()->CollectAllGarbage(Heap::kLowMemory);
93
94 // Ensure we've something live in new space.
95 String::New("<land-in-new-space2>", Heap::kNew);
96
97 {
98 TransitionVMToNative transition(Thread::Current());
99
100 Dart_Isolate isolate = Dart_CurrentIsolate();
101#if !defined(PRODUCT)
102 EXPECT(Dart_VMIsolateCountMetric() > 0);
103#endif
104 EXPECT(Dart_IsolateHeapOldUsedMetric(isolate) > 0);
105 EXPECT(Dart_IsolateHeapOldUsedMaxMetric(isolate) > 0);
106 EXPECT(Dart_IsolateHeapOldCapacityMetric(isolate) > 0);
107 EXPECT(Dart_IsolateHeapOldCapacityMaxMetric(isolate) > 0);
108 EXPECT(Dart_IsolateHeapNewUsedMetric(isolate) > 0);
109 EXPECT(Dart_IsolateHeapNewUsedMaxMetric(isolate) > 0);
110 EXPECT(Dart_IsolateHeapNewCapacityMetric(isolate) > 0);
111 EXPECT(Dart_IsolateHeapNewCapacityMaxMetric(isolate) > 0);
112 EXPECT(Dart_IsolateHeapGlobalUsedMetric(isolate) > 0);
113 EXPECT(Dart_IsolateHeapGlobalUsedMaxMetric(isolate) > 0);
114 }
115}
116
117} // namespace dart
118