ftp Performance Analysis

Video uploaded recently encountered very slow situation, 20M takes about a minute to upload to play, but by ftp test, long found around 4s, Ali cloud 2M bandwidth, the following tests are based on Ali cloud 2M bandwidth to do a the results of the test.

1. Without Buffer

Under 2M bandwidth, 20M video upload length of about 60s, several tests, averaged

 /**
     * 上传文件(可供Action/Controller层使用)
     * 
     * @param hostname
     *            FTP服务器地址
     * @param port
     *            FTP服务器端口号
     * @param username
     *            FTP登录帐号
     * @param password
     *            FTP登录密码
     * @param pathname
     *            FTP服务器保存目录
     * @param fileName
     *            上传到FTP服务器后的文件名称
     * @param inputStream
     *            输入文件流
     * @return
     */
    public static boolean uploadFile(String hostname, int port, String username, String password, String pathname,
            String fileName, InputStream inputStream) {
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        try {
            // 连接FTP服务器
            ftpClient.connect(hostname, port);
            // 登录FTP服务器
            ftpClient.login(username, password);
            // 是否成功登录FTP服务器
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return flag;
            }
            //指定上传文件的类型  二进制文件
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //检查上传路径是否存在 如果不存在返回false
            boolean pathFlagExist = createDir(ftpClient, pathname);
            if(!pathFlagExist){
                return false;
            }
            //这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在linux上或者其他服务器上面,由于安全限制,可能某些端口没有开启,所以就出现阻塞。OK,问题解决。
            ftpClient.enterLocalPassiveMode();
            //设置上传目录  
            ftpClient.changeWorkingDirectory(pathname);
       
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            logger.info("上传失败"+e);
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    logger.info("上传文件-->关闭连接失败"+e);
                }
            }
        }
        return flag;
    }

2. Increase the cache code

Under 2M bandwidth, 20M video upload length of about 11S, several tests, averaged

 /**
     * 上传文件(可供Action/Controller层使用)
     * 
     * @param hostname
     *            FTP服务器地址
     * @param port
     *            FTP服务器端口号
     * @param username
     *            FTP登录帐号
     * @param password
     *            FTP登录密码
     * @param pathname
     *            FTP服务器保存目录
     * @param fileName
     *            上传到FTP服务器后的文件名称
     * @param inputStream
     *            输入文件流
     * @return
     */
    public static boolean uploadFile(String hostname, int port, String username, String password, String pathname,
            String fileName, InputStream inputStream) {
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        try {
            // 连接FTP服务器
            ftpClient.connect(hostname, port);
            // 登录FTP服务器
            ftpClient.login(username, password);
            // 是否成功登录FTP服务器
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return flag;
            }
            //指定上传文件的类型  二进制文件
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //检查上传路径是否存在 如果不存在返回false
            boolean pathFlagExist = createDir(ftpClient, pathname);
            if(!pathFlagExist){
                return false;
            }
            //这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在linux上或者其他服务器上面,由于安全限制,可能某些端口没有开启,所以就出现阻塞。OK,问题解决。
            ftpClient.enterLocalPassiveMode();
            //设置上传目录  
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.setBufferSize(1024*1024);
            BufferedInputStream input = new BufferedInputStream(inputStream);
            ftpClient.storeFile(fileName, input);
            inputStream.close();
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            logger.info("上传失败"+e);
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    logger.info("上传文件-->关闭连接失败"+e);
                }
            }
        }
        return flag;
    }

Actually increased two sentences

Add buffer manner, according to the size of the uploaded file, set the buffer size, the upload process, the buffer will be filled with reading and writing disks

ftpClient.setBufferSize(1024*1024);
BufferedInputStream input = new BufferedInputStream(inputStream);

3. Conclusion

  By setting the buffer, to enhance the performance of similar size and six times faster, if there is a good optimization methods can leave a message at the bottom

如果你热衷技术,喜欢交流,欢迎加入我们! 

Guess you like

Origin blog.csdn.net/qq_16855077/article/details/91811658