Search in sources :

Example 1 with ChannelRequest

use of org.bcos.channel.dto.ChannelRequest in project web3sdk by FISCO-BCOS.

the class Channel2Client method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("参数: 目标topic         总请求量");
        return;
    }
    String topic = args[0];
    Integer count = Integer.parseInt(args[1]);
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    logger.debug("初始化链上链下客户端");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    // 阻塞到连接成功,或通知
    service.run();
    System.out.println("3s后开始测试...");
    Thread.sleep(1000);
    System.out.println("2s后开始测试...");
    Thread.sleep(1000);
    System.out.println("1s后开始测试...");
    Thread.sleep(1000);
    System.out.println("开始测试");
    System.out.println("===================================================================");
    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        ChannelRequest request = new ChannelRequest();
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);
        request.setContent("request seq:" + request.getMessageID());
        System.out.println(df.format(LocalDateTime.now()) + " 发送请求 seq:" + String.valueOf(request.getMessageID()) + ", 内容:" + request.getContent());
        ChannelResponse response = service.sendChannelMessage2(request);
        System.out.println(df.format(LocalDateTime.now()) + "收到回包 seq:" + String.valueOf(response.getMessageID()) + ", 错误码:" + response.getErrorCode() + ", 内容:" + response.getContent());
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ChannelRequest(org.bcos.channel.dto.ChannelRequest) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Service(org.bcos.channel.client.Service) ChannelResponse(org.bcos.channel.dto.ChannelResponse) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 2 with ChannelRequest

use of org.bcos.channel.dto.ChannelRequest in project web3sdk by FISCO-BCOS.

the class ChannelClient method main.

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("参数: 请求方         接收方         总请求量");
        return;
    }
    String from = args[0];
    String to = args[1];
    Integer count = Integer.parseInt(args[2]);
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    logger.debug("初始化链上链下客户端");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    // 阻塞到连接成功,或通知
    service.run();
    // service.setPushCallback(new PushCallback());
    System.out.println("3s后开始测试...");
    Thread.sleep(1000);
    System.out.println("2s后开始测试...");
    Thread.sleep(1000);
    System.out.println("1s后开始测试...");
    Thread.sleep(1000);
    System.out.println("开始测试");
    System.out.println("===================================================================");
    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        ChannelRequest request = new ChannelRequest();
        request.setFromOrg(from);
        request.setToOrg(to);
        request.setMessageID(service.newSeq());
        request.setContent("request seq:" + request.getMessageID());
        System.out.println(df.format(LocalDateTime.now()) + " 发送请求 seq:" + String.valueOf(request.getMessageID()) + ", 内容:" + request.getContent());
        ChannelResponse response = service.sendChannelMessage(request);
        System.out.println(df.format(LocalDateTime.now()) + "收到回包 seq:" + String.valueOf(response.getMessageID()) + ", 错误码:" + response.getErrorCode() + ", 内容:" + response.getContent());
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ChannelRequest(org.bcos.channel.dto.ChannelRequest) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Service(org.bcos.channel.client.Service) ChannelResponse(org.bcos.channel.dto.ChannelResponse) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 3 with ChannelRequest

use of org.bcos.channel.dto.ChannelRequest in project web3sdk by FISCO-BCOS.

the class LoadTestRunner method main.

public static void main(String[] args) throws Exception {
    logger.debug("初始化");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("3s后开始测试...");
    Thread.sleep(1000);
    System.out.println("2s后开始测试...");
    Thread.sleep(1000);
    System.out.println("1s后开始测试...");
    Thread.sleep(1000);
    System.out.println("开始测试");
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    channelEthereumService.setTimeout(10000);
    Web3j web3 = Web3j.build(channelEthereumService);
    Thread.sleep(2000);
    // 先部署一个测试合约
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    RawTransactionManager rawTransactionManager = new RawTransactionManager(web3, credentials);
    java.math.BigInteger gasPrice = new BigInteger("30000000");
    java.math.BigInteger gasLimit = new BigInteger("30000000");
    java.math.BigInteger initialWeiValue = new BigInteger("0");
    LoadTest loadTest = LoadTest.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("LoadTest getContractAddress " + loadTest.getContractAddress());
    // 发送交易
    for (Integer i = 0; i < 10; ++i) {
        Thread txThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        // TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
                        loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1));
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        txThread.start();
    }
    // 读取合约
    for (Integer i = 0; i < 10; ++i) {
        Thread callThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        Address origin = loadTest._origin().get();
                        Address sender = loadTest._sender().get();
                        logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        callThread.start();
    }
    // 200个线程,发链上链下消息
    for (Integer i = 0; i < 10; ++i) {
        Thread messageThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        ChannelRequest request = new ChannelRequest();
                        request.setToTopic("test");
                        request.setFromOrg("WB");
                        request.setToOrg("EB");
                        request.setMessageID(service.newSeq());
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < 500; ++i) {
                            sb.append("hello world!:");
                        }
                        request.setContent(sb.toString() + request.getMessageID());
                        request.setTimeout(2000);
                        // ChannelResponse response = service.sendChannelMessage2(request);
                        logger.info("发送链上链下消息:{}", request.getMessageID());
                        ChannelResponse response = service.sendChannelMessage(request);
                        logger.info("收到链上链下响应:{} {}", response.getMessageID(), response.getErrorCode());
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        messageThread.start();
    }
    for (int i = 0; i < 10; ++i) {
        TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
        Address origin = loadTest._origin().get();
        Address sender = loadTest._sender().get();
        List<Type> addresses = loadTest.constGetInf().get();
        logger.info("const sender:{} origin:{}", (((Address) addresses.get(0)).toString()), (((Address) addresses.get(1)).toString()));
        logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
    }
}
Also used : ChannelRequest(org.bcos.channel.dto.ChannelRequest) Address(org.bcos.web3j.abi.datatypes.Address) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) RawTransactionManager(org.bcos.web3j.tx.RawTransactionManager) ChannelResponse(org.bcos.channel.dto.ChannelResponse) BigInteger(java.math.BigInteger) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Int256(org.bcos.web3j.abi.datatypes.generated.Int256) Web3j(org.bcos.web3j.protocol.Web3j) Type(org.bcos.web3j.abi.datatypes.Type) BigInteger(java.math.BigInteger) Credentials(org.bcos.web3j.crypto.Credentials)

