1 #ifndef INCLUDE_UTILS_COMMON_H_
2 #define INCLUDE_UTILS_COMMON_H_
4 #include <glog/logging.h>
5 #include <gflags/gflags.h>
6 #include <google/protobuf/message.h>
17 using google::protobuf::Message;
19 #ifndef GFLAGS_GFLAGS_H_
20 namespace gflags = google;
21 #endif // GFLAGS_GFLAGS_H_
26 void ReadProtoFromTextFile(
const char* filename, Message* proto) ;
27 void WriteProtoToTextFile(
const Message& proto,
const char* filename) ;
28 void ReadProtoFromBinaryFile(
const char* filename, Message* proto) ;
29 void WriteProtoToBinaryFile(
const Message& proto,
const char* filename);
31 std::string IntVecToString(
const vector<int>& vec) ;
32 string StringPrintf(
string fmt, ...) ;
34 inline bool check_exists(
const std::string& name) {
36 return (stat (name.c_str(), &buffer) == 0);
45 inline float rand_real(){
46 return static_cast<float>(rand())/(RAND_MAX+1.0f);
52 void AddMetric(
const string& name,
float value){
54 if(name.find(
"@")!=string::npos)
55 prefix=name.substr(0, name.find(
"@"));
56 if(data_.find(prefix)==data_.end())
61 void AddMetrics(
const Metric& other){
62 for(
auto& entry: other.data_)
63 AddMetric(entry.first, entry.second);
70 for(
auto& entry: data_)
71 entry.second/=counter_;
76 const string ToString()
const{
77 string disp=std::to_string(data_.size())+
" fields, ";
78 for(
const auto& entry: data_){
79 disp+=entry.first+
" : "+std::to_string(entry.second)+
"\t";
83 void ParseString(
const string & perf) {
84 std::stringstream stream(perf);
96 map<string, float> data_;
100 #endif // INCLUDE_UTILS_COMMON_H_