Ninja
elide_middle_perftest.cc
Go to the documentation of this file.
1// Copyright 2024 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#include <vector>
21
22#include "elide_middle.h"
23#include "metrics.h"
24
25static const char* kTestInputs[] = {
26 "01234567890123456789",
27 "012345\x1B[0;35m67890123456789",
28 "abcd\x1b[1;31mefg\x1b[0mhlkmnopqrstuvwxyz",
29};
30
31int main() {
32 std::vector<int> times;
33
34 int64_t kMaxTimeMillis = 5 * 1000;
35 int64_t base_time = GetTimeMillis();
36
37 const int kRuns = 100;
38 for (int j = 0; j < kRuns; ++j) {
39 int64_t start = GetTimeMillis();
40 if (start >= base_time + kMaxTimeMillis)
41 break;
42
43 const int kNumRepetitions = 2000;
44 for (int count = kNumRepetitions; count > 0; --count) {
45 for (const char* input : kTestInputs) {
46 size_t input_len = ::strlen(input);
47 for (size_t max_width = input_len; max_width > 0; --max_width) {
48 std::string str(input, input_len);
49 ElideMiddleInPlace(str, max_width);
50 }
51 }
52 }
53
54 int delta = (int)(GetTimeMillis() - start);
55 times.push_back(delta);
56 }
57
58 int min = times[0];
59 int max = times[0];
60 float total = 0;
61 for (size_t i = 0; i < times.size(); ++i) {
62 total += times[i];
63 if (times[i] < min)
64 min = times[i];
65 else if (times[i] > max)
66 max = times[i];
67 }
68
69 printf("min %dms max %dms avg %.1fms\n", min, max, total / times.size());
70
71 return 0;
72}
void ElideMiddleInPlace(std::string &str, size_t max_width)
Elide the given string str with '...' in the middle if the length exceeds max_width.
static const char * kTestInputs[]
int main()
int64_t GetTimeMillis()
Get the current time as relative to some epoch.
Definition metrics.cc:111
signed long long int64_t
A 64-bit integer type.
Definition win32port.h:28