blob: 19e481dbd1a5cc6ffec796fdd4686bb5c94dd346 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2018 The Abseil Authors.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// https://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#include <cstddef>
15#include <string>
16
17#include "absl/time/internal/test_util.h"
18#include "absl/time/time.h"
19#include "benchmark/benchmark.h"
20
21namespace {
22
23namespace {
24const char* const kFormats[] = {
25 absl::RFC1123_full, // 0
26 absl::RFC1123_no_wday, // 1
27 absl::RFC3339_full, // 2
28 absl::RFC3339_sec, // 3
Austin Schuhb4691e92020-12-31 12:37:18 -080029 "%Y-%m-%d%ET%H:%M:%S", // 4
Austin Schuh36244a12019-09-21 17:52:38 -070030 "%Y-%m-%d", // 5
31};
32const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
33} // namespace
34
35void BM_Format_FormatTime(benchmark::State& state) {
36 const std::string fmt = kFormats[state.range(0)];
37 state.SetLabel(fmt);
38 const absl::TimeZone lax =
39 absl::time_internal::LoadTimeZone("America/Los_Angeles");
40 const absl::Time t =
41 absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
42 absl::Nanoseconds(1);
43 while (state.KeepRunning()) {
44 benchmark::DoNotOptimize(absl::FormatTime(fmt, t, lax).length());
45 }
46}
47BENCHMARK(BM_Format_FormatTime)->DenseRange(0, kNumFormats - 1);
48
49void BM_Format_ParseTime(benchmark::State& state) {
50 const std::string fmt = kFormats[state.range(0)];
51 state.SetLabel(fmt);
52 const absl::TimeZone lax =
53 absl::time_internal::LoadTimeZone("America/Los_Angeles");
54 absl::Time t = absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
55 absl::Nanoseconds(1);
56 const std::string when = absl::FormatTime(fmt, t, lax);
57 std::string err;
58 while (state.KeepRunning()) {
59 benchmark::DoNotOptimize(absl::ParseTime(fmt, when, lax, &t, &err));
60 }
61}
62BENCHMARK(BM_Format_ParseTime)->DenseRange(0, kNumFormats - 1);
63
64} // namespace