Search in sources :

Example 41 with RateLimiter

use of com.google.common.util.concurrent.RateLimiter in project web3sdk by FISCO-BCOS.

the class PerformanceDTTest method userAddTest.

public void userAddTest(BigInteger count, BigInteger qps) {
    try {
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(200);
        threadPool.setMaxPoolSize(500);
        threadPool.setQueueCapacity(count.intValue());
        threadPool.initialize();
        System.out.println("Start UserAdd test, count " + count);
        System.out.println("===================================================================");
        RateLimiter limiter = RateLimiter.create(qps.intValue());
        Integer area = count.intValue() / 10;
        long seconds = System.currentTimeMillis() / 1000l;
        this.collector.setStartTimestamp(System.currentTimeMillis());
        for (Integer i = 0; i < count.intValue(); ++i) {
            final int index = i;
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    limiter.acquire();
                    String user = Long.toHexString(seconds) + Integer.toHexString(index);
                    BigInteger amount = new BigInteger("1000000000");
                    DagTransferUser dtu = new DagTransferUser();
                    dtu.setUser(user);
                    dtu.setAmount(amount);
                    PerformanceDTCallback callback = new PerformanceDTCallback();
                    callback.setCollector(collector);
                    callback.setDagTransferUser(dtu);
                    callback.setDagUserMgr(getDagUserMgr());
                    callback.setCallBackType("add");
                    try {
                        callback.recordStartTime();
                        dagTransfer.userAdd(user, amount, callback);
                    } catch (Exception e) {
                        TransactionReceipt receipt = new TransactionReceipt();
                        receipt.setStatus("-1");
                        callback.onResponse(receipt);
                        logger.info(e.getMessage());
                    }
                    int current = sended.incrementAndGet();
                    if (current >= area && ((current % area) == 0)) {
                        System.out.println("Already sended: " + current + "/" + count + " transactions");
                    }
                }
            });
        }
        // end or not
        while (!collector.isEnd()) {
            Thread.sleep(2000);
            logger.info(" received: {}, total: {}", collector.getReceived().intValue(), collector.getTotal());
        }
        dagUserMgr.writeDagTransferUser();
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}
Also used : TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 42 with RateLimiter

use of com.google.common.util.concurrent.RateLimiter in project web3sdk by FISCO-BCOS.

the class PerformanceDTTest method veryTransferData.

public void veryTransferData(ThreadPoolTaskExecutor threadPool, BigInteger qps) {
    List<DagTransferUser> allUser = dagUserMgr.getUserList();
    Integer total_user = allUser.size();
    AtomicInteger verify_success = new AtomicInteger(0);
    AtomicInteger verify_failed = new AtomicInteger(0);
    allUser = dagUserMgr.getUserList();
    RateLimiter limiter = RateLimiter.create(qps.intValue());
    try {
        final DagTransfer _dagTransfer = dagTransfer;
        final List<DagTransferUser> _allUser = allUser;
        for (int i = 0; i < allUser.size(); ++i) {
            final Integer _i = i;
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        limiter.acquire();
                        Tuple2<BigInteger, BigInteger> result = _dagTransfer.userBalance(_allUser.get(_i).getUser()).send();
                        String user = _allUser.get(_i).getUser();
                        BigInteger local = _allUser.get(_i).getAmount();
                        BigInteger remote = result.getValue2();
                        if (result.getValue1().compareTo(new BigInteger("0")) != 0) {
                            logger.error(" query failed, user " + user + " ret code " + result.getValue1());
                            verify_failed.incrementAndGet();
                            return;
                        }
                        logger.debug(" user  " + user + " local amount  " + local + " remote amount " + remote);
                        if (local.compareTo(remote) != 0) {
                            verify_failed.incrementAndGet();
                            logger.error(" local amount is not same as remote, user " + user + " local " + local + " remote " + remote);
                        } else {
                            verify_success.incrementAndGet();
                        }
                    } catch (Exception e) {
                        logger.error("getAmount error: ", e);
                    }
                }
            });
        }
        while (verify_success.get() + verify_failed.get() < total_user) {
            Thread.sleep(40);
        }
        System.out.println("validation:");
        System.out.println(" \tuser count is " + total_user);
        System.out.println(" \tverify_success count is " + verify_success);
        System.out.println(" \tverify_failed count is " + verify_failed);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}
Also used : RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple2(org.fisco.bcos.web3j.tuples.generated.Tuple2) BigInteger(java.math.BigInteger)

Example 43 with RateLimiter

use of com.google.common.util.concurrent.RateLimiter in project web3sdk by FISCO-BCOS.

the class PerfomanceTableRemove method main.

