OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 | 6 |
7 #include "vm/dart_api_impl.h" | 7 #include "vm/dart_api_impl.h" |
8 #include "vm/dart_api_state.h" | 8 #include "vm/dart_api_state.h" |
9 #include "vm/globals.h" | 9 #include "vm/globals.h" |
10 #include "vm/json_stream.h" | 10 #include "vm/json_stream.h" |
11 #include "vm/metrics.h" | 11 #include "vm/metrics.h" |
12 #include "vm/unit_test.h" | 12 #include "vm/unit_test.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 UNIT_TEST_CASE(Metric_Simple) { | 16 UNIT_TEST_CASE(Metric_Simple) { |
17 Isolate::Flags vm_flags; | 17 Dart_CreateIsolate( |
18 Dart_IsolateFlags api_flags; | 18 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
19 vm_flags.CopyTo(&api_flags); | |
20 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
21 EXPECT_EQ(isolate, Isolate::Current()); | |
22 Metric metric; | 19 Metric metric; |
23 | 20 |
24 // Initialize metric. | 21 // Initialize metric. |
25 metric.Init(Isolate::Current(), "a.b.c", "foobar", Metric::kCounter); | 22 metric.Init(Isolate::Current(), "a.b.c", "foobar", Metric::kCounter); |
26 EXPECT_EQ(0, metric.value()); | 23 EXPECT_EQ(0, metric.value()); |
27 metric.increment(); | 24 metric.increment(); |
28 EXPECT_EQ(1, metric.value()); | 25 EXPECT_EQ(1, metric.value()); |
29 metric.set_value(44); | 26 metric.set_value(44); |
30 EXPECT_EQ(44, metric.value()); | 27 EXPECT_EQ(44, metric.value()); |
| 28 Dart_ShutdownIsolate(); |
31 } | 29 } |
32 | 30 |
33 class MyMetric : public Metric { | 31 class MyMetric : public Metric { |
34 protected: | 32 protected: |
35 int64_t Value() const { | 33 int64_t Value() const { |
36 // 99 bytes. | 34 // 99 bytes. |
37 return 99; | 35 return 99; |
38 } | 36 } |
39 | 37 |
40 public: | 38 public: |
41 // Just used for testing. | 39 // Just used for testing. |
42 int64_t LeakyValue() const { return Value(); } | 40 int64_t LeakyValue() const { return Value(); } |
43 }; | 41 }; |
44 | 42 |
45 UNIT_TEST_CASE(Metric_OnDemand) { | 43 UNIT_TEST_CASE(Metric_OnDemand) { |
46 Isolate::Flags vm_flags; | 44 Dart_CreateIsolate( |
47 Dart_IsolateFlags api_flags; | 45 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
48 vm_flags.CopyTo(&api_flags); | 46 { |
49 Isolate* isolate = Isolate::Init(NULL, api_flags); | 47 Isolate* isolate = Isolate::Current(); |
50 EXPECT_EQ(isolate, Isolate::Current()); | 48 StackZone zone(isolate); |
51 StackZone zone(isolate); | 49 HANDLESCOPE(isolate); |
52 HANDLESCOPE(isolate); | 50 MyMetric metric; |
53 MyMetric metric; | |
54 | 51 |
55 metric.Init(Isolate::Current(), "a.b.c", "foobar", Metric::kByte); | 52 metric.Init(Isolate::Current(), "a.b.c", "foobar", Metric::kByte); |
56 // value is still the default value. | 53 // value is still the default value. |
57 EXPECT_EQ(0, metric.value()); | 54 EXPECT_EQ(0, metric.value()); |
58 // Call LeakyValue to confirm that Value returns constant 99. | 55 // Call LeakyValue to confirm that Value returns constant 99. |
59 EXPECT_EQ(99, metric.LeakyValue()); | 56 EXPECT_EQ(99, metric.LeakyValue()); |
60 | 57 |
61 // Serialize to JSON. | 58 // Serialize to JSON. |
62 JSONStream js; | 59 JSONStream js; |
63 metric.PrintJSON(&js); | 60 metric.PrintJSON(&js); |
64 const char* json = js.ToCString(); | 61 const char* json = js.ToCString(); |
65 EXPECT_STREQ("{\"type\":\"Counter\",\"name\":\"a.b.c\",\"description\":" | 62 EXPECT_STREQ("{\"type\":\"Counter\",\"name\":\"a.b.c\",\"description\":" |
66 "\"foobar\",\"unit\":\"byte\"," | 63 "\"foobar\",\"unit\":\"byte\"," |
67 "\"fixedId\":true,\"id\":\"metrics\\/native\\/a.b.c\"" | 64 "\"fixedId\":true,\"id\":\"metrics\\/native\\/a.b.c\"" |
68 ",\"value\":99.000000}", json); | 65 ",\"value\":99.000000}", json); |
| 66 } |
| 67 Dart_ShutdownIsolate(); |
69 } | 68 } |
70 | 69 |
71 } // namespace dart | 70 } // namespace dart |
OLD | NEW |