All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rabbitmq.perf.PerfTestMulti Maven / Gradle / Ivy

There is a newer version: 2.22.1
Show newest version
// Copyright (c) 2007-Present Pivotal Software, Inc.  All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2.  For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// [email protected].

package com.rabbitmq.perf;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.perf.Scenario;
import com.rabbitmq.perf.ScenarioFactory;
import com.rabbitmq.tools.json.JSONReader;
import com.rabbitmq.tools.json.JSONWriter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PerfTestMulti {
    private static final ConnectionFactory factory = new ConnectionFactory();

    private static final Map results = new HashMap();

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.out.println("Usage: PerfTestMulti input-json-file output-json-file");
            System.exit(1);
        }
        Log.configureLog();
        String inJSON = args[0];
        String outJSON = args[1];
        List scenariosJSON = null;
        try {
            scenariosJSON = (List) new JSONReader().read(readFile(inJSON));
        } catch (FileNotFoundException e) {
            System.out.println("Input json file " + inJSON + " could not be found");
            System.exit(1);
        }
        if (scenariosJSON == null) {
            System.out.println("Input json file " + inJSON + " could not be parsed");
            System.exit(1);
        }
        Scenario[] scenarios = new Scenario[scenariosJSON.size()];
        for (int i = 0; i < scenariosJSON.size(); i++) {
            scenarios[i] = ScenarioFactory.fromJSON(scenariosJSON.get(i), factory);
        }
        runStaticBrokerTests(scenarios);
        writeJSON(outJSON);
    }

    private static String readFile(String path) throws IOException {
        final char[] buf = new char[4096];
        StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(new FileInputStream(path), "UTF-8");
        try {
            int chars;
            while ((chars = in.read(buf, 0, buf.length)) > 0) {
                out.append(buf, 0, chars);
            }
        } finally {
            in.close();
        }
        return out.toString();
    }

    private static void writeJSON(String outJSON) throws IOException {
        try (FileWriter outFile = new FileWriter(outJSON);
             PrintWriter out = new PrintWriter(outFile)) {
            out.println(new JSONWriter(true).write(results));
        }
    }

    private static void runStaticBrokerTests(Scenario[] scenarios) throws Exception {
        runTests(scenarios);
    }

    private static void runTests(Scenario[] scenarios) throws Exception {
        for (Scenario scenario : scenarios) {
            System.out.print("Running scenario '" + scenario.getName() + "' ");
            scenario.run();
            System.out.println();
            results.put(scenario.getName(), scenario.getStats().results());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy