Optimize formatTime utility (#3777)

Adds benchmarks and unit tests for the function

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Kasia 2022-05-29 13:54:42 +02:00 committed by GitHub
parent 143f4ef2ec
commit 0ad66c0af4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 183 additions and 15 deletions

View file

@ -3,6 +3,7 @@ project(chatterino-benchmark)
set(benchmark_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/Emojis.cpp
${CMAKE_CURRENT_LIST_DIR}/src/FormatTime.cpp
# Add your new file above this line!
)

View file

@ -0,0 +1,38 @@
#include "util/FormatTime.hpp"
#include <benchmark/benchmark.h>
using namespace chatterino;
template <class... Args>
void BM_TimeFormatting(benchmark::State &state, Args &&...args)
{
auto args_tuple = std::make_tuple(std::move(args)...);
for (auto _ : state)
{
formatTime(std::get<0>(args_tuple));
}
}
BENCHMARK_CAPTURE(BM_TimeFormatting, 0, 0);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs0, "0");
BENCHMARK_CAPTURE(BM_TimeFormatting, 1337, 1337);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs1337, "1337");
BENCHMARK_CAPTURE(BM_TimeFormatting, 623452, 623452);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs623452, "623452");
BENCHMARK_CAPTURE(BM_TimeFormatting, 8345, 8345);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs8345, "8345");
BENCHMARK_CAPTURE(BM_TimeFormatting, 314034, 314034);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs314034, "314034");
BENCHMARK_CAPTURE(BM_TimeFormatting, 27, 27);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs27, "27");
BENCHMARK_CAPTURE(BM_TimeFormatting, 34589, 34589);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs34589, "34589");
BENCHMARK_CAPTURE(BM_TimeFormatting, 3659, 3659);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs3659, "3659");
BENCHMARK_CAPTURE(BM_TimeFormatting, 1045345, 1045345);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs1045345, "1045345");
BENCHMARK_CAPTURE(BM_TimeFormatting, 86432, 86432);
BENCHMARK_CAPTURE(BM_TimeFormatting, qs86432, "86432");
BENCHMARK_CAPTURE(BM_TimeFormatting, qsempty, "");
BENCHMARK_CAPTURE(BM_TimeFormatting, qsinvalid, "asd");

View file

@ -1,12 +1,19 @@
#include "FormatTime.hpp"
namespace chatterino {
namespace {
void appendDuration(int count, QChar &&order, QString &outString)
void appendDuration(int count, QChar &&suffix, QString &out)
{
outString.append(QString::number(count));
outString.append(order);
if (!out.isEmpty())
{
out.append(' ');
}
out.append(QString::number(count));
out.append(suffix);
}
} // namespace
QString formatTime(int totalSeconds)
@ -25,26 +32,14 @@ QString formatTime(int totalSeconds)
}
if (hours > 0)
{
if (!res.isEmpty())
{
res.append(" ");
}
appendDuration(hours, 'h', res);
}
if (minutes > 0)
{
if (!res.isEmpty())
{
res.append(" ");
}
appendDuration(minutes, 'm', res);
}
if (seconds > 0)
{
if (!res.isEmpty())
{
res.append(" ");
}
appendDuration(seconds, 's', res);
}
return res;

View file

@ -18,6 +18,7 @@ set(test_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/IrcHelpers.cpp
${CMAKE_CURRENT_LIST_DIR}/src/TwitchPubSubClient.cpp
${CMAKE_CURRENT_LIST_DIR}/src/TwitchMessageBuilder.cpp
${CMAKE_CURRENT_LIST_DIR}/src/FormatTime.cpp
# Add your new file above this line!
)

133
tests/src/FormatTime.cpp Normal file
View file

@ -0,0 +1,133 @@
#include "util/FormatTime.hpp"
#include <gtest/gtest.h>
using namespace chatterino;
TEST(FormatTime, Int)
{
struct TestCase {
int input;
QString expectedOutput;
};
std::vector<TestCase> tests{
{
0,
"",
},
{
1337,
"22m 17s",
},
{
623452,
"7d 5h 10m 52s",
},
{
8345,
"2h 19m 5s",
},
{
314034,
"3d 15h 13m 54s",
},
{
27,
"27s",
},
{
34589,
"9h 36m 29s",
},
{
3659,
"1h 59s",
},
{
1045345,
"12d 2h 22m 25s",
},
{
86432,
"1d 32s",
},
};
for (const auto &[input, expected] : tests)
{
const auto actual = formatTime(input);
EXPECT_EQ(actual, expected)
<< qUtf8Printable(actual) << " (" << input
<< ") did not match expected value " << qUtf8Printable(expected);
}
}
TEST(FormatTime, QString)
{
struct TestCase {
QString input;
QString expectedOutput;
};
std::vector<TestCase> tests{
{
"0",
"",
},
{
"1337",
"22m 17s",
},
{
"623452",
"7d 5h 10m 52s",
},
{
"8345",
"2h 19m 5s",
},
{
"314034",
"3d 15h 13m 54s",
},
{
"27",
"27s",
},
{
"34589",
"9h 36m 29s",
},
{
"3659",
"1h 59s",
},
{
"1045345",
"12d 2h 22m 25s",
},
{
"86432",
"1d 32s",
},
{
"",
"n/a",
},
{
"asd",
"n/a",
},
};
for (const auto &[input, expected] : tests)
{
const auto actual = formatTime(input);
EXPECT_EQ(actual, expected)
<< qUtf8Printable(actual) << " (" << qUtf8Printable(input)
<< ") did not match expected value " << qUtf8Printable(expected);
}
}