1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "src/server/report/logging/compositor_report.h"
#include "mir/logging/logger.h"
#include "mir/test/doubles/advanceable_clock.h"
#include <gtest/gtest.h>
#include <string>
#include <cstdio>
using namespace std;
namespace mtd = mir::test::doubles;
namespace mrl = mir::report::logging;
namespace ml = mir::logging;
namespace
{
class Recorder : public ml::Logger
{
public:
void log(ml::Severity, string const& message, string const&)
{
last = message;
}
string const& last_message() const
{
return last;
}
bool last_message_contains(char const* substr)
{
return last.find(substr) != string::npos;
}
bool scrape(float& fps, float& frame_time) const
{
return sscanf(last.c_str(), "Display %*s averaged %f FPS, %f ms/frame",
&fps, &frame_time) == 2;
}
private:
string last;
};
struct LoggingCompositorReport : ::testing::Test
{
std::shared_ptr<mtd::AdvanceableClock> const clock =
std::make_shared<mtd::AdvanceableClock>();
std::shared_ptr<Recorder> const recorder =
make_shared<Recorder>();
mrl::CompositorReport report{recorder, clock};
};
} // namespace
TEST_F(LoggingCompositorReport, calculates_accurate_stats)
{
/*
* This test just verifies the important stats; FPS and frame time.
* We don't need to validate all the numbers because maintaining low
* coupling to log formats is more important.
*/
const void* const display_id = nullptr;
int target_fps = 60;
for (int frame = 0; frame < target_fps*3; frame++)
{
report.began_frame(display_id);
clock->advance_by(chrono::microseconds(1000000 / target_fps));
report.rendered_frame(display_id);
report.finished_frame(display_id);
}
float measured_fps, measured_frame_time;
ASSERT_TRUE(recorder->scrape(measured_fps, measured_frame_time))
<< recorder->last_message();
EXPECT_LE(59.9f, measured_fps);
EXPECT_GE(60.1f, measured_fps);
EXPECT_LE(16.5f, measured_frame_time);
EXPECT_GE(16.7f, measured_frame_time);
clock->advance_by(chrono::microseconds(5000000));
target_fps = 100;
for (int frame = 0; frame < target_fps*3; frame++)
{
report.began_frame(display_id);
clock->advance_by(chrono::microseconds(1000000 / target_fps));
report.rendered_frame(display_id);
report.finished_frame(display_id);
}
ASSERT_TRUE(recorder->scrape(measured_fps, measured_frame_time))
<< recorder->last_message();
EXPECT_FLOAT_EQ(100.0f, measured_fps);
EXPECT_FLOAT_EQ(10.0f, measured_frame_time);
}
TEST_F(LoggingCompositorReport, survives_pause_resume)
{
const void* const before = "before";
const void* const after = "after";
report.started();
report.began_frame(before);
clock->advance_by(chrono::microseconds(12345));
report.rendered_frame(before);
report.finished_frame(before);
report.stopped();
clock->advance_by(chrono::microseconds(12345678));
report.started();
report.began_frame(after);
clock->advance_by(chrono::microseconds(12345));
report.rendered_frame(after);
report.finished_frame(after);
clock->advance_by(chrono::microseconds(12345678));
report.began_frame(after);
clock->advance_by(chrono::microseconds(12345));
report.rendered_frame(after);
report.finished_frame(after);
report.stopped();
}
TEST_F(LoggingCompositorReport, reports_bypass_only_when_changed)
{
const void* const id = "My Screen";
report.started();
report.began_frame(id);
report.rendered_frame(id);
report.finished_frame(id);
EXPECT_TRUE(recorder->last_message_contains("bypass OFF"))
<< recorder->last_message();
for (int f = 0; f < 3; ++f)
{
report.began_frame(id);
report.rendered_frame(id);
report.finished_frame(id);
clock->advance_by(chrono::microseconds(12345678));
}
EXPECT_FALSE(recorder->last_message_contains("bypass "))
<< recorder->last_message();
report.began_frame(id);
report.finished_frame(id);
EXPECT_TRUE(recorder->last_message_contains("bypass ON"))
<< recorder->last_message();
report.stopped();
}
TEST_F(LoggingCompositorReport, bypass_has_no_render_time)
{ // Regression test for LP: #1408906
const void* const id = "My Screen";
report.started();
for (int f = 0; f < 3; ++f)
{
report.began_frame(id);
clock->advance_by(chrono::microseconds(1234));
report.finished_frame(id);
clock->advance_by(chrono::microseconds(12345678));
}
EXPECT_TRUE(recorder->last_message_contains("0.000 ms/frame"))
<< recorder->last_message();
report.stopped();
}
|