Package fr.jayasoft.util

Source Code of fr.jayasoft.util.ChecksumBenchmark$BenchResult

/*
* This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
* Copyright Jayasoft 2005 - All rights reserved
*
* #SNAPSHOT#
*/
package fr.jayasoft.util;

import java.io.File;
import java.io.PrintStream;

import fr.jayasoft.commons.security.ChecksumProvider;
import fr.jayasoft.commons.security.FastMD5ChecksumProvider;
import fr.jayasoft.commons.security.MessageDigestChecksumProvider;

public class ChecksumBenchmark {
    public static void main(String[] args) {
        File testDir = new File("test/files");
        String[] algos = new String[] {"fastmd5", "MD5", "SHA-1"};
       
        for (int i = 0; i < algos.length; i++) {
            ChecksumProvider p = getChecksumProvider(algos[i]);
            File[] files = testDir.listFiles();
            BenchResult benchResult = new BenchResult(algos[i]);
            for (int j = 0; j < files.length; j++) {
                for (int k=0; k<10; k++) {
                    if (!files[j].isDirectory()) {
                        bench(benchResult, p, files[j]);
                    }
                }
            }
            benchResult.output(System.out);
        }
    }

    private static void bench(BenchResult benchResult, ChecksumProvider p, File file) {
        benchResult.startTest(file);
        p.compute(file);       
        benchResult.endTest();
    }

    private static ChecksumProvider getChecksumProvider(String algo) {
        if ("fastmd5".equals(algo)) {
            return FastMD5ChecksumProvider.getInstance();
        } else {
            return MessageDigestChecksumProvider.getNewProvider(algo);
        }
    }
   
    public static class BenchResult {

        private String _algo;
        private long _totalDuration = 0;
        private long _totalBytes = 0;
        private int _nb = 0;
        private long _minDuration = Long.MAX_VALUE;
        private long _maxDuration = 0;
       
        private long _start;
        private File _currentFile;

        public BenchResult(String algo) {
            _algo = algo;
        }

        public void output(PrintStream out) {
            out.println(_algo+": total bytes="+_totalBytes+" duration: total="+_totalDuration+" min="+_minDuration+" max="+_maxDuration);
        }

        public void startTest(File file) {
            _start = System.currentTimeMillis();
            _currentFile = file;
        }

        public void endTest() {
            long duration = System.currentTimeMillis() - _start;
            _totalBytes += _currentFile.length();
            _totalDuration += duration;
            _nb++;
            _minDuration = Math.min(duration, _minDuration);
            _maxDuration = Math.max(duration, _maxDuration);
        }

    }

}
TOP

Related Classes of fr.jayasoft.util.ChecksumBenchmark$BenchResult

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.