package peergos.server.tests.slow;

import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import peergos.server.*;
import peergos.server.tests.*;
import peergos.server.util.*;
import peergos.shared.*;
import peergos.shared.user.*;

import java.net.*;
import java.util.*;
import java.util.stream.*;

@RunWith(Parameterized.class)
public class SocialBenchmark {

    private static int RANDOM_SEED = 666;
    private final NetworkAccess network;
    private final Crypto crypto = Crypto.initJava();

    private static Random random = new Random(RANDOM_SEED);

    public SocialBenchmark(String useIPFS, Random r) throws Exception {
        this.network = buildHttpNetworkAccess(useIPFS.equals("IPFS"), r);
    }

    private static NetworkAccess buildHttpNetworkAccess(boolean useIpfs, Random r) throws Exception {
        Args args = UserTests.buildArgs().with("useIPFS", "" + useIpfs);
        Main.PKI_INIT.main(args);
        return NetworkAccess.buildJava(new URL("http://localhost:" + args.getInt("port"))).get();
    }

    @Parameterized.Parameters()
    public static Collection<Object[]> parameters() {
        return Arrays.asList(new Object[][] {
//                {"IPFS", new Random(0)}
                {"NOTIPFS", new Random(0)}
        });
    }

    // SendFollowRequest(19) duration: 2132 mS, best: 1891 mS, worst: 2576 mS, av: 2110 mS
    //    pointers.set: 4 * 80 mS = 320 mS
    @Test
    public void social() {
        String username = generateUsername();
        String password = "test01";
        UserContext context = ensureSignedUp(username, password, network, crypto);
        List<String> names = new ArrayList<>();
        IntStream.range(0, 20).forEach(i -> names.add(generateUsername()));
        names.forEach(name -> ensureSignedUp(name, password, network, crypto));

        long worst = 0, best = Long.MAX_VALUE, start = System.currentTimeMillis();

        for (int i = 0; i < 20; i++) {
            long t1 = System.currentTimeMillis();
            context.sendInitialFollowRequest(names.get(i)).join();
            long duration = System.currentTimeMillis() - t1;
            worst = Math.max(worst, duration);
            best = Math.min(best, duration);
            System.err.printf("SendFollowRequest(%d) duration: %d mS, best: %d mS, worst: %d mS, av: %d mS\n", i,
                    duration, best, worst, (t1 + duration - start) / (i + 1));
        }
    }

    private String generateUsername() {
        return "test" + (random.nextInt() % 10000);
    }

    public static UserContext ensureSignedUp(String username, String password, NetworkAccess network, Crypto crypto) {
        return UserContext.ensureSignedUp(username, password, network, crypto).join();
    }
}

@@ -211,24 +211,56 @@
 s.shared.*;%0A
+import peergos.shared.social.*;%0A
 import peerg
@@ -277,16 +277,16 @@
 user.*;%0A
-
 %0Aimport 
@@ -1403,51 +1403,8 @@
  mS%0A
-    //    pointers.set: 4 * 80 mS = 320 mS%0A
     
@@ -2191,32 +2191,32 @@
 est, duration);%0A
-
             Syst
@@ -2401,32 +2401,1570 @@
        %7D%0A    %7D%0A%0A
+    // ReplyToFollowRequest(19) duration: 4291 mS, best: 3392 mS, worst: 5239 mS, av: 3898 mS%0A    @Test%0A    public void replyToFollowRequest() %7B%0A        String username = generateUsername();%0A        String password = %22test01%22;%0A        UserContext context = ensureSignedUp(username, password, network, crypto);%0A        List%3CString%3E names = new ArrayList%3C%3E();%0A        IntStream.range(0, 20).forEach(i -%3E names.add(generateUsername()));%0A        List%3CUserContext%3E users = names.stream()%0A                .map(name -%3E ensureSignedUp(name, password, network, crypto))%0A                .collect(Collectors.toList());%0A%0A        for (int i = 0; i %3C 20; i++) %7B%0A            users.get(i).sendInitialFollowRequest(username).join();%0A        %7D%0A%0A        List%3CFollowRequestWithCipherText%3E pending = context.getSocialState().join().pendingIncoming;%0A        long worst = 0, best = Long.MAX_VALUE, start = System.currentTimeMillis();%0A        // Profile accepting the requests%0A        for (int i = 0; i %3C 20; i++) %7B%0A            FollowRequestWithCipherText req = pending.get(i);%0A            long t1 = System.currentTimeMillis();%0A            context.sendReplyFollowRequest(req, true, true).join();%0A            long duration = System.currentTimeMillis() - t1;%0A            worst = Math.max(worst, duration);%0A            best = Math.min(best, duration);%0A            System.err.printf(%22ReplyToFollowRequest(%25d) duration: %25d mS, best: %25d mS, worst: %25d mS, av: %25d mS%5Cn%22, i,%0A                    duration, best, worst, (t1 + duration - start) / (i + 1));%0A        %7D%0A    %7D%0A%0A
     private Stri

Add benchmark for replying to follow request