public static void main(String[] args) throws Exception {
    try {
        String groupId = args[3];
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(Integer.parseInt(groupId));
        service.run();
        System.out.println("Start test...");
        System.out.println("===================================================================");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);
        Web3j web3 = Web3j.build(channelEthereumService, 15 * 100, scheduledExecutorService, Integer.parseInt(groupId));
        Credentials credentials = Credentials.create("b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
        BigInteger gasPrice = new BigInteger("30000000");
        BigInteger gasLimit = new BigInteger("30000000");
        String command = args[0];
        Integer count = 0;
        Integer qps = 0;
        switch(command) {
            case "trans":
                count = Integer.parseInt(args[1]);
                qps = Integer.parseInt(args[2]);
                break;
            default:
                System.out.println("Args: <trans> <Total> <QPS>");
        }
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(200);
        threadPool.setMaxPoolSize(500);
        threadPool.setQueueCapacity(count);
        threadPool.initialize();
        System.out.println("Deploying contract...");
        TableTest tabletest = TableTest.deploy(web3, credentials, gasPrice, gasLimit).send();
        PerfomanceCollector collector = new PerfomanceCollector();
        collector.setTotal(count);
        RateLimiter limiter = RateLimiter.create(qps);
        Integer area = count / 10;
        final Integer total = count;
        Random random = new Random(System.currentTimeMillis());
        System.out.println("Start test,total:" + count);
        for (Integer i = 0; i < count; ++i) {
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    limiter.acquire();
                    PerfomanceTableTestCallback callback = new PerfomanceTableTestCallback();
                    callback.setCollector(collector);
                    try {
                        long _id = getNextID();
                        Random r = new Random();
                        long l1 = r.nextLong();
                        tabletest.remove("fruit" + l1 % TableTestClient.modevalue, BigInteger.valueOf(_id), callback);
                    } catch (Exception e) {
                        TransactionReceipt receipt = new TransactionReceipt();
                        receipt.setStatus("-1");
                        callback.onResponse(receipt);
                        logger.error("Error sending:", e);
                    }
                    int current = sended.incrementAndGet();
                    if (current >= area && ((current % area) == 0)) {
                        System.out.println("Already sended: " + current + "/" + total + " transactions");
                    }
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(org.fisco.bcos.channel.client.Service) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) Random(java.util.Random) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Example 44 with RateLimiter

use of com.google.common.util.concurrent.RateLimiter in project web3sdk by FISCO-BCOS.

the class PerformanceRPC method main.

public static void main(String[] args) throws Exception {
    try {
        if (args.length < 3) {
            Usage();
        }
        int groupId = Integer.valueOf(args[0]);
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(groupId);
        service.run();
        System.out.println("Start test...");
        System.out.println("===================================================================");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);
        Web3j web3j = Web3j.build(channelEthereumService, 15 * 100, scheduledExecutorService, groupId);
        Integer count = Integer.parseInt(args[1]);
        Integer qps = Integer.parseInt(args[2]);
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(200);
        threadPool.setMaxPoolSize(500);
        threadPool.setQueueCapacity(count);
        threadPool.initialize();
        PerformanceRpcCollector collector = new PerformanceRpcCollector();
        collector.setTotal(count);
        RateLimiter limiter = RateLimiter.create(qps);
        Integer area = count / 10;
        final Integer total = count;
        System.out.println("Start test,total:" + count);
        for (Integer i = 0; i < count; ++i) {
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    limiter.acquire();
                    Response response = new Response();
                    try {
                        int random = new SecureRandom().nextInt(50000);
                        int methodNum = 10;
                        Long startTime = System.nanoTime();
                        switch(random % methodNum) {
                            // 1. call getPendingTxSize
                            case 0:
                                response = web3j.getPendingTxSize().send();
                                break;
                            // 2. call getBlockNumber
                            case 1:
                                response = web3j.getBlockNumber().send();
                                break;
                            // 3. call getSyncStatus
                            case 2:
                                response = web3j.getSyncStatus().send();
                                break;
                            // 5. call getSealerList
                            case 4:
                                response = web3j.getSealerList().send();
                                break;
                            // 6. call getTotalTransactionCount
                            case 5:
                                response = web3j.getTotalTransactionCount().send();
                                break;
                            // 7. call getObserverList
                            case 6:
                                response = web3j.getObserverList().send();
                                break;
                            // 8. call getBlockHashByNumber
                            case 7:
                                BigInteger blockNumber = web3j.getBlockNumber().send().getBlockNumber();
                                DefaultBlockParameter blockParam = DefaultBlockParameter.valueOf(blockNumber);
                                response = web3j.getBlockHashByNumber(blockParam).send();
                                break;
                            // 9. call getSystemConfigByKey
                            case 8:
                                response = web3j.getSystemConfigByKey("tx_count_limit").send();
                                break;
                            // 10. call getPbftView
                            case 9:
                                response = web3j.getPbftView().send();
                                break;
                            default:
                                // default call getPbftView
                                response = web3j.getPbftView().send();
                        }
                        Long cost = System.nanoTime() - startTime;
                        collector.onMessage(response, cost);
                    } catch (Exception e) {
                        logger.error("test rpc interface failed, error info: {}", e.getMessage());
                        Error error = new Error();
                        error.setCode(1);
                        response.setError(error);
                        collector.onMessage(response, 0L);
                    }
                    int current = sended.incrementAndGet();
                    if (current >= area && ((current % area) == 0)) {
                        System.out.println("Already sended: " + current + "/" + total + " RPC Requests");
                    }
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(org.fisco.bcos.channel.client.Service) SecureRandom(java.security.SecureRandom) Error(org.fisco.bcos.web3j.protocol.core.Response.Error) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Response(org.fisco.bcos.web3j.protocol.core.Response) DefaultBlockParameter(org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 45 with RateLimiter

use of com.google.common.util.concurrent.RateLimiter in project web3sdk by FISCO-BCOS.

the class PerformanceOkDSync method main.

public static void main(String[] args) throws Exception {
    try {
        String groupId = args[3];
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(Integer.parseInt(groupId));
        service.run();
        System.out.println("Start test...");
        System.out.println("===================================================================");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        if (args.length > 4) {
            Integer threadPoolSize = Integer.parseInt(args[4]);
            Async async = new Async(Executors.newFixedThreadPool(threadPoolSize));
            System.out.println(" === thread pool size = " + threadPoolSize);
        }
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);
        Web3j web3 = Web3j.build(channelEthereumService, 15 * 100, scheduledExecutorService, Integer.parseInt(groupId));
        Credentials credentials = Credentials.create("b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
        BigInteger gasPrice = new BigInteger("30000000");
        BigInteger gasLimit = new BigInteger("30000000");
        String command = args[0];
        Integer count = 0;
        Integer qps = 0;
        switch(command) {
            case "trans":
                count = Integer.parseInt(args[1]);
                qps = Integer.parseInt(args[2]);
                break;
            default:
                System.out.println("Args: <trans> <Total> <QPS> <GroupID> <ThreadPoolSize>");
        }
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(200);
        threadPool.setMaxPoolSize(500);
        threadPool.setQueueCapacity(count);
        threadPool.initialize();
        System.out.println("Deploying contract...");
        OkD ok = OkD.deploy(web3, credentials, gasPrice, gasLimit).send();
        PerformanceCollector collector = new PerformanceCollector();
        collector.setTotal(count);
        RateLimiter limiter = RateLimiter.create(qps);
        Integer area = count / 10;
        final Integer total = count;
        Random random = new Random(System.currentTimeMillis());
        System.out.println("Start test,total:" + count);
        for (Integer i = 0; i < count; ++i) {
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    limiter.acquire();
                    PerformanceOkCallback callback = new PerformanceOkCallback();
                    callback.setCollector(collector);
                    try {
                        TransactionReceipt receipt = ok.trans(String.valueOf(random.nextLong()), new BigInteger("1")).sendAsync().get();
                        callback.onResponse(receipt);
                    } catch (Exception e) {
                        TransactionReceipt receipt = new TransactionReceipt();
                        receipt.setStatus("-1");
                        callback.onResponse(receipt);
                        logger.error("Error sending:", e);
                    }
                    int current = sended.incrementAndGet();
                    if (current >= area && ((current % area) == 0)) {
                        System.out.println("Already sended: " + current + "/" + total + " transactions");
                    }
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(org.fisco.bcos.channel.client.Service) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) Random(java.util.Random) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Async(org.fisco.bcos.web3j.utils.Async) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Aggregations

RateLimiter (com.google.common.util.concurrent.RateLimiter)64 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 BigInteger (java.math.BigInteger)16 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)14 ParameterException (com.beust.jcommander.ParameterException)12 Test (org.junit.Test)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ApplicationContext (org.springframework.context.ApplicationContext)10 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)10 Random (java.util.Random)9 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 Service (org.fisco.bcos.channel.client.Service)9 Web3j (org.fisco.bcos.web3j.protocol.Web3j)9 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)9 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)9 Credentials (org.fisco.bcos.web3j.crypto.Credentials)8 ArrayList (java.util.ArrayList)7 ExecutorService (java.util.concurrent.ExecutorService)7 JCommander (com.beust.jcommander.JCommander)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6