mirror of
https://github.com/boostorg/beast.git
synced 2025-08-02 14:24:31 +02:00
Add bench-zlib
This commit is contained in:
@@ -6,6 +6,7 @@ Version 96:
|
|||||||
* Rename wsload compile target
|
* Rename wsload compile target
|
||||||
* Fix library.json
|
* Fix library.json
|
||||||
* Update boostorg links
|
* Update boostorg links
|
||||||
|
* Add bench-zlib
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@@ -112,22 +112,22 @@ endif()
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|
||||||
function(DoGroupSources curdir rootdir folder)
|
function (DoGroupSources curdir rootdir folder)
|
||||||
file (GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir} ${PROJECT_SOURCE_DIR}/${curdir}/*)
|
file (GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir} ${PROJECT_SOURCE_DIR}/${curdir}/*)
|
||||||
foreach (child ${children})
|
foreach (child ${children})
|
||||||
if (IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child})
|
if (IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child})
|
||||||
DoGroupSources(${curdir}/${child} ${rootdir} ${folder})
|
DoGroupSources (${curdir}/${child} ${rootdir} ${folder})
|
||||||
elseif (${child} STREQUAL "CMakeLists.txt")
|
elseif (${child} STREQUAL "CMakeLists.txt")
|
||||||
source_group("" FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
|
source_group("" FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
|
||||||
else()
|
else()
|
||||||
string(REGEX REPLACE ^${rootdir} ${folder} groupname ${curdir})
|
string (REGEX REPLACE ^${rootdir} ${folder} groupname ${curdir})
|
||||||
string(REPLACE "/" "\\" groupname ${groupname})
|
string (REPLACE "/" "\\" groupname ${groupname})
|
||||||
source_group(${groupname} FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
|
source_group (${groupname} FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
|
||||||
endif()
|
endif()
|
||||||
endforeach()
|
endforeach()
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
function(GroupSources curdir folder)
|
function (GroupSources curdir folder)
|
||||||
DoGroupSources (${curdir} ${curdir} ${folder})
|
DoGroupSources (${curdir} ${curdir} ${folder})
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
@@ -7,7 +7,8 @@
|
|||||||
# Official repository: https://github.com/boostorg/beast
|
# Official repository: https://github.com/boostorg/beast
|
||||||
#
|
#
|
||||||
|
|
||||||
include_directories (extras/include)
|
include_directories (./extern)
|
||||||
|
include_directories (./extras/include)
|
||||||
|
|
||||||
file (GLOB_RECURSE EXTRAS_INCLUDES
|
file (GLOB_RECURSE EXTRAS_INCLUDES
|
||||||
${PROJECT_SOURCE_DIR}/test/extras/include/boost/beast/*.hpp
|
${PROJECT_SOURCE_DIR}/test/extras/include/boost/beast/*.hpp
|
||||||
@@ -20,6 +21,29 @@ if (OPENSSL_FOUND)
|
|||||||
link_libraries (${OPENSSL_LIBRARIES})
|
link_libraries (${OPENSSL_LIBRARIES})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
set (ZLIB_SOURCES
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/crc32.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/deflate.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inffast.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inffixed.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inflate.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inftrees.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/trees.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/zlib.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/zutil.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/adler32.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/compress.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/crc32.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/deflate.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/infback.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inffast.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inflate.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/inftrees.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/trees.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/uncompr.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/extern/zlib-1.2.11/zutil.c
|
||||||
|
)
|
||||||
|
|
||||||
add_subdirectory (beast)
|
add_subdirectory (beast)
|
||||||
add_subdirectory (bench)
|
add_subdirectory (bench)
|
||||||
add_subdirectory (doc)
|
add_subdirectory (doc)
|
||||||
|
19
test/Jamfile
19
test/Jamfile
@@ -7,6 +7,25 @@
|
|||||||
# Official repository: https://github.com/boostorg/beast
|
# Official repository: https://github.com/boostorg/beast
|
||||||
#
|
#
|
||||||
|
|
||||||
|
project test
|
||||||
|
: requirements
|
||||||
|
<include>./extern
|
||||||
|
;
|
||||||
|
|
||||||
|
path-constant ZLIB_SOURCES :
|
||||||
|
extern/zlib-1.2.11/adler32.c
|
||||||
|
extern/zlib-1.2.11/compress.c
|
||||||
|
extern/zlib-1.2.11/crc32.c
|
||||||
|
extern/zlib-1.2.11/deflate.c
|
||||||
|
extern/zlib-1.2.11/infback.c
|
||||||
|
extern/zlib-1.2.11/inffast.c
|
||||||
|
extern/zlib-1.2.11/inflate.c
|
||||||
|
extern/zlib-1.2.11/inftrees.c
|
||||||
|
extern/zlib-1.2.11/trees.c
|
||||||
|
extern/zlib-1.2.11/uncompr.c
|
||||||
|
extern/zlib-1.2.11/zutil.c
|
||||||
|
;
|
||||||
|
|
||||||
alias run-tests :
|
alias run-tests :
|
||||||
beast//run-tests
|
beast//run-tests
|
||||||
bench//run-tests
|
bench//run-tests
|
||||||
|
@@ -7,33 +7,11 @@
|
|||||||
# Official repository: https://github.com/boostorg/beast
|
# Official repository: https://github.com/boostorg/beast
|
||||||
#
|
#
|
||||||
|
|
||||||
GroupSources(test/extras/include/boost/beast extras)
|
|
||||||
GroupSources(include/boost/beast beast)
|
GroupSources(include/boost/beast beast)
|
||||||
|
GroupSources(test/extras/include/boost/beast extras)
|
||||||
|
GroupSources(test/extern/zlib-1.2.11 zlib)
|
||||||
GroupSources(test/beast/zlib "/")
|
GroupSources(test/beast/zlib "/")
|
||||||
|
|
||||||
set(ZLIB_SOURCES
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/crc32.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/deflate.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inffast.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inffixed.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inflate.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inftrees.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/trees.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/zlib.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/zutil.h
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/adler32.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/compress.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/crc32.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/deflate.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/infback.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inffast.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inflate.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/inftrees.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/trees.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/uncompr.c
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/zlib-1.2.11/zutil.c
|
|
||||||
)
|
|
||||||
|
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
set_source_files_properties (${ZLIB_SOURCES} PROPERTIES COMPILE_FLAGS "/wd4127 /wd4131 /wd4244")
|
set_source_files_properties (${ZLIB_SOURCES} PROPERTIES COMPILE_FLAGS "/wd4127 /wd4131 /wd4244")
|
||||||
endif()
|
endif()
|
||||||
|
@@ -7,20 +7,6 @@
|
|||||||
# Official repository: https://github.com/boostorg/beast
|
# Official repository: https://github.com/boostorg/beast
|
||||||
#
|
#
|
||||||
|
|
||||||
local ZLIB_SOURCES =
|
|
||||||
zlib-1.2.11/adler32.c
|
|
||||||
zlib-1.2.11/compress.c
|
|
||||||
zlib-1.2.11/crc32.c
|
|
||||||
zlib-1.2.11/deflate.c
|
|
||||||
zlib-1.2.11/infback.c
|
|
||||||
zlib-1.2.11/inffast.c
|
|
||||||
zlib-1.2.11/inflate.c
|
|
||||||
zlib-1.2.11/inftrees.c
|
|
||||||
zlib-1.2.11/trees.c
|
|
||||||
zlib-1.2.11/uncompr.c
|
|
||||||
zlib-1.2.11/zutil.c
|
|
||||||
;
|
|
||||||
|
|
||||||
local SOURCES =
|
local SOURCES =
|
||||||
error.cpp
|
error.cpp
|
||||||
deflate_stream.cpp
|
deflate_stream.cpp
|
||||||
|
@@ -11,3 +11,4 @@ add_subdirectory (buffers)
|
|||||||
add_subdirectory (parser)
|
add_subdirectory (parser)
|
||||||
add_subdirectory (utf8_checker)
|
add_subdirectory (utf8_checker)
|
||||||
add_subdirectory (wsload)
|
add_subdirectory (wsload)
|
||||||
|
add_subdirectory (zlib)
|
||||||
|
@@ -12,4 +12,5 @@ alias run-tests :
|
|||||||
parser//run-tests
|
parser//run-tests
|
||||||
wsload//run-tests
|
wsload//run-tests
|
||||||
utf8_checker//run-tests
|
utf8_checker//run-tests
|
||||||
|
#zlib//run-tests # Not built
|
||||||
;
|
;
|
||||||
|
28
test/bench/zlib/CMakeLists.txt
Normal file
28
test/bench/zlib/CMakeLists.txt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||||
|
#
|
||||||
|
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
#
|
||||||
|
# Official repository: https://github.com/boostorg/beast
|
||||||
|
#
|
||||||
|
|
||||||
|
GroupSources(include/boost/beast beast)
|
||||||
|
GroupSources(test/extras/include/boost/beast extras)
|
||||||
|
GroupSources(test/extern/zlib-1.2.11 zlib)
|
||||||
|
GroupSources(test/bench/zlib "/")
|
||||||
|
|
||||||
|
if (MSVC)
|
||||||
|
set_source_files_properties (${ZLIB_SOURCES} PROPERTIES COMPILE_FLAGS "/wd4127 /wd4131 /wd4244")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_executable (bench-zlib
|
||||||
|
${BOOST_BEAST_INCLUDES}
|
||||||
|
${EXTRAS_INCLUDES}
|
||||||
|
${ZLIB_SOURCES}
|
||||||
|
${TEST_MAIN}
|
||||||
|
Jamfile
|
||||||
|
ztest.hpp
|
||||||
|
deflate_stream.cpp
|
||||||
|
inflate_stream.cpp
|
||||||
|
)
|
15
test/bench/zlib/Jamfile
Normal file
15
test/bench/zlib/Jamfile
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||||
|
#
|
||||||
|
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
#
|
||||||
|
# Official repository: https://github.com/boostorg/beast
|
||||||
|
#
|
||||||
|
|
||||||
|
exe bench-zlib :
|
||||||
|
$(ZLIB_SOURCES)
|
||||||
|
$(TEST_MAIN)
|
||||||
|
deflate_stream.cpp
|
||||||
|
inflate_stream.cpp
|
||||||
|
;
|
200
test/bench/zlib/deflate_stream.cpp
Normal file
200
test/bench/zlib/deflate_stream.cpp
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// Official repository: https://github.com/boostorg/beast
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <boost/beast/core/string.hpp>
|
||||||
|
#include <boost/beast/zlib/deflate_stream.hpp>
|
||||||
|
#include <boost/beast/test/throughput.hpp>
|
||||||
|
#include <boost/beast/unit_test/dstream.hpp>
|
||||||
|
#include <boost/beast/unit_test/suite.hpp>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "zlib-1.2.11/zlib.h"
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace beast {
|
||||||
|
namespace zlib {
|
||||||
|
|
||||||
|
class deflate_stream_test : public beast::unit_test::suite
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Lots of repeats, limited char range
|
||||||
|
static
|
||||||
|
std::string
|
||||||
|
corpus1(std::size_t n)
|
||||||
|
{
|
||||||
|
static std::string const alphabet{
|
||||||
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
};
|
||||||
|
std::string s;
|
||||||
|
s.reserve(n + 5);
|
||||||
|
std::mt19937 g;
|
||||||
|
std::uniform_int_distribution<std::size_t> d0{
|
||||||
|
0, alphabet.size() - 1};
|
||||||
|
std::uniform_int_distribution<std::size_t> d1{
|
||||||
|
1, 5};
|
||||||
|
while(s.size() < n)
|
||||||
|
{
|
||||||
|
auto const rep = d1(g);
|
||||||
|
auto const ch = alphabet[d0(g)];
|
||||||
|
s.insert(s.end(), rep, ch);
|
||||||
|
}
|
||||||
|
s.resize(n);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random data
|
||||||
|
static
|
||||||
|
std::string
|
||||||
|
corpus2(std::size_t n)
|
||||||
|
{
|
||||||
|
std::string s;
|
||||||
|
s.reserve(n);
|
||||||
|
std::mt19937 g;
|
||||||
|
std::uniform_int_distribution<std::uint32_t> d0{0, 255};
|
||||||
|
while(n--)
|
||||||
|
s.push_back(static_cast<char>(d0(g)));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
doDeflateBeast(string_view const& in)
|
||||||
|
{
|
||||||
|
z_params zs;
|
||||||
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
deflate_stream ds;
|
||||||
|
ds.reset(
|
||||||
|
Z_DEFAULT_COMPRESSION,
|
||||||
|
15,
|
||||||
|
4,
|
||||||
|
Strategy::normal);
|
||||||
|
std::string out;
|
||||||
|
out.resize(deflate_upper_bound(in.size()));
|
||||||
|
zs.next_in = in.data();
|
||||||
|
zs.avail_in = in.size();
|
||||||
|
zs.next_out = &out[0];
|
||||||
|
zs.avail_out = out.size();
|
||||||
|
error_code ec;
|
||||||
|
ds.write(zs, Flush::full, ec);
|
||||||
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
|
out.resize(zs.total_out);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
doDeflateZLib(string_view const& in)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
z_stream zs;
|
||||||
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
result = deflateInit2(
|
||||||
|
&zs,
|
||||||
|
Z_DEFAULT_COMPRESSION,
|
||||||
|
Z_DEFLATED,
|
||||||
|
-15,
|
||||||
|
4,
|
||||||
|
Z_DEFAULT_STRATEGY
|
||||||
|
);
|
||||||
|
if(result != Z_OK)
|
||||||
|
throw std::logic_error("deflateInit2 failed");
|
||||||
|
std::string out;
|
||||||
|
out.resize(deflateBound(&zs,
|
||||||
|
static_cast<uLong>(in.size())));
|
||||||
|
zs.next_in = (Bytef*)in.data();
|
||||||
|
zs.avail_in = static_cast<uInt>(in.size());
|
||||||
|
zs.next_out = (Bytef*)&out[0];
|
||||||
|
zs.avail_out = static_cast<uInt>(out.size());
|
||||||
|
result = deflate(&zs, Z_FULL_FLUSH);
|
||||||
|
if(result != Z_OK)
|
||||||
|
throw std::logic_error("deflate failed");
|
||||||
|
out.resize(zs.total_out);
|
||||||
|
deflateEnd(&zs);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
doCorpus(
|
||||||
|
std::size_t size,
|
||||||
|
std::size_t repeat)
|
||||||
|
{
|
||||||
|
std::size_t constexpr trials = 3;
|
||||||
|
auto const c1 = corpus1(size);
|
||||||
|
auto const c2 = corpus2(size);
|
||||||
|
log <<
|
||||||
|
std::left << std::setw(10) << (std::to_string(size) + "B") <<
|
||||||
|
std::right << std::setw(12) << "Beast" << " " <<
|
||||||
|
std::right << std::setw(12) << "ZLib" <<
|
||||||
|
std::endl;
|
||||||
|
for(std::size_t i = 0; i < trials; ++i)
|
||||||
|
{
|
||||||
|
test::timer t;
|
||||||
|
log << std::left << std::setw(10) << "corpus1";
|
||||||
|
std::string out1;
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out1 = doDeflateBeast(c1);
|
||||||
|
auto const t1 =
|
||||||
|
test::throughput(t.elapsed(), size * repeat);
|
||||||
|
log << std::right << std::setw(12) << t1 << " B/s ";
|
||||||
|
std::string out2;
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out2 = doDeflateZLib(c1);
|
||||||
|
BEAST_EXPECT(out1 == out2);
|
||||||
|
auto const t2 =
|
||||||
|
test::throughput(t.elapsed(), size * repeat);
|
||||||
|
log << std::right << std::setw(12) << t2 << " B/s";
|
||||||
|
log << std::right << std::setw(12) <<
|
||||||
|
unsigned(double(t1)*100/t2-100) << "%";
|
||||||
|
log << std::endl;
|
||||||
|
}
|
||||||
|
for(std::size_t i = 0; i < trials; ++i)
|
||||||
|
{
|
||||||
|
test::timer t;
|
||||||
|
log << std::left << std::setw(10) << "corpus2";
|
||||||
|
std::string out1;
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out1 = doDeflateBeast(c2);
|
||||||
|
auto const t1 =
|
||||||
|
test::throughput(t.elapsed(), size * repeat);
|
||||||
|
log << std::right << std::setw(12) << t1 << " B/s ";
|
||||||
|
std::string out2;
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out2 = doDeflateZLib(c2);
|
||||||
|
BEAST_EXPECT(out1 == out2);
|
||||||
|
auto const t2 =
|
||||||
|
test::throughput(t.elapsed(), size * repeat);
|
||||||
|
log << std::right << std::setw(12) << t2 << " B/s";
|
||||||
|
log << std::right << std::setw(12) <<
|
||||||
|
unsigned(double(t1)*100/t2-100) << "%";
|
||||||
|
log << std::endl;
|
||||||
|
}
|
||||||
|
log << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
doBench()
|
||||||
|
{
|
||||||
|
doCorpus( 16 * 1024, 512);
|
||||||
|
doCorpus( 1024 * 1024, 8);
|
||||||
|
doCorpus(8 * 1024 * 1024, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
run() override
|
||||||
|
{
|
||||||
|
doBench();
|
||||||
|
pass();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_BEAST_DEFINE_TESTSUITE(deflate_stream,zlib,beast);
|
||||||
|
|
||||||
|
} // zlib
|
||||||
|
} // beast
|
||||||
|
} // boost
|
245
test/bench/zlib/inflate_stream.cpp
Normal file
245
test/bench/zlib/inflate_stream.cpp
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// Official repository: https://github.com/boostorg/beast
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <boost/beast/core/string.hpp>
|
||||||
|
#include <boost/beast/zlib/inflate_stream.hpp>
|
||||||
|
#include <boost/beast/test/throughput.hpp>
|
||||||
|
#include <boost/beast/unit_test/dstream.hpp>
|
||||||
|
#include <boost/beast/unit_test/suite.hpp>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "zlib-1.2.11/zlib.h"
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace beast {
|
||||||
|
namespace zlib {
|
||||||
|
|
||||||
|
class inflate_stream_test : public beast::unit_test::suite
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Lots of repeats, limited char range
|
||||||
|
static
|
||||||
|
std::string
|
||||||
|
corpus1(std::size_t n)
|
||||||
|
{
|
||||||
|
static std::string const alphabet{
|
||||||
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
};
|
||||||
|
std::string s;
|
||||||
|
s.reserve(n + 5);
|
||||||
|
std::mt19937 g;
|
||||||
|
std::uniform_int_distribution<std::size_t> d0{
|
||||||
|
0, alphabet.size() - 1};
|
||||||
|
std::uniform_int_distribution<std::size_t> d1{
|
||||||
|
1, 5};
|
||||||
|
while(s.size() < n)
|
||||||
|
{
|
||||||
|
auto const rep = d1(g);
|
||||||
|
auto const ch = alphabet[d0(g)];
|
||||||
|
s.insert(s.end(), rep, ch);
|
||||||
|
}
|
||||||
|
s.resize(n);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random data
|
||||||
|
static
|
||||||
|
std::string
|
||||||
|
corpus2(std::size_t n)
|
||||||
|
{
|
||||||
|
std::string s;
|
||||||
|
s.reserve(n);
|
||||||
|
std::mt19937 g;
|
||||||
|
std::uniform_int_distribution<std::uint32_t> d0{0, 255};
|
||||||
|
while(n--)
|
||||||
|
s.push_back(static_cast<char>(d0(g)));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
std::string
|
||||||
|
compress(string_view const& in)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
z_stream zs;
|
||||||
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
result = deflateInit2(
|
||||||
|
&zs,
|
||||||
|
Z_DEFAULT_COMPRESSION,
|
||||||
|
Z_DEFLATED,
|
||||||
|
-15,
|
||||||
|
4,
|
||||||
|
Z_DEFAULT_STRATEGY);
|
||||||
|
if(result != Z_OK)
|
||||||
|
throw std::logic_error("deflateInit2 failed");
|
||||||
|
zs.next_in = (Bytef*)in.data();
|
||||||
|
zs.avail_in = static_cast<uInt>(in.size());
|
||||||
|
std::string out;
|
||||||
|
out.resize(deflateBound(&zs,
|
||||||
|
static_cast<uLong>(in.size())));
|
||||||
|
zs.next_in = (Bytef*)in.data();
|
||||||
|
zs.avail_in = static_cast<uInt>(in.size());
|
||||||
|
zs.next_out = (Bytef*)&out[0];
|
||||||
|
zs.avail_out = static_cast<uInt>(out.size());
|
||||||
|
result = deflate(&zs, Z_FULL_FLUSH);
|
||||||
|
if(result != Z_OK)
|
||||||
|
throw std::logic_error("deflate failed");
|
||||||
|
out.resize(zs.total_out);
|
||||||
|
deflateEnd(&zs);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
doInflateBeast(string_view const& in)
|
||||||
|
{
|
||||||
|
z_params zs;
|
||||||
|
std::string out;
|
||||||
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
inflate_stream is;
|
||||||
|
zs.next_in = &in[0];
|
||||||
|
zs.avail_in = in.size();
|
||||||
|
out.resize(in.size());
|
||||||
|
zs.next_out = &out[0];
|
||||||
|
zs.avail_out = out.size();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
error_code ec;
|
||||||
|
is.write(zs, Flush::sync, ec);
|
||||||
|
if(ec)
|
||||||
|
throw std::logic_error("inflate_stream failed");
|
||||||
|
if(zs.avail_out > 0)
|
||||||
|
break;
|
||||||
|
out.resize(2 * zs.total_out);
|
||||||
|
zs.next_out = &out[zs.total_out];
|
||||||
|
zs.avail_out = out.size() - zs.total_out;
|
||||||
|
}
|
||||||
|
out.resize(zs.total_out);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
doInflateZLib(string_view const& in)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
z_stream zs;
|
||||||
|
std::string out;
|
||||||
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
result = inflateInit2(&zs, -15);
|
||||||
|
zs.next_in = (Bytef*)in.data();
|
||||||
|
zs.avail_in = static_cast<uInt>(in.size());
|
||||||
|
out.resize(in.size());
|
||||||
|
zs.next_out = (Bytef*)&out[0];
|
||||||
|
zs.avail_out = static_cast<uInt>(out.size());
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
result = inflate(&zs, Z_SYNC_FLUSH);
|
||||||
|
if( result == Z_NEED_DICT ||
|
||||||
|
result == Z_DATA_ERROR ||
|
||||||
|
result == Z_MEM_ERROR)
|
||||||
|
{
|
||||||
|
throw std::logic_error("inflate failed");
|
||||||
|
}
|
||||||
|
if(zs.avail_out > 0)
|
||||||
|
break;
|
||||||
|
if(result == Z_STREAM_END)
|
||||||
|
break;
|
||||||
|
out.resize(2 * zs.total_out);
|
||||||
|
zs.next_out = (Bytef*)&out[zs.total_out];
|
||||||
|
zs.avail_out = static_cast<uInt>(
|
||||||
|
out.size() - zs.total_out);
|
||||||
|
}
|
||||||
|
out.resize(zs.total_out);
|
||||||
|
inflateEnd(&zs);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
doCorpus(
|
||||||
|
std::size_t size,
|
||||||
|
std::size_t repeat)
|
||||||
|
{
|
||||||
|
std::size_t constexpr trials = 3;
|
||||||
|
std::uint64_t constexpr scale = 16;
|
||||||
|
auto const c1 = corpus1(size);
|
||||||
|
auto const c2 = corpus2(size * scale);
|
||||||
|
auto const in1 = compress(c1);
|
||||||
|
auto const in2 = compress(c2);
|
||||||
|
log <<
|
||||||
|
std::left << std::setw(10) << (std::to_string(size) + "B") <<
|
||||||
|
std::right << std::setw(12) << "Beast" << " " <<
|
||||||
|
std::right << std::setw(12) << "ZLib" <<
|
||||||
|
std::endl;
|
||||||
|
for(std::size_t i = 0; i < trials; ++i)
|
||||||
|
{
|
||||||
|
test::timer t;
|
||||||
|
log << std::left << std::setw(10) << "corpus1";
|
||||||
|
std::string out;
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out = doInflateBeast(in1);
|
||||||
|
BEAST_EXPECT(out == c1);
|
||||||
|
auto const t1 =
|
||||||
|
test::throughput(t.elapsed(), size * repeat);
|
||||||
|
log << std::right << std::setw(12) << t1 << " B/s ";
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out = doInflateZLib(in1);
|
||||||
|
BEAST_EXPECT(out == c1);
|
||||||
|
auto const t2 =
|
||||||
|
test::throughput(t.elapsed(), size * repeat);
|
||||||
|
log << std::right << std::setw(12) << t2 << " B/s";
|
||||||
|
log << std::right << std::setw(12) <<
|
||||||
|
unsigned(double(t1)*100/t2-100) << "%";
|
||||||
|
log << std::endl;
|
||||||
|
}
|
||||||
|
for(std::size_t i = 0; i < trials; ++i)
|
||||||
|
{
|
||||||
|
test::timer t;
|
||||||
|
log << std::left << std::setw(10) << "corpus2";
|
||||||
|
std::string out;
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out = doInflateBeast(in2);
|
||||||
|
BEAST_EXPECT(out == c2);
|
||||||
|
auto const t1 =
|
||||||
|
test::throughput(t.elapsed(), size * scale * repeat);
|
||||||
|
log << std::right << std::setw(12) << t1 << " B/s ";
|
||||||
|
for(std::size_t j = 0; j < repeat; ++j)
|
||||||
|
out = doInflateZLib(in2);
|
||||||
|
BEAST_EXPECT(out == c2);
|
||||||
|
auto const t2 =
|
||||||
|
test::throughput(t.elapsed(), size * scale * repeat);
|
||||||
|
log << std::right << std::setw(12) << t2 << " B/s";
|
||||||
|
log << std::right << std::setw(12) <<
|
||||||
|
unsigned(double(t1)*100/t2-100) << "%";
|
||||||
|
log << std::endl;
|
||||||
|
}
|
||||||
|
log << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
doBench()
|
||||||
|
{
|
||||||
|
doCorpus( 1 * 1024 * 1024, 64);
|
||||||
|
doCorpus( 4 * 1024 * 1024, 16);
|
||||||
|
doCorpus( 16 * 1024 * 1024, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
run() override
|
||||||
|
{
|
||||||
|
doBench();
|
||||||
|
pass();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_BEAST_DEFINE_TESTSUITE(inflate_stream,zlib,beast);
|
||||||
|
|
||||||
|
} // zlib
|
||||||
|
} // beast
|
||||||
|
} // boost
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user