Statistics { public double estimatedNumberOfNodes() { final List<Map<Number160, PeerStatistic>> map = peerMap.peerMapVerified(); double gap = 0D; int gapCount = 0; for (int i = 0; i < Number160.BITS; i++) { Map<Number160, PeerStatistic> peers = map.get(i); final int numPeers = peers.size(); if (numPeers > 0 && numPeers < peerMap.bagSizeVerified(i)) { double currentGap = Math.pow(2, i) / numPeers; gap += currentGap * numPeers; gapCount += numPeers; } else if (numPeers == 0) { } else if (numPeers == peerMap.bagSizeVerified(i)) { } } avgGap = gap / gapCount; estimatedNumberOfPeers = (MAX / avgGap); return estimatedNumberOfPeers; } Statistics(final PeerMap peerMap); double estimatedNumberOfNodes(); double avgGap();  }
@Test public void testCountPeers1() throws UnknownHostException { Random rnd = new Random(42L); for (int j = 1; j < 10; j++) { int nr = 100000 * j; PeerMapConfiguration conf = new PeerMapConfiguration(ID); conf.setFixedVerifiedBagSizes(20).setFixedOverflowBagSizes(20); conf.offlineCount(1000).offlineTimeout(60); conf.addMapPeerFilter(new DefaultPeerFilter()).maintenance(new DefaultMaintenance(0, new int[] {})); PeerMap peerMap = new PeerMap(conf); Statistics statistics = new Statistics(peerMap); for (int i = 0; i < nr; i++) { PeerAddress pa = Utils2.createAddress(new Number160(rnd)); peerMap.peerFound(pa, null, null, null); } double diff = nr / statistics.estimatedNumberOfNodes(); System.err.println("diff: " + diff); System.err.println("estimated: " + statistics.estimatedNumberOfNodes() + " actual: " + (nr-1)); Assert.assertTrue(diff < 1.5 && diff > 0.5); } }