Skip to content

Commit

Permalink
Added performance tests for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettRegnier committed Dec 7, 2022
1 parent 3c49f47 commit a42b842
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ CMD cmake -B build -DBUILD_TESTS=OFF && cmake --build build && build/bin/numero_

FROM base as test
CMD cmake -B build -DBUILD_TESTS=ON && cmake --build build \
&& ctest --test-dir build/tests --output-on-failure
&& ctest --test-dir build/tests --output-on-failure --verbose
91 changes: 91 additions & 0 deletions lib/inc/UrlEncoder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,91 @@ public:
const std::uint16_t Pen_Bits = 24;
const std::uint16_t Sub_Pen_Bits = 8;

/*
* UrlEncoder::UrlEncoder
*
* Description:
* Initializes an empty UrlEncoder
*
* Parameters:
*
* Returns:
*
* Comments:
*
*/
UrlEncoder();

/*
* UrlEncoder::UrlEncoder
*
* Description:
* Initializes an UrlEncoder with the given template
*
* Parameters:
* init_template [in]
* A string containing a template
*
* Returns:
*
* Comments:
*
*/
UrlEncoder(const std::string &init_template);

/*
* UrlEncoder::UrlEncoder
*
* Description:
* Initializes an UrlEncoder with the given templates
*
* Parameters:
* init_template [in]
* A vector of strings containing templates
*
* Returns:
*
* Comments:
*
*/
UrlEncoder(const std::vector<std::string> &init_templates);

/*
* UrlEncoder::UrlEncoder
*
* Description:
* Initializes an UrlEncoder with the given templates
*
* Parameters:
* init_template [in]
* A array of strings containing templates
* count [in]
* The size of the array
*
* Returns:
*
* Comments:
*
*/
UrlEncoder(const std::string* init_templates, const size_t count);

/*
* UrlEncoder::UrlEncoder
*
* Description:
* Initializes an UrlEncoder with the given templates
*
* Parameters:
* init_template [in]
* A json object that contains complete templates
*
* Returns:
*
* Comments:
*
*/
UrlEncoder(const json &init_templates);

/*
* UrlEncoder::EncodeUrl
*
Expand Down Expand Up @@ -386,6 +471,10 @@ public:
*/
const template_map& GetTemplate(std::uint64_t pen) const;


const std::uint64_t TemplateCount(
const bool count_sub_pen=true) const;

private:
/*
* UrlEncoder::PraseJson
Expand All @@ -403,5 +492,7 @@ private:
* Comments:
*/
pen_template_map ParseJson(const json& data) const;

/* Variables */
pen_template_map templates;
};
56 changes: 55 additions & 1 deletion lib/src/UrlEncoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
#include <regex>
#include <iostream>

UrlEncoder::UrlEncoder() : templates()
{

}

UrlEncoder::UrlEncoder(const std::string &init_template)
: templates()
{
AddTemplate(init_template);
}

UrlEncoder::UrlEncoder(const std::vector<std::string> &init_templates)
: templates()
{
AddTemplate(init_templates);
}

UrlEncoder::UrlEncoder(const std::string* init_templates, const size_t count)
: templates()
{
AddTemplate(init_templates, count);
}

UrlEncoder::UrlEncoder(const json& init_templates)
: templates()
{
AddTemplate(init_templates);
}

big_uint UrlEncoder::EncodeUrl(const std::string &url)
{
// To extract the 3 groups from each url format
Expand Down Expand Up @@ -322,7 +351,8 @@ void UrlEncoder::AddTemplate(const std::string& new_template,
if (temp_map.find(sub_pen.value()) != temp_map.end())
{
throw UrlEncoderException("Error. Sub PEN key already exists "
+ sub_pen.ToDecimalString());
+ sub_pen.ToDecimalString() + " for PEN key "
+ pen_value->ToDecimalString());
}
}

Expand Down Expand Up @@ -455,16 +485,24 @@ bool UrlEncoder::RemoveTemplate(const std::uint64_t pen)
bool UrlEncoder::RemoveSubTemplate(const std::uint32_t pen,
const std::uint8_t sub_pen)
{
// Check if the PEN exists
if (templates.find(pen) == templates.end())
return false;

// Get the template map for this PEN
auto temp_map = templates[pen];

// Check if this sub PEN exists
if (temp_map.find(sub_pen) == temp_map.end())
return false;

// Remove the sub PEN
temp_map.erase(sub_pen);

// If there are no more sub-PENs remove the PEN from the template
if (temp_map.size() == 0)
templates.erase(pen);

return true;
}

Expand Down Expand Up @@ -525,6 +563,22 @@ const UrlEncoder::template_map&
return templates.at(pen);
}

const std::uint64_t UrlEncoder::TemplateCount(const bool count_sub_pen) const
{
if (!count_sub_pen)
{
return templates.size();
}

std::uint64_t sz=0;
for (auto temp_map : templates)
{
sz += temp_map.second.size();
}

return sz;
}

/** Begin Private functions**/
UrlEncoder::pen_template_map UrlEncoder::ParseJson(const json& data) const
{
Expand Down
12 changes: 12 additions & 0 deletions tests/test_url_encoder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ target_link_libraries(TestUrlEncoder PUBLIC
add_test(
NAME TestUrlEncoder
COMMAND TestUrlEncoder
)

add_executable(TestUrlEncoderPerformance TestUrlEncoderPerformance.cc)

target_link_libraries(TestUrlEncoderPerformance PUBLIC
numero_uri_lib
gtest_main
)

add_test(
NAME TestUrlEncoderPerformance
COMMAND TestUrlEncoderPerformance
)
Loading

0 comments on commit a42b842

Please sign in to comment.