Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: mojo/services/test_service/toy_monitoring_service_impl.cc

Issue 349303006: mojo: add some end-to-end shell tests and a new test sample app (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ServiceProvider rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/bind.h"
6 #include "mojo/services/test_service/toy_monitoring_service_impl.h"
7
8 namespace mojo {
9 namespace test {
10
11 MonitoringContext::MonitoringContext() : next_id(1) {}
12 MonitoringContext::~MonitoringContext() {}
13
14 ToyMonitoringServiceRecorderImpl::ToyMonitoringServiceRecorderImpl(
15 ApplicationConnection* connection,
16 MonitoringContext* context) : context_(context), weak_factory_(this) {
17 }
18
19 ToyMonitoringServiceRecorderImpl::~ToyMonitoringServiceRecorderImpl() {
20 }
21
22 void ToyMonitoringServiceRecorderImpl::RecordStats(
23 uint64_t client_id,
24 ServiceStatsPtr stats) {
25 assert(context_->ids_to_names.find(client_id) !=
viettrungluu 2014/06/30 17:08:24 You can use base/logging.h (CHECK/DCHECK) here.
tim (not reviewing) 2014/06/30 22:20:54 Done.
26 context_->ids_to_names.end());
27 context_->records[client_id].push_back(*stats);
28 }
29
30 void ToyMonitoringServiceRecorderImpl::OnConnectionEstablished() {
31 uint64_t id = context_->next_id++;
32 client()->SetIdAndReturnName(id,
33 base::Bind(&ToyMonitoringServiceRecorderImpl::UploaderNameCallback,
34 weak_factory_.GetWeakPtr(),
35 id));
36 }
37
38 void ToyMonitoringServiceRecorderImpl::UploaderNameCallback(
39 uint64_t id, const mojo::String& name) {
40 assert(context_->ids_to_names.find(id) == context_->ids_to_names.end());
41 context_->ids_to_names[id] = name;
42 }
43
44 ToyMonitoringServiceImpl::ToyMonitoringServiceImpl(
45 ApplicationConnection* connection,
46 MonitoringContext* context) : context_(context) {
47 }
48
49 ToyMonitoringServiceImpl::~ToyMonitoringServiceImpl() {
50 }
51
52 void ToyMonitoringServiceImpl::GetReport(
53 const mojo::Callback<void(mojo::Array<ServiceReportPtr>)>& callback) {
54 mojo::Array<ServiceReportPtr> reports;
55 for (AllRecordsMap::const_iterator it1 = context_->records.begin();
56 it1 != context_->records.end(); ++it1) {
57 ServiceReportPtr report(ServiceReport::New());
58 report->service_name = context_->ids_to_names[it1->first];
59 double mean_health_numerator = 0;
60 int num_samples = it1->second.size();
61 if (num_samples == 0)
62 continue;
63
64 for (std::vector<ServiceStats>::const_iterator it2 = it1->second.begin();
65 it2 != it1->second.end(); ++it2) {
66 report->total_requests += it2->num_new_requests;
67 mean_health_numerator += it2->health;
68 }
69 report->mean_health = mean_health_numerator / num_samples;
70 reports.push_back(report.Pass());
71 }
72 callback.Run(reports.Pass());
73 }
74
75 } // namespace test
76 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698