Search in sources :

Example 71 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project web3sdk by FISCO-BCOS.

the class PerformanceDTTest method userTransferTest.

public void userTransferTest(BigInteger count, BigInteger qps, BigInteger deci, BigInteger queryAccountQPS) {
    List<String> signedTransactions = new ArrayList<String>();
    List<PerformanceDTCallback> callbacks = new ArrayList<PerformanceDTCallback>();
    try {
        parallelokAddr = dagUserMgr.getContractAddr();
        parallelok = ParallelOk.load(parallelokAddr, web3, credentials, new StaticGasProvider(new BigInteger("30000000"), new BigInteger("30000000")));
        /*
            System.out.println("Reading account state...");
            List<DagTransferUser> allUser = dagUserMgr.getUserList();
            for (int i = 0; i < allUser.size(); ++i) {
                BigInteger result = parallelok.balanceOf(allUser.get(i).getUser()).send();
                allUser.get(i).setAmount(result);
            }
            */
        List<DagTransferUser> allUser = dagUserMgr.getUserList();
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(200);
        threadPool.setMaxPoolSize(500);
        threadPool.setQueueCapacity(Math.max(count.intValue(), allUser.size()) + 1000);
        threadPool.initialize();
        Lock lock = new ReentrantLock();
        final ParallelOk _parallelok = parallelok;
        RateLimiter queryAccountLimiter = RateLimiter.create(queryAccountQPS.intValue());
        AtomicInteger geted = new AtomicInteger(0);
        for (int i = 0; i < allUser.size(); ++i) {
            final Integer _i = i;
            queryAccountLimiter.acquire();
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        BigInteger result = _parallelok.balanceOf(allUser.get(_i).getUser()).send();
                        allUser.get(_i).setAmount(result);
                        int all = geted.incrementAndGet();
                        if (all >= allUser.size()) {
                            System.out.println(dateFormat.format(new Date()) + " Query account finished");
                        }
                    } catch (Exception e) {
                        System.out.println(" Query failed, user is " + allUser.get(_i).getUser());
                        System.exit(0);
                    }
                }
            });
        }
        while (geted.get() < allUser.size()) {
            Thread.sleep(50);
        }
        latch = new CountDownLatch(count.intValue());
        AtomicLong signed = new AtomicLong(0);
        // create signed transactions
        System.out.println("Creating signed transactions...");
        for (int i = 0; i < count.intValue(); ++i) {
            final int index = i;
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    while (true) {
                        DagTransferUser from = dagUserMgr.getFrom(index);
                        DagTransferUser to = dagUserMgr.getTo(index);
                        if ((deci.intValue() > 0) && (deci.intValue() >= (index % 10 + 1))) {
                            to = dagUserMgr.getNext(index);
                        }
                        Random random = new Random();
                        int r = random.nextInt(100);
                        BigInteger amount = BigInteger.valueOf(r);
                        PerformanceDTCallback callback = new PerformanceDTCallback();
                        callback.setCallBackType("transfer");
                        callback.setCollector(collector);
                        callback.setDagUserMgr(getDagUserMgr());
                        callback.setFromUser(from);
                        callback.setToUser(to);
                        callback.setAmount(amount);
                        try {
                            callback.recordStartTime();
                            String signedTransaction = parallelok.transferSeq(from.getUser(), to.getUser(), amount);
                            lock.lock();
                            signedTransactions.add(signedTransaction);
                            callbacks.add(callback);
                            long totalSigned = signed.incrementAndGet();
                            if (totalSigned % (count.longValue() / 10) == 0) {
                                System.out.println("Signed transaction: " + String.valueOf(totalSigned * 100 / count.longValue()) + "%");
                            }
                            break;
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        } finally {
                            lock.unlock();
                        }
                    }
                    latch.countDown();
                }
            });
        }
        latch.await();
        latch = new CountDownLatch(count.intValue());
        long startTime = System.currentTimeMillis();
        collector.setStartTimestamp(startTime);
        AtomicInteger sent = new AtomicInteger(0);
        int division = count.intValue() / 10;
        RateLimiter limiter = RateLimiter.create(qps.intValue());
        System.out.println("Sending signed transactions...");
        for (int i = 0; i < count.intValue(); ++i) {
            limiter.acquire();
            final int index = i;
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    while (true) {
                        try {
                            callbacks.get(index).recordStartTime();
                            transactionManager.sendTransaction(signedTransactions.get(index), callbacks.get(index));
                            break;
                        } catch (Exception e) {
                            continue;
                        }
                    }
                    int current = sent.incrementAndGet();
                    if (current >= division && ((current % division) == 0)) {
                        long elapsed = System.currentTimeMillis() - startTime;
                        double sendSpeed = current / ((double) elapsed / 1000);
                        System.out.println("Already sent: " + current + "/" + count + " transactions" + ",QPS=" + sendSpeed);
                    }
                    latch.countDown();
                }
            });
        }
        latch.await();
        while (!collector.isEnd()) {
            Thread.sleep(3000);
            logger.info(" received: {}, total: {}", collector.getReceived().intValue(), collector.getTotal());
        }
        veryTransferData(threadPool, queryAccountQPS);
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) ArrayList(java.util.ArrayList) Random(java.util.Random) ReentrantLock(java.util.concurrent.locks.ReentrantLock) CountDownLatch(java.util.concurrent.CountDownLatch) RateLimiter(com.google.common.util.concurrent.RateLimiter) Date(java.util.Date) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 72 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project web3sdk by FISCO-BCOS.

