Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
benchmark
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
benchmark
Commits
69d1a524
Commit
69d1a524
authored
May 23, 2016
by
Eric Fiselier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add checks that <Resume|Pause>Timing functions are not called outside of the benchmark. Fixes #204
parent
f434ce3f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
7 deletions
+71
-7
benchmark_api.h
include/benchmark/benchmark_api.h
+5
-2
benchmark.cc
src/benchmark.cc
+3
-1
check.h
src/check.h
+16
-4
CMakeLists.txt
test/CMakeLists.txt
+3
-0
diagnostics_test.cc
test/diagnostics_test.cc
+44
-0
No files found.
include/benchmark/benchmark_api.h
View file @
69d1a524
...
@@ -242,15 +242,17 @@ public:
...
@@ -242,15 +242,17 @@ public:
// returned false.
// returned false.
bool
KeepRunning
()
{
bool
KeepRunning
()
{
if
(
BENCHMARK_BUILTIN_EXPECT
(
!
started_
,
false
))
{
if
(
BENCHMARK_BUILTIN_EXPECT
(
!
started_
,
false
))
{
ResumeTiming
(
);
assert
(
!
finished_
);
started_
=
true
;
started_
=
true
;
ResumeTiming
();
}
}
bool
const
res
=
total_iterations_
++
<
max_iterations
;
bool
const
res
=
total_iterations_
++
<
max_iterations
;
if
(
BENCHMARK_BUILTIN_EXPECT
(
!
res
,
false
))
{
if
(
BENCHMARK_BUILTIN_EXPECT
(
!
res
,
false
))
{
assert
(
started_
);
assert
(
started_
&&
!
finished_
);
PauseTiming
();
PauseTiming
();
// Total iterations now is one greater than max iterations. Fix this.
// Total iterations now is one greater than max iterations. Fix this.
total_iterations_
=
max_iterations
;
total_iterations_
=
max_iterations
;
finished_
=
true
;
}
}
return
res
;
return
res
;
}
}
...
@@ -371,6 +373,7 @@ public:
...
@@ -371,6 +373,7 @@ public:
private
:
private
:
bool
started_
;
bool
started_
;
bool
finished_
;
size_t
total_iterations_
;
size_t
total_iterations_
;
bool
has_range_x_
;
bool
has_range_x_
;
...
...
src/benchmark.cc
View file @
69d1a524
...
@@ -815,7 +815,7 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b,
...
@@ -815,7 +815,7 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b,
State
::
State
(
size_t
max_iters
,
bool
has_x
,
int
x
,
bool
has_y
,
int
y
,
State
::
State
(
size_t
max_iters
,
bool
has_x
,
int
x
,
bool
has_y
,
int
y
,
int
thread_i
,
int
n_threads
)
int
thread_i
,
int
n_threads
)
:
started_
(
false
),
total_iterations_
(
0
),
:
started_
(
false
),
finished_
(
false
),
total_iterations_
(
0
),
has_range_x_
(
has_x
),
range_x_
(
x
),
has_range_x_
(
has_x
),
range_x_
(
x
),
has_range_y_
(
has_y
),
range_y_
(
y
),
has_range_y_
(
has_y
),
range_y_
(
y
),
bytes_processed_
(
0
),
items_processed_
(
0
),
bytes_processed_
(
0
),
items_processed_
(
0
),
...
@@ -830,11 +830,13 @@ State::State(size_t max_iters, bool has_x, int x, bool has_y, int y,
...
@@ -830,11 +830,13 @@ State::State(size_t max_iters, bool has_x, int x, bool has_y, int y,
void
State
::
PauseTiming
()
{
void
State
::
PauseTiming
()
{
// Add in time accumulated so far
// Add in time accumulated so far
CHECK
(
running_benchmark
);
CHECK
(
running_benchmark
);
CHECK
(
started_
&&
!
finished_
);
timer_manager
->
StopTimer
();
timer_manager
->
StopTimer
();
}
}
void
State
::
ResumeTiming
()
{
void
State
::
ResumeTiming
()
{
CHECK
(
running_benchmark
);
CHECK
(
running_benchmark
);
CHECK
(
started_
&&
!
finished_
);
timer_manager
->
StartTimer
();
timer_manager
->
StartTimer
();
}
}
...
...
src/check.h
View file @
69d1a524
...
@@ -10,6 +10,18 @@
...
@@ -10,6 +10,18 @@
namespace
benchmark
{
namespace
benchmark
{
namespace
internal
{
namespace
internal
{
typedef
void
(
AbortHandlerT
)();
inline
AbortHandlerT
*&
get_abort_handler
()
{
static
AbortHandlerT
*
handler
=
&
std
::
abort
;
return
handler
;
}
BENCHMARK_NORETURN
inline
void
abort_handler
()
{
get_abort_handler
()();
std
::
abort
();
// fallback to enforce noreturn
}
// CheckHandler is the class constructed by failing CHECK macros. CheckHandler
// CheckHandler is the class constructed by failing CHECK macros. CheckHandler
// will log information about the failures and abort when it is destructed.
// will log information about the failures and abort when it is destructed.
class
CheckHandler
{
class
CheckHandler
{
...
@@ -25,13 +37,13 @@ public:
...
@@ -25,13 +37,13 @@ public:
return
log_
;
return
log_
;
}
}
BENCHMARK_NORETURN
~
CheckHandler
()
{
BENCHMARK_NORETURN
~
CheckHandler
()
noexcept
(
false
)
{
log_
<<
std
::
endl
;
log_
<<
std
::
endl
;
std
::
abort
();
abort_handler
();
}
}
CheckHandler
&
operator
=
(
const
CheckHandler
&
)
=
delete
;
CheckHandler
&
operator
=
(
const
CheckHandler
&
)
=
delete
;
CheckHandler
(
const
CheckHandler
&
)
=
delete
;
CheckHandler
(
const
CheckHandler
&
)
=
delete
;
CheckHandler
()
=
delete
;
CheckHandler
()
=
delete
;
private
:
private
:
std
::
ostream
&
log_
;
std
::
ostream
&
log_
;
...
...
test/CMakeLists.txt
View file @
69d1a524
...
@@ -36,6 +36,9 @@ add_test(options_benchmarks options_test --benchmark_min_time=0.01)
...
@@ -36,6 +36,9 @@ add_test(options_benchmarks options_test --benchmark_min_time=0.01)
compile_benchmark_test
(
basic_test
)
compile_benchmark_test
(
basic_test
)
add_test
(
basic_benchmark basic_test --benchmark_min_time=0.01
)
add_test
(
basic_benchmark basic_test --benchmark_min_time=0.01
)
compile_benchmark_test
(
diagnostics_test
)
add_test
(
diagnostics_test diagnostics_test --benchmark_min_time=0.01
)
compile_benchmark_test
(
fixture_test
)
compile_benchmark_test
(
fixture_test
)
add_test
(
fixture_test fixture_test --benchmark_min_time=0.01
)
add_test
(
fixture_test fixture_test --benchmark_min_time=0.01
)
...
...
test/diagnostics_test.cc
0 → 100644
View file @
69d1a524
#include "benchmark/benchmark_api.h"
#include "../src/check.h"
void
test_handler
()
{
throw
std
::
logic_error
(
""
);
}
void
try_invalid_pause_resume
(
benchmark
::
State
&
state
)
{
#ifndef NDEBUG
try
{
state
.
PauseTiming
();
std
::
abort
();
}
catch
(
std
::
logic_error
const
&
)
{}
try
{
state
.
ResumeTiming
();
std
::
abort
();
}
catch
(
std
::
logic_error
const
&
)
{}
#else
(
void
)
state
;
// avoid unused warning
#endif
}
void
BM_diagnostic_test
(
benchmark
::
State
&
state
)
{
static
bool
called_once
=
false
;
if
(
called_once
==
false
)
try_invalid_pause_resume
(
state
);
while
(
state
.
KeepRunning
())
{
benchmark
::
DoNotOptimize
(
state
.
iterations
());
}
if
(
called_once
==
false
)
try_invalid_pause_resume
(
state
);
called_once
=
true
;
}
BENCHMARK
(
BM_diagnostic_test
);
int
main
(
int
argc
,
char
**
argv
)
{
benchmark
::
internal
::
get_abort_handler
()
=
&
test_handler
;
benchmark
::
Initialize
(
&
argc
,
argv
);
benchmark
::
RunSpecifiedBenchmarks
();
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment