added high performance bdecoder code

This commit is contained in:
Arvid Norberg
2008-04-11 03:41:09 +00:00
parent 093d912e9a
commit b300c7f835
8 changed files with 476 additions and 1 deletions

View File

@@ -37,6 +37,7 @@ test-suite libtorrent :
[ run test_fast_extension.cpp ]
[ run test_pe_crypto.cpp ]
[ run test_bencoding.cpp ]
[ run test_bdecode_performance.cpp ]
[ run test_primitives.cpp ]
[ run test_ip_filter.cpp ]
[ run test_hasher.cpp ]

View File

@@ -0,0 +1,27 @@
#include "libtorrent/lazy_entry.hpp"
#include <boost/lexical_cast.hpp>
#include <iostream>
#include "test.hpp"
#include "libtorrent/time.hpp"
using namespace libtorrent;
int test_main()
{
using namespace libtorrent;
ptime start(time_now());
for (int i = 0; i < 1000000; ++i)
{
char b[] = "d1:ai12453e1:b3:aaa1:c3:bbbe";
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
}
ptime stop(time_now());
std::cout << "done in " << total_milliseconds(stop - start) / 1000. << " seconds per million message" << std::endl;
return 0;
}

View File

@@ -1,4 +1,5 @@
#include "libtorrent/bencode.hpp"
#include "libtorrent/lazy_entry.hpp"
#include <boost/lexical_cast.hpp>
#include "test.hpp"
@@ -68,7 +69,52 @@ int test_main()
TEST_CHECK(encode(e) == "d3:cow3:moo4:spam4:eggse");
TEST_CHECK(decode(encode(e)) == e);
}
{
char b[] = "i12453e";
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
TORRENT_ASSERT(e.type() == lazy_entry::int_t);
TORRENT_ASSERT(e.int_value() == 12453);
}
{
char b[] = "26:abcdefghijklmnopqrstuvwxyz";
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
TORRENT_ASSERT(e.type() == lazy_entry::string_t);
TORRENT_ASSERT(e.string_value() == std::string("abcdefghijklmnopqrstuvwxyz"));
}
{
char b[] = "li12453e3:aaae";
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
TORRENT_ASSERT(e.type() == lazy_entry::list_t);
TORRENT_ASSERT(e.list_size() == 2);
TORRENT_ASSERT(e.list_at(0)->type() == lazy_entry::int_t);
TORRENT_ASSERT(e.list_at(1)->type() == lazy_entry::string_t);
TORRENT_ASSERT(e.list_at(0)->int_value() == 12453);
TORRENT_ASSERT(e.list_at(1)->string_value() == std::string("aaa"));
}
{
char b[] = "d1:ai12453e1:b3:aaa1:c3:bbbe";
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
TORRENT_ASSERT(e.type() == lazy_entry::dict_t);
TORRENT_ASSERT(e.dict_size() == 3);
TORRENT_ASSERT(e.dict_find("a")->type() == lazy_entry::int_t);
TORRENT_ASSERT(e.dict_find("a")->int_value() == 12453);
TORRENT_ASSERT(e.dict_find("b")->type() == lazy_entry::string_t);
TORRENT_ASSERT(e.dict_find("b")->string_value() == std::string("aaa"));
TORRENT_ASSERT(e.dict_find("c")->type() == lazy_entry::string_t);
TORRENT_ASSERT(e.dict_find("c")->string_value() == std::string("bbb"));
}
return 0;
}