Merging from branches/v0.5-peernet/libbitdht (--- Merging r4354 through r4356 into '.')

* adding Bloom filter to speed up finding RS peers.
 * fixed startup bug. (loops into FAILURE)




git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4357 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-06-29 16:06:33 +00:00
parent 702ff44194
commit 79b48d5a89
15 changed files with 820 additions and 21 deletions

View file

@ -11,14 +11,19 @@ include $(TEST_TOP_DIR)/scripts/config.mk
# Generic Test Harnesses.
TESTOBJ = bdmetric_test.o bdmsgs_test.o bdnode_test.o bdspace_test.o
TESTOBJ += bdmgr_multitest.o bdnode_multitest1.o bdquery_test.o bdstore_test.o
TESTOBJ += bdmidids_test.o bdnode_test2.o bdspace_test2.o udpbitdht_nettest.o
TESTOBJ += bdmgr_multitest.o bdquery_test.o bdstore_test.o
TESTOBJ += bdmidids_test.o bdspace_test2.o udpbitdht_nettest.o
TESTOBJ += bdbloom_test.o bdbloom_makefilter.o
#TESTOBJ += bencode_test.o bdudp_test.o
#TESTOBJ = bdnode_test.o bdnode_multitest1.o bdnode_test2.o
TESTS = bdmetric_test bdmsgs_test bdnode_test bdspace_test
TESTS += bdmgr_multitest bdnode_multitest1 bdquery_test bdstore_test
TESTS += bdmidids_test bdnode_test2 bdspace_test2 udpbitdht_nettest
TESTS = bdmetric_test bdmsgs_test bdspace_test
TESTS += bdmgr_multitest bdquery_test bdstore_test
TESTS += bdmidids_test bdspace_test2 udpbitdht_nettest
TESTS += bdbloom_test
#TESTS += bencode_test bdudp_test
#Tests to Fix.
#TESTS = bdnode_test bdnode_multitest1 bdnode_test2
MANUAL_TESTS =
@ -68,6 +73,12 @@ udpbitdht_nettest: udpbitdht_nettest.o
bencode_test: bencode_test.o
$(CC) $(CFLAGS) -o bencode_test bencode_test.o $(LIBS)
bdbloom_test: bdbloom_test.o
$(CC) $(CFLAGS) -o bdbloom_test bdbloom_test.o $(LIBS)
bdbloom_makefilter: bdbloom_makefilter.o
$(CC) $(CFLAGS) -o bdbloom_makefilter bdbloom_makefilter.o $(LIBS)
clobber: remove_extra_files

View file

@ -0,0 +1,91 @@
/*
* bitdht/bdbloom_test.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "util/bdbloom.h"
#include "bitdht/bdstddht.h"
#include <iostream>
#include <sstream>
#define N_TESTS 100
int main(int argc, char **argv)
{
/* read from the file */
if (argc < 2)
{
std::cerr << "Missing Hash File";
std::cerr << std::endl;
}
FILE *fd = fopen(argv[1], "r");
if (!fd)
{
std::cerr << "Failed to Open File: " << argv[1];
std::cerr << std::endl;
return 1;
}
char line[1000];
bdBloom filter;
int nHashes = 0;
while(fgets(line, 1000-1, fd))
{
std::string hash = line;
std::cerr << "Read Hash: " << hash;
std::cerr << std::endl;
filter.add(hash);
nHashes++;
}
fclose(fd);
filter.printFilter(std::cerr);
int bits = filter.countBits();
int filterBits = filter.filterBits();
std::cerr << "Filter Bits Set: " << bits;
std::cerr << std::endl;
double possible = ((double) bits) * bits;
double max = ((double) filterBits) * filterBits;
std::cerr << "Therefore Possible Finds: " << possible << "/" << max << " = %" << 100 * possible / max;
std::cerr << std::endl;
std::cerr << "With Insertions: " << nHashes;
std::cerr << std::endl;
std::cerr << std::endl;
std::cerr << std::endl;
std::cerr << "Filter String: " << filter.getFilter() << std::endl;
std::cerr << std::endl;
std::cerr << std::endl;
return 1;
}

View file

