| 1 | #include <chrono> |
| 2 | #include <fstream> |
| 3 | #include <iomanip> |
| 4 | #include <iostream> |
| 5 | #include <iterator> |
| 6 | #include <sstream> |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #ifdef __APPLE__ |
| 11 | #include <mach/mach.h> |
| 12 | #elif defined(_WIN32) |
| 13 | #define NOMINMAX |
| 14 | #include <windows.h> |
| 15 | #include <psapi.h> |
| 16 | #elif defined(__linux__) |
| 17 | #include <unistd.h> |
| 18 | #endif |
| 19 | |
| 20 | #include "cppjieba/DictTrie.hpp" |
| 21 | #include "cppjieba/HMMModel.hpp" |
| 22 | #include "cppjieba/MPSegment.hpp" |
| 23 | #include "cppjieba/MixSegment.hpp" |
| 24 | #include "test_paths.h" |
| 25 | |
| 26 | using namespace cppjieba; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | typedef std::chrono::steady_clock BenchmarkClock; |
| 31 | |
| 32 | std::string ReadFile(const std::string& path) { |
| 33 | std::ifstream ifs(path.c_str(), std::ifstream::in | std::ifstream::binary); |
| 34 | if (!ifs.is_open()) { |
| 35 | return std::string(); |
| 36 | } |
| 37 | return std::string((std::istreambuf_iterator<char>(ifs)), |
| 38 | std::istreambuf_iterator<char>()); |
| 39 | } |
| 40 | |
| 41 | std::vector<std::string> LoadDictWords(const std::string& path, size_t limit) { |
| 42 | std::ifstream ifs(path.c_str()); |
| 43 | std::vector<std::string> words; |
| 44 | words.reserve(limit); |
| 45 | std::string word; |
| 46 | while (words.size() < limit && ifs >> word) { |
| 47 | words.push_back(word); |
| 48 | std::string ignore; |
| 49 | std::getline(ifs, ignore); |
| 50 | } |
| 51 | return words; |
| 52 | } |
| 53 | |
| 54 | double GetCurrentRSSInMB() { |
| 55 | #ifdef __APPLE__ |
| 56 | mach_task_basic_info info; |
| 57 | mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; |
| 58 | if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, |
| 59 | reinterpret_cast<task_info_t>(&info), &count) != KERN_SUCCESS) { |
| 60 | return -1.0; |
| 61 | } |
| 62 | return static_cast<double>(info.resident_size) / (1024.0 * 1024.0); |
| 63 | #elif defined(__linux__) |
| 64 | long total_pages = 0; |
| 65 | long rss_pages = 0; |
| 66 | std::ifstream ifs("/proc/self/statm"); |
| 67 | if (!(ifs >> total_pages >> rss_pages)) { |
| 68 | return -1.0; |
| 69 | } |
| 70 | const long page_size = sysconf(_SC_PAGESIZE); |
| 71 | return static_cast<double>(rss_pages) * static_cast<double>(page_size) / (1024.0 * 1024.0); |
| 72 | #elif defined(_WIN32) |
| 73 | PROCESS_MEMORY_COUNTERS counters; |
| 74 | if (!GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters))) { |
| 75 | return -1.0; |
| 76 | } |
| 77 | return static_cast<double>(counters.WorkingSetSize) / (1024.0 * 1024.0); |
| 78 | #else |
| 79 | return -1.0; |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | template <typename Func> |
| 84 | double MeasureMillis(Func func) { |
| 85 | const BenchmarkClock::time_point begin = BenchmarkClock::now(); |
| 86 | func(); |
| 87 | const BenchmarkClock::time_point end = BenchmarkClock::now(); |
| 88 | return std::chrono::duration_cast<std::chrono::duration<double, std::milli> >(end - begin).count(); |
| 89 | } |
| 90 | |
| 91 | void PrintMetric(const std::string& name, double millis, const std::string& details) { |
| 92 | std::cout << std::fixed << std::setprecision(3) |
| 93 | << "BENCH " << name |
| 94 | << " ms=" << millis; |
| 95 | if (!details.empty()) { |
| 96 | std::cout << " " << details; |
| 97 | } |
| 98 | std::cout << std::endl; |
| 99 | } |
| 100 | |
| 101 | std::string FormatRSSDetails(double before_rss_mb, double after_rss_mb) { |
| 102 | if (after_rss_mb < 0.0) { |
| 103 | return std::string(); |
| 104 | } |
| 105 | std::ostringstream oss; |
| 106 | oss << std::fixed << std::setprecision(3) |
| 107 | << "rss_mb=" << after_rss_mb; |
| 108 | if (before_rss_mb >= 0.0) { |
| 109 | oss << " rss_delta_mb=" << (after_rss_mb - before_rss_mb); |
| 110 | } |
| 111 | return oss.str(); |
| 112 | } |
| 113 | |
| 114 | std::string FormatThroughputDetails(size_t iterations, size_t bytes, size_t output_words, double millis) { |
| 115 | const double seconds = millis / 1000.0; |
| 116 | const double mb = static_cast<double>(bytes) / (1024.0 * 1024.0); |
| 117 | const double docs_per_sec = seconds > 0.0 ? static_cast<double>(iterations) / seconds : 0.0; |
| 118 | const double mb_per_sec = seconds > 0.0 ? mb * iterations / seconds : 0.0; |
| 119 | std::ostringstream oss; |
| 120 | oss << std::fixed << std::setprecision(3) |
| 121 | << "iters=" << iterations |
| 122 | << " bytes=" << bytes |
| 123 | << " output_words=" << output_words |
| 124 | << " docs_per_sec=" << docs_per_sec |
| 125 | << " mb_per_sec=" << mb_per_sec; |
| 126 | return oss.str(); |
| 127 | } |
| 128 | |
| 129 | std::string FormatLookupDetails(size_t iterations, size_t queries, size_t hits, double millis) { |
| 130 | const double seconds = millis / 1000.0; |
| 131 | const double qps = seconds > 0.0 ? static_cast<double>(iterations * queries) / seconds : 0.0; |
| 132 | std::ostringstream oss; |
| 133 | oss << std::fixed << std::setprecision(3) |
| 134 | << "iters=" << iterations |
| 135 | << " queries=" << queries |
| 136 | << " hits=" << hits |
| 137 | << " qps=" << qps; |
| 138 | return oss.str(); |
| 139 | } |
| 140 | |
| 141 | } // namespace |
| 142 | |
| 143 | int main(int argc, char** argv) { |
| 144 | (void)argc; |
| 145 | (void)argv; |
| 146 | |
| 147 | const std::string dict_path = DICT_DIR "/jieba.dict.utf8"; |
| 148 | const std::string model_path = DICT_DIR "/hmm_model.utf8"; |
| 149 | const std::string user_dict_path = DICT_DIR "/user.dict.utf8"; |
| 150 | const std::string cut_doc = ReadFile(TEST_DATA_DIR "/synthetic_doc.utf8"); |
| 151 | const std::vector<std::string> queries = LoadDictWords(dict_path, 50000); |
| 152 | |
| 153 | if (cut_doc.empty()) { |
| 154 | std::cerr << "Failed to load benchmark document." << std::endl; |
| 155 | return 1; |
| 156 | } |
| 157 | if (queries.empty()) { |
| 158 | std::cerr << "Failed to load benchmark queries." << std::endl; |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | const size_t mp_cut_iterations = 200; |
| 163 | const size_t mix_cut_iterations = 200; |
| 164 | const size_t find_iterations = 20; |
| 165 | |
| 166 | double rss_before = GetCurrentRSSInMB(); |
| 167 | DictTrie* dict_trie = NULL; |
| 168 | const double dict_load_ms = MeasureMillis([&dict_trie, &dict_path, &user_dict_path]() { |
| 169 | dict_trie = new DictTrie(dict_path, user_dict_path); |
| 170 | }); |
| 171 | double rss_after_dict = GetCurrentRSSInMB(); |
| 172 | PrintMetric("DictTrieLoad", dict_load_ms, FormatRSSDetails(rss_before, rss_after_dict)); |
| 173 | |
| 174 | HMMModel* hmm_model = NULL; |
| 175 | const double hmm_load_ms = MeasureMillis([&hmm_model, &model_path]() { |
| 176 | hmm_model = new HMMModel(model_path); |
| 177 | }); |
| 178 | double rss_after_hmm = GetCurrentRSSInMB(); |
| 179 | PrintMetric("HMMModelLoad", hmm_load_ms, FormatRSSDetails(rss_after_dict, rss_after_hmm)); |
| 180 | |
| 181 | MPSegment mp_segment(dict_trie); |
| 182 | MixSegment mix_segment(dict_trie, hmm_model); |
| 183 | |
| 184 | size_t mp_words = 0; |
| 185 | const double mp_cut_ms = MeasureMillis([&]() { |
| 186 | std::vector<std::string> words; |
| 187 | for (size_t i = 0; i < mp_cut_iterations; ++i) { |
| 188 | words.clear(); |
| 189 | mp_segment.Cut(cut_doc, words); |
| 190 | } |
| 191 | mp_words = words.size(); |
| 192 | }); |
| 193 | PrintMetric("MPCut", mp_cut_ms, |
| 194 | FormatThroughputDetails(mp_cut_iterations, cut_doc.size(), mp_words, mp_cut_ms)); |
| 195 | |
| 196 | size_t mix_words = 0; |
| 197 | const double mix_cut_ms = MeasureMillis([&]() { |
| 198 | std::vector<std::string> words; |
| 199 | for (size_t i = 0; i < mix_cut_iterations; ++i) { |
| 200 | words.clear(); |
| 201 | mix_segment.Cut(cut_doc, words); |
| 202 | } |
| 203 | mix_words = words.size(); |
| 204 | }); |
| 205 | PrintMetric("MixCut", mix_cut_ms, |
| 206 | FormatThroughputDetails(mix_cut_iterations, cut_doc.size(), mix_words, mix_cut_ms)); |
| 207 | |
| 208 | size_t hits = 0; |
| 209 | const double find_ms = MeasureMillis([&]() { |
| 210 | for (size_t i = 0; i < find_iterations; ++i) { |
| 211 | for (size_t j = 0; j < queries.size(); ++j) { |
| 212 | if (dict_trie->Find(queries[j])) { |
| 213 | ++hits; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | PrintMetric("DictFind", find_ms, |
| 219 | FormatLookupDetails(find_iterations, queries.size(), hits, find_ms)); |
| 220 | |
| 221 | delete hmm_model; |
| 222 | delete dict_trie; |
| 223 | return 0; |
| 224 | } |