Debian Codegraph @

src/tools/SpeedBenchmark.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2026 Carbo Kuo and contributors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 // Throughput benchmark for SimpleConverter.
20 //
21 // Usage:
22 // speed_benchmark [--resource-zip <zip>] <config> <corpus_file> [reps]
23 // [-- <search_path>...]
24 //
25 // Examples:
26 // speed_benchmark s2t.json /tmp/corpus.txt 30 -- data/config bazel-bin/data/dictionary
27 // speed_benchmark --resource-zip opencc-resources-ocd2.zip s2t.json /tmp/corpus.txt 30
28 //
29 // Prints:
30 // - init time (ms)
31 // - corpus size
32 // - per-iteration time (ms) and throughput (M chars/s, ns/char)
33
34 #include <chrono>
35 #include <fstream>
36 #include <iostream>
37 #include <memory>
38 #include <string>
39 #include <vector>
40
41 #include "src/ResourceProvider.hpp"
42 #include "src/SimpleConverter.hpp"
43 #include "src/UTF8Util.hpp"
44
45 using Clock = std::chrono::steady_clock;
46
47 static double ElapsedMs(Clock::time_point a, Clock::time_point b) {
48 return std::chrono::duration<double, std::milli>(b - a).count();
49 }
50
51 static std::string ReadFile(const std::string& path) {
52 std::ifstream f(path, std::ios::binary);
53 if (!f) {
54 throw std::runtime_error("cannot open file: " + path);
55 }
56 return std::string(std::istreambuf_iterator<char>(f), {});
57 }
58
59 int main(int argc, char** argv) {
60 std::string resourceZip;
61 std::vector<std::string> extraPaths;
62
63 // Collect non-positional args first
64 int argIdx = 1;
65 while (argIdx < argc && std::string(argv[argIdx]) == "--resource-zip") {
66 if (argIdx + 1 >= argc) {
67 std::cerr << "error: --resource-zip requires an argument\n";
68 return 1;
69 }
70 resourceZip = argv[argIdx + 1];
71 argIdx += 2;
72 }
73
74 if (argIdx + 1 >= argc) {
75 std::cerr << "usage: speed_benchmark [--resource-zip <zip>] <config> <corpus_file>"
76 " [reps] [-- <search_path>...]\n";
77 return 1;
78 }
79
80 const std::string config = argv[argIdx++];
81 const std::string corpusFile = argv[argIdx++];
82 int reps = 20;
83
84 for (int i = argIdx; i < argc; i++) {
85 if (std::string(argv[i]) == "--") {
86 for (int j = i + 1; j < argc; j++) {
87 extraPaths.push_back(argv[j]);
88 }
89 break;
90 }
91 reps = std::stoi(argv[i]);
92 }
93
94 const std::string corpus = ReadFile(corpusFile);
95 const size_t corpusBytes = corpus.size();
96
97 // Count Unicode code points for a fair comparison with JS benchmark.
98 size_t corpusChars = 0;
99 for (const char* p = corpus.data(), *end = p + corpus.size(); p < end;) {
100 const size_t n = opencc::UTF8Util::NextCharLength(p);
101 p += (n > 0 && p + n <= end) ? n : 1;
102 ++corpusChars;
103 }
104
105 // ── init ──────────────────────────────────────────────────────────────────
106 auto t0 = Clock::now();
107 std::unique_ptr<opencc::SimpleConverter> converterPtr;
108 if (!resourceZip.empty()) {
109 auto provider = std::make_shared<opencc::ZipResourceProvider>(resourceZip);
110 converterPtr = std::make_unique<opencc::SimpleConverter>(config, provider);
111 } else {
112 converterPtr = std::make_unique<opencc::SimpleConverter>(config, extraPaths);
113 }
114 auto t1 = Clock::now();
115 const double initMs = ElapsedMs(t0, t1);
116
117 opencc::SimpleConverter& converter = *converterPtr;
118
119 // ── warmup ────────────────────────────────────────────────────────────────
120 volatile size_t sink = 0;
121 sink += converter.Convert(corpus).size();
122
123 // ── timed loop ────────────────────────────────────────────────────────────
124 auto t2 = Clock::now();
125 for (int i = 0; i < reps; i++) {
126 sink += converter.Convert(corpus).size();
127 }
128 auto t3 = Clock::now();
129
130 (void)sink;
131
132 const double totalMs = ElapsedMs(t2, t3);
133 const double perIterMs = totalMs / reps;
134 const double totalChars = static_cast<double>(corpusChars) * reps;
135 const double throughputMCharsPerSec = totalChars / totalMs / 1000.0;
136 const double nsPerChar = totalMs * 1e6 / totalChars;
137
138 std::string zipLabel = resourceZip.empty() ? "(filesystem)" : resourceZip;
139 // Strip directory prefix for brevity
140 const size_t slash = zipLabel.rfind('/');
141 if (slash != std::string::npos) zipLabel = zipLabel.substr(slash + 1);
142
143 std::cout << "C++ SimpleConverter config=" << config
144 << " zip=" << zipLabel << "\n";
145 std::cout << " init: " << initMs << " ms\n";
146 std::cout << " corpus: " << corpusChars << " chars "
147 << corpusBytes << " bytes\n";
148 std::cout << " reps: " << reps << "\n";
149 std::cout << " per iter: " << perIterMs << " ms\n";
150 std::cout << " throughput: " << throughputMCharsPerSec << " M chars/s\n";
151 std::cout << " ns/char: " << nsPerChar << "\n";
152
153 return 0;
154 }