@ -0,0 +1,165 @@
/*
* bitdht/bdbloom_test.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "util/bdbloom.h"
#include "bitdht/bdstddht.h"
#include <iostream>
#include <sstream>
#define N_TESTS 100
int main(int argc, char **argv)
{
/* Do this multiple times */
int i;
bdBloom filter;
std::list<bdNodeId> testIds;
std::list<bdNodeId>::iterator it;
for(i = 0; i < N_TESTS; i++)
{
bdNodeId targetId;
bdStdRandomNodeId(&targetId);
testIds.push_back(targetId);
}
std::cerr << "Test bdBloom Filter...." << std::endl;
for(it = testIds.begin(); it != testIds.end(); it++)
{
bdNodeId targetId = *it;
std::cerr << "-------------------------------------------------" << std::endl;
std::cerr << "Inserting : ";
std::ostringstream str;
bdStdPrintNodeId(str, &targetId);
std::cerr << str.str();
std::cerr << std::endl;
filter.add(str.str());
filter.printFilter(std::cerr);
}
std::string fs1 = filter.getFilter();
/* now extract, and reinsert filter */
bdBloom filter2;
filter2.setFilterBits(fs1);
std::string fs2 = filter2.getFilter();
filter2.printFilter(std::cerr);
if (fs1 == fs2)
{
std::cerr << "SUCCESS: Filter Correctly Transferred!";
std::cerr << std::endl;
}
else
{
std::cerr << "FAILURE: Filter Not Transferred!";
std::cerr << std::endl;
}
for(it = testIds.begin(); it != testIds.end(); it++)
{
bdNodeId targetId = *it;
std::cerr << "-------------------------------------------------" << std::endl;
std::cerr << "Testing : ";
std::cerr << std::endl;
std::ostringstream str;
bdStdPrintNodeId(str, &targetId);
std::cerr << str.str() << std::endl;
if (filter2.test(str.str()))
{
std::cerr << "SUCCESS: Filter Found Entry";
std::cerr << std::endl;
}
else
{
std::cerr << "FAILURE: Filter Didn't Found Entry";
std::cerr << std::endl;
}
}
int bits = filter.countBits();
int filterBits = filter.filterBits();
std::cerr << "Filter Bits Set: " << bits;
std::cerr << std::endl;
double possible = ((double) bits) * bits;
double max = ((double) filterBits) * filterBits;
std::cerr << "Therefore Possible Finds: " << possible << "/" << max << " = %" << 100 * possible / max;
std::cerr << std::endl;
std::cerr << "With Insertions: " << N_TESTS;
std::cerr << std::endl;
#define FINAL_TESTS (1000000)
int found = 0;
int didnt = 0;
for(i = 0; i < FINAL_TESTS; i++)
{
if ((i != 0) && (i % 100000 == 0))
{
std::cerr << "Run " << i << " Checks" << std::endl;
}
bdNodeId targetId;
bdStdRandomNodeId(&targetId);
std::ostringstream str;
bdStdPrintNodeId(str, &targetId);
if (filter2.test(str.str()))
{
found++;
}
else
{
didnt++;
}
}
std::cerr << "Final Stats: " << FINAL_TESTS << " random checks done";
std::cerr << std::endl;
std::cerr << "\t" << found << " " << 100.0 * ((double) found) / FINAL_TESTS << "% found";
std::cerr << std::endl;
std::cerr << "\t" << didnt << " " << 100.0 * ((double) didnt) / FINAL_TESTS << "% didnt";
std::cerr << std::endl;
return 1;
}

View file

@ -88,7 +88,7 @@ int main(int argc, char **argv)
for(j = 0; j < 5; j++)
{
int peeridx = bdRand::random_u32() % n_nodes;
int peeridx = bdRandom::random_u32() % n_nodes;
bdId pid = portIdx[peeridx];
node->addPotentialPeer(&pid, NULL);

View file

@ -56,7 +56,7 @@ int main(int argc, char **argv)
node.printState();
#if 0
for(i = 0; i < N_QUERIES; i++)
{
/* create a query */
@ -65,6 +65,7 @@ int main(int argc, char **argv)
node.addQuery(&queryId, 0);
}
#endif
node.printState();