OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/services/test_service/test_request_tracker_impl.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 | |
12 namespace mojo { | |
13 namespace test { | |
14 | |
15 TrackingContext::TrackingContext() : next_id(1) { | |
16 } | |
17 | |
18 TrackingContext::~TrackingContext() { | |
19 } | |
20 | |
21 TestRequestTrackerImpl::TestRequestTrackerImpl( | |
22 InterfaceRequest<TestRequestTracker> request, | |
23 TrackingContext* context) | |
24 : context_(context), | |
25 binding_(this, std::move(request)), | |
26 weak_factory_(this) {} | |
27 | |
28 TestRequestTrackerImpl::~TestRequestTrackerImpl() { | |
29 } | |
30 | |
31 void TestRequestTrackerImpl::RecordStats( | |
32 uint64_t client_id, | |
33 ServiceStatsPtr stats) { | |
34 assert(context_->ids_to_names.find(client_id) != | |
35 context_->ids_to_names.end()); | |
36 context_->records[client_id].push_back(*stats); | |
37 } | |
38 | |
39 void TestRequestTrackerImpl::SetNameAndReturnId( | |
40 const String& service_name, | |
41 const Callback<void(uint64_t id)>& callback) { | |
42 uint64_t id = context_->next_id++; | |
43 callback.Run(id); | |
44 DCHECK(context_->ids_to_names.find(id) == context_->ids_to_names.end()); | |
45 context_->ids_to_names[id] = service_name; | |
46 } | |
47 | |
48 TestTrackedRequestServiceImpl::TestTrackedRequestServiceImpl( | |
49 InterfaceRequest<TestTrackedRequestService> request, | |
50 TrackingContext* context) | |
51 : context_(context), binding_(this, std::move(request)) {} | |
52 | |
53 TestTrackedRequestServiceImpl::~TestTrackedRequestServiceImpl() { | |
54 } | |
55 | |
56 void TestTrackedRequestServiceImpl::GetReport( | |
57 const mojo::Callback<void(mojo::Array<ServiceReportPtr>)>& callback) { | |
58 mojo::Array<ServiceReportPtr> reports; | |
59 for (AllRecordsMap::const_iterator it1 = context_->records.begin(); | |
60 it1 != context_->records.end(); ++it1) { | |
61 ServiceReportPtr report(ServiceReport::New()); | |
62 report->service_name = context_->ids_to_names[it1->first]; | |
63 double mean_health_numerator = 0; | |
64 size_t num_samples = it1->second.size(); | |
65 if (num_samples == 0) | |
66 continue; | |
67 | |
68 for (std::vector<ServiceStats>::const_iterator it2 = it1->second.begin(); | |
69 it2 != it1->second.end(); ++it2) { | |
70 report->total_requests += it2->num_new_requests; | |
71 mean_health_numerator += it2->health; | |
72 } | |
73 report->mean_health = mean_health_numerator / num_samples; | |
74 reports.push_back(std::move(report)); | |
75 } | |
76 callback.Run(std::move(reports)); | |
77 } | |
78 | |
79 } // namespace test | |
80 } // namespace mojo | |
OLD | NEW |