Example 4 with ChannelRequest

use of org.bcos.channel.dto.ChannelRequest in project web3sdk by FISCO-BCOS.

the class Perfomance method main.

public static void main(String[] args) throws Exception {
    if (args.length < 6) {
        System.out.println("参数: 请求方         接收方         总请求量          发送TPS     包大小           超时时间");
        return;
    }
    String from = args[0];
    String to = args[1];
    Integer count = Integer.parseInt(args[2]);
    Integer tps = Integer.parseInt(args[3]);
    Integer packageSize = Integer.parseInt(args[4]);
    Integer timeout = Integer.parseInt(args[5]);
    logger.debug("初始化");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.setPushCallback(new PushCallback());
    service.run();
    System.out.println("3s后开始测试...");
    Thread.sleep(1000);
    System.out.println("2s后开始测试...");
    Thread.sleep(1000);
    System.out.println("1s后开始测试...");
    Thread.sleep(1000);
    System.out.println("开始测试");
    System.out.println("===================================================================");
    ChannelRequest request = new ChannelRequest();
    request.setAppName("");
    request.setBankNO("");
    request.setFromOrg(from);
    request.setOrgApp("");
    request.setTimeout(0);
    request.setToOrg(to);
    request.setTimeout(timeout);
    request.setVersion("");
    String message = "";
    for (Integer i = 0; i < packageSize; ++i) {
        message += "z";
    }
    Map<Integer, RequestTimer> resultMap = new ConcurrentHashMap<Integer, RequestTimer>();
    PerfomanceCollector collector = new PerfomanceCollector();
    collector.total = count;
    collector.resultMap = resultMap;
    collector.startTimestamp = System.currentTimeMillis();
    collector.tps = tps;
    collector.packageSize = packageSize;
    RateLimiter limiter = RateLimiter.create((double) tps);
    for (Integer seq = 0; seq < count; ++seq) {
        limiter.acquire();
        if ((seq + 1) % (count / 10) == 0) {
            System.out.println("已发送:" + String.valueOf((seq + 1) * 100 / count) + "%");
        }
        request.setContent(message);
        request.setMessageID(service.newSeq());
        RequestTimer timer = new RequestTimer();
        timer.sendTimestamp = System.currentTimeMillis();
        resultMap.put(seq, timer);
        PerfomanceCallback callback = new PerfomanceCallback();
        callback.collector = collector;
        service.asyncSendChannelMessage(request, callback);
    }
    System.out.println("共发送:" + String.valueOf(count) + "条");
}
Also used : ChannelRequest(org.bcos.channel.dto.ChannelRequest) Service(org.bcos.channel.client.Service) RateLimiter(com.google.common.util.concurrent.RateLimiter) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Service (org.bcos.channel.client.Service)4 ChannelRequest (org.bcos.channel.dto.ChannelRequest)4 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ChannelResponse (org.bcos.channel.dto.ChannelResponse)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 BigInteger (java.math.BigInteger)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Address (org.bcos.web3j.abi.datatypes.Address)1 Type (org.bcos.web3j.abi.datatypes.Type)1 Utf8String (org.bcos.web3j.abi.datatypes.Utf8String)1 Int256 (org.bcos.web3j.abi.datatypes.generated.Int256)1 Credentials (org.bcos.web3j.crypto.Credentials)1 ECKeyPair (org.bcos.web3j.crypto.ECKeyPair)1 Web3j (org.bcos.web3j.protocol.Web3j)1 ChannelEthereumService (org.bcos.web3j.protocol.channel.ChannelEthereumService)1 TransactionReceipt (org.bcos.web3j.protocol.core.methods.response.TransactionReceipt)1 RawTransactionManager (org.bcos.web3j.tx.RawTransactionManager)1