the class PerformanceDTTest method userTransferSignTxPerfTest.

/**
 * Stress tests that create tx and sign them
 *
 * @param totalSignedTxCount
 * @param threadC
 * @throws InterruptedException
 */
public void userTransferSignTxPerfTest(BigInteger totalSignedTxCount, int threadC) throws InterruptedException {
    ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
    threadPool.setCorePoolSize(threadC > 0 ? threadC : 10);
    threadPool.setMaxPoolSize(threadC > 0 ? threadC : 10);
    threadPool.setQueueCapacity(threadC > 0 ? threadC : 10);
    threadPool.initialize();
    Credentials credentials = GenCredential.create();
    TransferSignTransactionManager extendedRawTransactionManager = new TransferSignTransactionManager(null, credentials, BigInteger.ONE, BigInteger.ONE);
    dagTransfer = DagTransfer.load(dagTransferAddr, null, extendedRawTransactionManager, new StaticGasProvider(new BigInteger("30000000"), new BigInteger("30000000")));
    AtomicLong signed = new AtomicLong(0);
    long startTime = System.currentTimeMillis();
    System.out.println(" => " + dateFormat.format(new Date()));
    for (int i = 0; i < threadC; i++) {
        threadPool.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    long index = signed.incrementAndGet();
                    if (index > totalSignedTxCount.intValue()) {
                        break;
                    }
                    DagTransferUser from = dagUserMgr.getFrom((int) index);
                    DagTransferUser to = dagUserMgr.getTo((int) index);
                    Random random = new Random();
                    int r = random.nextInt(100) + 1;
                    BigInteger amount = BigInteger.valueOf(r);
                    try {
                        String signedTransaction = dagTransfer.userTransferSeq(from.getUser(), to.getUser(), amount);
                        if (index % (totalSignedTxCount.longValue() / 10) == 0) {
                            System.out.println("Signed transaction: " + String.valueOf(index * 100 / totalSignedTxCount.longValue()) + "%");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(-1);
                    }
                }
            }
        });
    }
    while (signed.get() < totalSignedTxCount.intValue()) {
        Thread.sleep(10);
    }
    long endTime = System.currentTimeMillis();
    double elapsed = (endTime - startTime) / 1000.0;
    System.out.println(" => " + dateFormat.format(new Date()));
    System.out.print(" sign transactions finished, elapse time: " + elapsed + ", tx count = " + totalSignedTxCount + " ,sps = " + (totalSignedTxCount.intValue() / elapsed));
    System.exit(0);
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) Date(java.util.Date) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Example 73 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project paascloud-master by paascloud.

the class AsyncTaskExecutorConfiguration method getAsyncExecutor.

@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(paascloudProperties.getTask().getCorePoolSize());
    executor.setMaxPoolSize(paascloudProperties.getTask().getMaxPoolSize());
    executor.setQueueCapacity(paascloudProperties.getTask().getQueueCapacity());
    executor.setKeepAliveSeconds(paascloudProperties.getTask().getKeepAliveSeconds());
    executor.setThreadNamePrefix(paascloudProperties.getTask().getThreadNamePrefix());
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Bean(org.springframework.context.annotation.Bean)

Example 74 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project benchmark by seelunzi.

the class AsyncTaskConfig method getAsyncExecutor.

@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.setQueueCapacity(25);
    taskExecutor.initialize();
    return taskExecutor;
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 75 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project spring-cloud-gcp by spring-cloud.

the class GcpPubSubAutoConfiguration method pubSubAcknowledgementExecutor.

@Bean
@ConditionalOnMissingBean(name = "pubSubAcknowledgementExecutor")
public Executor pubSubAcknowledgementExecutor() {
    ThreadPoolTaskExecutor ackExecutor = new ThreadPoolTaskExecutor();
    ackExecutor.setMaxPoolSize(this.gcpPubSubProperties.getSubscriber().getMaxAcknowledgementThreads());
    ackExecutor.setThreadNamePrefix("gcp-pubsub-ack-executor");
    ackExecutor.setDaemon(true);
    return ackExecutor;
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)152 Bean (org.springframework.context.annotation.Bean)72 ExceptionHandlingAsyncTaskExecutor (io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor)19 Test (org.junit.jupiter.api.Test)19 BigInteger (java.math.BigInteger)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 RateLimiter (com.google.common.util.concurrent.RateLimiter)14 Test (org.junit.Test)14 Random (java.util.Random)12 ApplicationContext (org.springframework.context.ApplicationContext)12 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 Service (org.fisco.bcos.channel.client.Service)9 Credentials (org.fisco.bcos.web3j.crypto.Credentials)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 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)6 MDCCleanerTaskDecorator (com.sequenceiq.cloudbreak.concurrent.MDCCleanerTaskDecorator)4