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

org.jgroups.tests.TestToaOrder Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote Jakarta Enterprise Beans and Jakarta Messaging, including all dependencies. It is intended for use by those not using maven, maven users should just import the Jakarta Enterprise Beans and Jakarta Messaging BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 35.0.0.Beta1
Show newest version
package org.jgroups.tests;

import org.jgroups.*;
import org.jgroups.AnycastAddress;
import org.jgroups.util.Util;

import javax.management.*;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.*;

/**
 * Runs the Total Order Anycast protocol and saves the messages delivered
 * 
 * Note: this is used for debugging
 * Note2: this needs to be clean :)
 *
 * @author Pedro Ruivo
 * @since 3.1
 */
public class TestToaOrder {
    private static final String PROPS = "toa.xml";
    private static final String CLUSTER = "test-toa-cluster";
    private static final String OUTPUT_FILE_SUFFIX = "-messages.txt";
    private static final String JMX_DOMAIN = "org.jgroups";

    private JChannel jChannel;
    private MyReceiver receiver;
    private int numberOfNodes;
    private int numberOfMessages;
    private final List
members = new LinkedList<>(); private long start; private long stop; private long sentBytes = 0; private long sentMessages = 0; private String config; public static void main(String[] args) throws InterruptedException { System.out.println("=============="); System.out.println("Test TOA Order"); System.out.println("=============="); ArgumentsParser argumentsParser = new ArgumentsParser(args); if (argumentsParser.isHelp()) { helpAndExit(); } else if(argumentsParser.isTestOrder()) { /*String[] paths = argumentsParser.getFilesPath(); int numberOfFiles = paths.length; ProcessFile[] threads = new ProcessFile[numberOfFiles]; for (int i = 0; i < threads.length; ++i) { threads[i] = new ProcessFile(paths[i]); threads[i].start(); } Map allMessages = new HashMap(); for (ProcessFile processFile : threads) { processFile.join(); for (MessageInfo messageInfo : processFile.list) { String message = messageInfo.message; if (!allMessages.containsKey(message)) { allMessages.put(message, messageInfo); } else { allMessages.get(message).join(messageInfo); } } } for (MessageInfo messageInfo : allMessages.values()) { messageInfo.check(); } System.out.println("============= FINISHED ============="); System.exit(0);*/ } TestToaOrder test = new TestToaOrder( argumentsParser.getNumberOfNodes(), argumentsParser.getNumberOfMessages(), argumentsParser.getConfig()); try { test.startTest(); } catch (Exception e) { System.err.println("Error while executing the test: " + e.getMessage()); e.printStackTrace(); System.exit(1); } finally { test.closeJChannel(); System.out.println("============= FINISHED ============="); } System.exit(0); } private static void helpAndExit() { System.out.println("usage: " + TestToaOrder.class.getCanonicalName() + "
members) { int rand = members.indexOf(jChannel.getAddress()); AnycastAddress address = new AnycastAddress(); address.add(members.get(rand++ % members.size()), members.get(rand++ % members.size()), members.get(rand % members.size())); return address; } private void sendMessages() throws Exception { System.out.println("Start sending messages..."); String address = jChannel.getAddressAsString(); List
mbrs = jChannel.getView().getMembers(); start = System.nanoTime(); for (int i = 0; i < numberOfMessages; ++i) { AnycastAddress dst = getDestinations(mbrs); Message message = new Message(); message.setDest(dst); DataMessage dataMessage = new DataMessage(); dataMessage.type = DataMessage.DATA; dataMessage.data = address + ":" + i; message.setObject(dataMessage); jChannel.send(message); sentBytes += (dataMessage.data.getBytes().length + 1); sentMessages++; } stop = System.nanoTime(); System.out.println("Finish sending messages..."); } private void awaitUntilAllMessagesAreReceived() throws InterruptedException { int expectedMessages = 3 * numberOfMessages; receiver.await(expectedMessages); } private void awaitUntilAllFinishes() throws Exception { DataMessage dataMessage = new DataMessage(); dataMessage.type = DataMessage.FINISH; dataMessage.data = null; jChannel.send(null, dataMessage); synchronized (members) { if (!members.isEmpty()) { members.wait(); } } } public void printSenderInfo() { System.out.println("+++ Sender Information +++"); double duration = stop - start; duration /= 1000000.0; //nano to millis System.out.println("+ Duration (msec) = " + duration); System.out.println("+ Sent Bytes = " + sentBytes); System.out.println("+ Sent Messages = " + sentMessages); duration /= 1000.0; //millis to sec System.out.println("-------------------"); System.out.println("+ Sent Throughput (bytes/sec) = " + (sentBytes / duration)); System.out.println("+ Sent Messages (messages/sec) = " + (sentMessages / duration)); System.out.println("--------------------------------"); } public void memberFinished(Address addr) { synchronized (members) { members.remove(addr); if (members.isEmpty()) { members.notify(); } } } public void closeJChannel() { System.out.println("Close channel"); jChannel.close(); } public void startTest() throws Exception { System.out.println("Start testing..."); createJChannel(); sendMessages(); awaitUntilAllMessagesAreReceived(); String filePath = jChannel.getAddressAsString() + OUTPUT_FILE_SUFFIX; System.out.println("Writing messages in " + filePath); FileWriter fileWriter = new FileWriter(filePath); for (String s : receiver.getMessageList()) { fileWriter.write(s); fileWriter.write("\n"); } fileWriter.flush(); fileWriter.close(); System.out.println("All done!"); awaitUntilAllFinishes(); printSenderInfo(); receiver.printReceiverInfo(); printJMXStats(); } private static void printJMXStats() { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName groupMulticast = getGroupMulticastObjectName(mBeanServer); if (groupMulticast == null) { System.err.println("Unable to find the GROUP_MULTICAST protocol"); return ; } try { System.out.println("======== JMX STATS ========="); for (MBeanAttributeInfo mBeanAttributeInfo : mBeanServer.getMBeanInfo(groupMulticast).getAttributes()) { String attribute = mBeanAttributeInfo.getName(); String type = mBeanAttributeInfo.getType(); if (!type.equals("double") && !type.equals("int")) { continue; } System.out.println(attribute + "=" + mBeanServer.getAttribute(groupMulticast, attribute)); } System.out.println("======== JMX STATS ========="); } catch (Exception e) { System.err.println("Error collecting stats" + e.getLocalizedMessage()); } } private static ObjectName getGroupMulticastObjectName(MBeanServer mBeanServer) { for(ObjectName name : mBeanServer.queryNames(null, null)) { if(name.getDomain().equals(JMX_DOMAIN)) { if ("TOA".equals(name.getKeyProperty("protocol"))) { return name; } } } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy