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