Repository: opencv/opencv
File: modules/imgcodecs/perf/perf_decode_encode.cpp (C++)

Code before:
PERF_TEST_P(Encode, multi, testing::ValuesIn(exts_multi))
{
    String filename = getDataPath("perf/1920x1080.png");
    vector<Mat> vec;
    EXPECT_TRUE(imreadmulti(filename, vec));
    vec.push_back(vec.back().clone());
    circle(vec.back(), Point(100, 100), 45, Scalar(0, 0, 255, 0), 2, LINE_AA);
    vector<uchar> buf;
    EXPECT_TRUE(imwrite("test" + GetParam(), vec));

    TEST_CYCLE() imencode(GetParam(), vec, buf);

    std::cout << "  Encoded buffer size: " << buf.size()
        << " bytes, Compression ratio: " << std::fixed << std::setprecision(2)
        << (static_cast<double>(buf.size()) / (vec[0].total() * vec[0].channels())) * 100.0 << "%" << std::endl;

    SANITY_CHECK_NOTHING();
}

PERF_TEST_P(Encode, animation, testing::ValuesIn(exts_multi))
{
    const string filename = cv::tempfile(GetParam().c_str());
    Animation animation = makeCirclesAnimation();

    int success;
    TEST_CYCLE() success = imwriteanimation(filename, animation) ? 0 : -1;

    EXPECT_EQ(success, remove(filename.c_str()));
    SANITY_CHECK_NOTHING();
}

PERF_TEST_P(Encode, multi_page, testing::ValuesIn(exts_multi))
{
    const string filename = cv::tempfile(GetParam().c_str());
    Animation animation = makeCirclesAnimation();

    int success;
    TEST_CYCLE() success = imwritemulti(filename, animation.frames) ? 0 : -1;

    EXPECT_EQ(success, remove(filename.c_str()));
    SANITY_CHECK_NOTHING();
}

PERF_TEST_P(Decode, animation, testing::ValuesIn(exts_multi))
{
    const string filename = cv::tempfile(GetParam().c_str());
    Animation animation = makeCirclesAnimation();

    int success = imwriteanimation(filename, animation) ? 0 : -1;

    TEST_CYCLE() imreadanimation(filename, animation);

Review comment (suggestion):
As soon as it's perf test, I propose to exclude file i/o from scope and use in-memory encoding with imencodeanimation

Code after:
}

PERF_TEST_P(Encode, bgr, testing::ValuesIn(exts))
{
    String filename = getDataPath("perf/1920x1080.png");

    Mat src = imread(filename);
    EXPECT_FALSE(src.empty()) << "Cannot open test image perf/1920x1080.png";
    vector<uchar> buf;

    TEST_CYCLE() imencode(GetParam(), src, buf);

    std::cout << "  Encoded buffer size: " << buf.size()
        << " bytes, Compression ratio: " << std::fixed << std::setprecision(2)
        << (static_cast<double>(buf.size()) / (src.total() * src.channels())) * 100.0 << "%" << std::endl;

    SANITY_CHECK_NOTHING();
}

PERF_TEST_P(Encode, multi, testing::ValuesIn(exts_multi))
{
    String filename = getDataPath("perf/1920x1080.png");
    vector<Mat> vec;
    EXPECT_TRUE(imreadmulti(filename, vec));
    vec.push_back(vec.back().clone());
    circle(vec.back(), Point(100, 100), 45, Scalar(0, 0, 255, 0), 2, LINE_AA);
    vector<uchar> buf;
    EXPECT_TRUE(imwrite("test" + GetParam(), vec));

    TEST_CYCLE() imencode(GetParam(), vec, buf);

    std::cout << "  Encoded buffer size: " << buf.size()
        << " bytes, Compression ratio: " << std::fixed << std::setprecision(2)
        << (static_cast<double>(buf.size()) / (vec[0].total() * vec[0].channels())) * 100.0 << "%" << std::endl;

    SANITY_CHECK_NOTHING();
}

#endif // HAVE_PNG

PERF_TEST_P(Encode, animation, testing::ValuesIn(exts_anim))
{
    Animation animation = makeCirclesAnimation();

    TEST_CYCLE()
    {
        vector<uchar> buf;
        EXPECT_TRUE(imencodeanimation(GetParam().c_str(), animation, buf));
    }

    SANITY_CHECK_NOTHING();