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

Side by Side Diff: sync/engine/traffic_logger.cc

Issue 9732008: [Sync] Store the past 10 traffic records in memory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: For review. Created 8 years, 9 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "sync/engine/traffic_logger.h" 5 #include "sync/engine/traffic_logger.h"
6 6
7 #include <queue>
7 #include <string> 8 #include <string>
8 9
9 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h" 13 #include "base/values.h"
13 #include "sync/protocol/proto_value_conversions.h" 14 #include "sync/protocol/proto_value_conversions.h"
14 #include "sync/protocol/sync.pb.h" 15 #include "sync/protocol/sync.pb.h"
16 #include "sync/sessions/sync_session.h"
15 17
16 namespace browser_sync { 18 namespace browser_sync {
17 19
20 using sessions::SyncSession;
21
22 TrafficRecord::TrafficRecord(const std::string& message,
23 TrafficMessageType message_type,
24 bool truncated) :
25 message(message),
26 message_type(message_type),
27 truncated(truncated) {
28 }
29
30 TrafficRecord::~TrafficRecord() {
31 }
32
33 static const unsigned int kMaxMessages = 10;
34 static const unsigned int kMaxMessageSize = 100 * 1024;
akalin 2012/03/22 20:27:28 100k * 10 = 1MB, which seems like a lot just for l
lipalani1 2012/03/23 00:03:11 Letting the individual message size unbounded and
18 namespace { 35 namespace {
19 template <class T> 36 template <class T>
20 void LogData(const T& data, 37 void LogData(const T& data,
21 DictionaryValue* (*to_dictionary_value)(const T&, bool), 38 DictionaryValue* (*to_dictionary_value)(const T&, bool),
22 const std::string& description) { 39 const std::string& description) {
23 if (::logging::DEBUG_MODE && VLOG_IS_ON(1)) { 40 if (::logging::DEBUG_MODE && VLOG_IS_ON(1)) {
24 scoped_ptr<DictionaryValue> value( 41 scoped_ptr<DictionaryValue> value(
25 (*to_dictionary_value)(data, true /* include_specifics */)); 42 (*to_dictionary_value)(data, true /* include_specifics */));
26 std::string message; 43 std::string message;
27 base::JSONWriter::WriteWithOptions(value.get(), 44 base::JSONWriter::WriteWithOptions(value.get(),
28 base::JSONWriter::OPTIONS_PRETTY_PRINT, 45 base::JSONWriter::OPTIONS_PRETTY_PRINT,
29 &message); 46 &message);
30 DVLOG(1) << "\n" << description << "\n" << message << "\n"; 47 DVLOG(1) << "\n" << description << "\n" << message << "\n";
31 } 48 }
32 } 49 }
50
51 void AddTrafficToQueue(std::queue<TrafficRecord>* traffic_recorder,
akalin 2012/03/22 20:27:28 you'll run into problems later when reading the en
lipalani1 2012/03/23 00:03:11 The next patch would only need to access the front
akalin 2012/03/23 00:58:50 I see. But that's still not ideal. So if you do
lipalani1 2012/03/26 21:25:21 I did not want the memory to be used for displayin
52 const TrafficRecord& record) {
53 while (traffic_recorder->size() >= kMaxMessages) {
54 traffic_recorder->pop();
55 }
56
57 traffic_recorder->push(record);
akalin 2012/03/22 20:27:28 surely this should come before the popping? Other
lipalani1 2012/03/23 00:03:11 i did not mean >= in the previous while block. Cha
58 }
59
60 void StoreProtoInQueue(const ::google::protobuf::MessageLite& msg,
61 TrafficMessageType type,
62 SyncSession* session) {
63 bool truncated = false;
64 std::string message;
65 if (msg.ByteSize() >= kMaxMessageSize) {
66 truncated = true;
67 } else {
68 msg.SerializeToString(&message);
69 }
70
71 TrafficRecord record(message, type, truncated);
72 AddTrafficToQueue(session->context()->traffic_recorder(), record);
73 }
74
33 } // namespace 75 } // namespace
34 76
35 void LogClientToServerMessage(const sync_pb::ClientToServerMessage& msg) { 77 void LogClientToServerMessage(const sync_pb::ClientToServerMessage& msg,
78 SyncSession* session) {
36 LogData(msg, &ClientToServerMessageToValue, 79 LogData(msg, &ClientToServerMessageToValue,
37 "******Client To Server Message******"); 80 "******Client To Server Message******");
38 // TODO(lipalani) : Store the data (minus specifics) 81
39 // in a circular buffer in memory. 82 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE, session);
40 } 83 }
41 84
42 void LogClientToServerResponse( 85 void LogClientToServerResponse(
43 const sync_pb::ClientToServerResponse& response) { 86 const sync_pb::ClientToServerResponse& response,
87 SyncSession* session) {
44 LogData(response, &ClientToServerResponseToValue, 88 LogData(response, &ClientToServerResponseToValue,
45 "******Server Response******"); 89 "******Server Response******");
46 // TODO(lipalani) : Store the data (minus specifics) 90
47 // in a circular buffer in memory. 91 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE, session);
48 } 92 }
49 93
50 } // namespace browser_sync 94 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698