int main() { // Generate some random strings. RandomStringGenerator generator; std::vector<std::string> randomStrings; for (size_t i = 0; i < kNumStrings; ++i) randomStrings.push_back(generator.GenerateRandomString(kStringSize)); // Initialize MongoDB. mongo::client::initialize(); mongo::DBClientConnection connection(true); std::string errMsg; if (!connection.connect(kMongoHost, errMsg)) { std::cerr << "Error while connecting to mongodb: " << errMsg << std::endl; return 1; } // Warm-up. Action(randomStrings, connection); usleep(200000); // Run the benchmark. std::cout << "duration," << std::endl; for (size_t i = 0; i < kNumTests; ++i) { // Clean the database. mongo::BSONObjBuilder b; b.append("dropDatabase", 1); mongo::BSONObj r; if (!connection.runCommand("tracekit", b.obj(), r)) { std::cerr << "Failed to clean database." << std::endl; std::cerr << r.toString() << std::endl; return 1; } // Wait a little bit. usleep(200000); // Insert data in the database. auto start = GetMonotonicTime(); Action(randomStrings, connection); auto end = GetMonotonicTime(); std::cout << (end - start) << "," << std::endl; } return 0; }
bool MsgLogger::Log(const char* msg, int level) { if (level > m_maxLevel) return true; size_t msgLen = 0; string oldMsg; if (m_hasMsgModifier) { oldMsg = msg; string newMsg; for(list<MsgModifier*>::iterator it = m_msgModifiers.begin(); it != m_msgModifiers.end(); ++it) { MsgModifier* msgModifier = *it; if (!msgModifier->ModifyMsg(oldMsg, newMsg)) return false; oldMsg.swap(newMsg); newMsg.clear(); } msg = oldMsg.c_str(); msgLen = oldMsg.size(); } else { msgLen = strlen(msg); } WaitForSingleObject(m_mutex, INFINITE); _timeb tbNow; _ftime_s(&tbNow); time_t ttNow = tbNow.time; time_t ttNowAdj = ttNow + TZ_ADJUST; time_t day = ttNowAdj / (24 * 3600); int timeOfDay = ttNowAdj % (24 * 3600); int hour = timeOfDay / 3600; int timeOfHour = timeOfDay % 3600; int min = timeOfHour / 60; int sec = timeOfHour % 60; bool dayChanged = (day != m_ttLogDay); if (dayChanged) { m_ttLogDay = day; tm tmNow; localtime_s(&tmNow, &ttNow); char strLogDay[256]; strftime(strLogDay, 256, "%Y-%m-%d", &tmNow); m_strLogDay = strLogDay; } int tid = GetCurrentThreadId(); char title[256]; size_t titleLen = _snprintf_s(title, 256, _TRUNCATE, "%s %02d:%02d:%02d.%03d%s%s%s%d%s", m_strLogDay.c_str(), hour, min, sec, (int)tbNow.millitm, m_sep.c_str(), m_levels[level], m_sep.c_str(), tid, m_sep.c_str()); if (m_printToScreen) { if (level >= LOG_INFO) printf("%s%s\n", title, msg); else fprintf(stderr, "%s%s\n", title, msg); } if (m_failed) { if (GetMonotonicTime() >= m_lastFailTime + 1.0) { m_failed = false; m_lastFailTime = 0.0; } } bool succ; if (m_failed) { succ = false; } else { succ = true; if (m_logFile == NULL) { succ = DoDivide(); } else if (m_fileLen > m_maxFileLen || dayChanged) { Close(); succ = DoDivide(); } if (succ) { size_t logLineLen = titleLen + msgLen + 2; succ = ((int)logLineLen == fprintf(m_logFile, "%s%s\r\n", title, msg)); m_fileLen += logLineLen; } if (succ) succ = (0 == fflush(m_logFile)); if (!succ) { Close(); m_failed = true; m_lastFailTime = GetMonotonicTime(); } } ReleaseMutex(m_mutex); return succ; }
bool MsgLogger::Log(const char* msg, int level) { if (level > m_maxLevel) return true; size_t msgLen = 0; string oldMsg; if (m_hasMsgModifier) { oldMsg = msg; string newMsg; for(list<MsgModifier*>::iterator it = m_msgModifiers.begin(); it != m_msgModifiers.end(); ++it) { MsgModifier* msgModifier = *it; if (!msgModifier->ModifyMsg(oldMsg, newMsg)) return false; oldMsg.swap(newMsg); newMsg.clear(); } msg = oldMsg.c_str(); msgLen = oldMsg.size(); } else { msgLen = strlen(msg); } pthread_mutex_lock(&m_mutex); timeval tvNow; gettimeofday(&tvNow, NULL); time_t ttNow = tvNow.tv_sec; time_t ttNowAdj = ttNow + TZ_ADJUST; time_t day = ttNowAdj / (24 * 3600); int timeOfDay = ttNowAdj % (24 * 3600); int hour = timeOfDay / 3600; int timeOfHour = timeOfDay % 3600; int min = timeOfHour / 60; int sec = timeOfHour % 60; bool dayChanged = (day != m_ttLogDay); if (dayChanged) { m_ttLogDay = day; tm tmNow; localtime_r(&ttNow, &tmNow); char strLogDay[256]; strftime(strLogDay, 256, "%Y-%m-%d", &tmNow); m_strLogDay = strLogDay; } int tid = syscall(SYS_gettid); char title[256]; size_t titleLen = snprintf(title, 256, "%s %02d:%02d:%02d.%06d%s%s%s%d%s", m_strLogDay.c_str(), hour, min, sec, (int)tvNow.tv_usec, m_sep.c_str(), m_levels[level], m_sep.c_str(), tid, m_sep.c_str()); if (m_printToScreen) { if (level >= LOG_INFO) printf("%s%s\n", title, msg); else fprintf(stderr, "%s%s\n", title, msg); } if (m_failed) { if (GetMonotonicTime() >= m_lastFailTime + 1.0) { m_failed = false; m_lastFailTime = 0.0; } } bool succ; if (m_failed) { succ = false; } else { succ = true; if (m_logFile == NULL) { succ = DoDivide(); } else if (m_fileLen > m_maxFileLen || dayChanged) { Close(); succ = DoDivide(); } if (succ) { size_t logLineLen = titleLen + msgLen + 1; succ = ((int)logLineLen == fprintf(m_logFile, "%s%s\n", title, msg)); m_fileLen += logLineLen; } if (succ) succ = (0 == fflush(m_logFile)); if (!succ) { Close(); m_failed = true; m_lastFailTime = GetMonotonicTime(); } } pthread_mutex_unlock(&m_mutex); return succ; }