Search in sources :

Example 1 with ScenarioUnit

use of com.nhnent.eat.entity.ScenarioUnit in project eat by nhnent.

the class ScenarioLoader method loadScenario.

/**
 * Load Scenario from scenario file
 * Create ScenarioUnit list
 *
 * @param scenarioFileName Scenario file name
 * @param userId           User ID
 * @return ScenarioUnit list
 */
public final List<ScenarioUnit> loadScenario(final String scenarioFileName, final String userId) throws SuspendExecution {
    if (!userId.equals(EmptyString)) {
        TesterActor.globalVariable.get().put("userId", userId);
    }
    // Do precompile for 1.Remove comments 2.Apply Global variables 3.Apply Random variables
    String compiledScenarioFile = preCompile(scenarioFileName, userId);
    List<ScenarioUnit> listScenarioUnit = new ArrayList();
    try {
        BufferedReader br = new BufferedReader(new FileReader(compiledScenarioFile));
        StringBuilder entireScenarioString = new StringBuilder(EmptyString);
        String line;
        // Read a scenario file as a String
        while ((line = br.readLine()) != null) {
            entireScenarioString.append(line);
        }
        entireScenarioString = new StringBuilder(entireScenarioString.toString().trim());
        // Deck Setting problem..
        entireScenarioString = new StringBuilder(entireScenarioString.toString().replaceAll("\\s+", EmptyString));
        // Split the entire scenario String by '#'
        String[] syllableUnits = entireScenarioString.toString().split("[#]");
        List<String> finalSyllableUnits = new LinkedList<>();
        for (String syllableUnit : syllableUnits) {
            if (syllableUnit.equals(EmptyString))
                continue;
            finalSyllableUnits.add(syllableUnit);
        }
        for (String syllable : finalSyllableUnits) {
            String header;
            String jsonBody;
            ScenarioUnit scenarioUnit;
            try {
                if (syllable.contains("{")) {
                    int headerEndIndex = syllable.indexOf('{');
                    header = syllable.substring(0, headerEndIndex);
                    jsonBody = syllable.substring(headerEndIndex);
                } else {
                    header = syllable;
                    jsonBody = EmptyString;
                }
                // If the syllable starts with "include", recursive the loadScenario function.
                if (header.startsWith(IncludeDelimiter)) {
                    String scenarioPath = Config.obj().getScenario().getScenarioPath();
                    String includeFileName = scenarioPath + "/" + header.replace(" ", "").replace(IncludeDelimiter + "=", "");
                    listScenarioUnit.addAll(loadScenario(includeFileName, userId));
                } else {
                    // Create a ScenarioUnit by passing ScenarioUnit parser.
                    scenarioUnit = scenarioUnitParser.parse(header, jsonBody);
                    listScenarioUnit.add(scenarioUnit);
                }
            } catch (Exception e) {
                logger.error("Failed to load scenario [{}]: \n{}", scenarioFileName, syllable);
            }
        }
        br.close();
        // Delete temporary compiled file
        File f = new File(compiledScenarioFile);
        if (f.exists() && !f.isDirectory()) {
            if (f.delete()) {
                logger.debug("All of compiled scenario files are deleted.");
            } else {
                logger.debug("Failed to deletion of compiled scenario files.");
            }
        }
        return listScenarioUnit;
    } catch (Exception e) {
        logger.error("Exception raised in scenario[{}]: {}", scenarioFileName, ExceptionUtils.getStackTrace(e));
    }
    return null;
}
Also used : ScenarioUnit(com.nhnent.eat.entity.ScenarioUnit) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 2 with ScenarioUnit

use of com.nhnent.eat.entity.ScenarioUnit in project eat by nhnent.

the class ScenarioUnitParser method parse.

public ScenarioUnit parse(String packetHeader, String json) {
    ScenarioUnit newScenarioUnit = new ScenarioUnit();
    String pckJson = json;
    if (packetHeader.startsWith(RequestRESTString)) {
        newScenarioUnit.type = ScenarioUnitType.RequestREST;
    } else if (packetHeader.startsWith(ResponseRESTString)) {
        newScenarioUnit.type = ScenarioUnitType.ResponseREST;
    } else if (packetHeader.startsWith(RequestJMXString)) {
        String[] scenarioDefine;
        scenarioDefine = packetHeader.split(PckNameDelimiter);
        String mBeanName = scenarioDefine[1];
        String functionName = scenarioDefine[2];
        newScenarioUnit.name = mBeanName;
        newScenarioUnit.dest = functionName;
        newScenarioUnit.type = ScenarioUnitType.SetJMXQaCommand;
    } else if (packetHeader.startsWith(BeginQaCommandDelimiter)) {
        newScenarioUnit.type = ScenarioUnitType.SetQaCommand;
    } else if (packetHeader.startsWith(ResponseJMXString)) {
        newScenarioUnit.type = ScenarioUnitType.GetJMXQaCommand;
    } else if (packetHeader.startsWith(BeginSetCardDeckDelimiter)) {
        newScenarioUnit.type = ScenarioUnitType.SetCardDeck;
    } else if (packetHeader.startsWith(LoopStartDelimiter)) {
        int loopCount = Integer.parseInt(packetHeader.replace(LoopStartDelimiter, EmptyString).trim());
        newScenarioUnit.loopType = LoopType.LoopStart;
        newScenarioUnit.loopCount = loopCount;
        newScenarioUnit.loopDepth = loopDepth;
        loopDepth++;
    } else if (packetHeader.startsWith(LoopEndDelimiter)) {
        loopDepth--;
        newScenarioUnit.loopType = LoopType.LoopEnd;
        newScenarioUnit.loopDepth = loopDepth;
    } else if (packetHeader.startsWith(SleepDelimiter)) {
        int sleepPeriod = Integer.parseInt(packetHeader.replace(SleepDelimiter, EmptyString).trim());
        newScenarioUnit.type = ScenarioUnitType.Sleep;
        newScenarioUnit.sleepPeriod = sleepPeriod;
    } else if (packetHeader.startsWith(PrintDelimiter)) {
        String printString = packetHeader.replace(PrintDelimiter, EmptyString).trim();
        newScenarioUnit.type = ScenarioUnitType.Print;
        newScenarioUnit.reservedField = printString;
    } else if (packetHeader.startsWith(ExtraFunction)) {
        newScenarioUnit.type = ScenarioUnitType.ExtraFunctionCall;
        extractExtraFunctionElement(packetHeader, newScenarioUnit);
    } else if (packetHeader.startsWith(DisconnectionDelimiter)) {
        newScenarioUnit.type = ScenarioUnitType.Disconnect;
    } else if (packetHeader.startsWith(ConnectionDelimiter)) {
        newScenarioUnit.type = ScenarioUnitType.Connect;
    } else {
        final int indexOfPckName = 1;
        final int indexOfPckSubID = 2;
        final int lengthOfContainPckName = 2;
        final int lengthOfContainPckSubID = 3;
        String pckType;
        String pckName = EmptyString;
        String subId = EmptyString;
        String[] scenarioDefine = packetHeader.split(PckNameDelimiter);
        pckType = scenarioDefine[0].replace(PckDefDelimiter, EmptyString);
        if (pckType.equals("END")) {
            return null;
        }
        if (scenarioDefine.length >= lengthOfContainPckName) {
            pckName = scenarioDefine[indexOfPckName];
        }
        if (scenarioDefine.length == lengthOfContainPckSubID) {
            subId = scenarioDefine[indexOfPckSubID];
        }
        newScenarioUnit.packageName = pckName.split("]")[0].replace("[", "");
        newScenarioUnit.name = pckName.split("]")[1];
        newScenarioUnit.subId = subId;
        switch(pckType) {
            case RequestPacketString:
                newScenarioUnit.type = ScenarioUnitType.Request;
                break;
            case SendPacketString:
                newScenarioUnit.type = ScenarioUnitType.Send;
                break;
            case ResponsePacketString:
                newScenarioUnit.type = ScenarioUnitType.Response;
                break;
            case ReceivePacketString:
                newScenarioUnit.type = ScenarioUnitType.Receive;
                break;
            case ReceiveUntilPacketString:
                newScenarioUnit.type = ScenarioUnitType.ReceiveUntil;
                break;
            default:
                newScenarioUnit.type = ScenarioUnitType.None;
        }
        // If the current packet is a Protocol Buffer, have to remove "_" for real variable name.
        pckJson = convertProtoVarNameToRealVarName(pckJson);
    }
    // Set communicationMethod
    switch(newScenarioUnit.type) {
        case ScenarioUnitType.Request:
        case ScenarioUnitType.RequestREST:
        case ScenarioUnitType.SetJMXQaCommand:
        case ScenarioUnitType.RequestRestCall:
        case ScenarioUnitType.Send:
            newScenarioUnit.communicationMethod = CommunicationMethod.Request;
            break;
        case ScenarioUnitType.Response:
        case ScenarioUnitType.ResponseREST:
        case ScenarioUnitType.GetJMXQaCommand:
        case ScenarioUnitType.Receive:
            newScenarioUnit.communicationMethod = CommunicationMethod.Response;
            break;
        default:
            newScenarioUnit.communicationMethod = CommunicationMethod.None;
            break;
    }
    newScenarioUnit.json = pckJson;
    return newScenarioUnit;
}
Also used : ScenarioUnit(com.nhnent.eat.entity.ScenarioUnit)

Example 3 with ScenarioUnit

use of com.nhnent.eat.entity.ScenarioUnit in project eat by nhnent.

the class TesterActor method doRun.

/**
 * doRun of Actor. It performs requests on Actor Mailbox.
 */
@Override
protected Void doRun() throws InterruptedException, SuspendExecution {
    TesterActor.globalVariable.set(new HashMap<>());
    List<ScenarioUnit> listScenarioPacket = new ArrayList<>();
    while (true) {
        Object o = receive();
        logger.debug("Received testing execute (" + this.ref().toString() + ")");
        if (o instanceof Messages) {
            Messages m = (Messages) o;
            // Handle execute of test preparation
            if (m.type == MessageType.PrepareTest) {
                userInfo.set(m.userId);
                actorIndex = m.actorIndex;
                String playerId = String.format("Player_%06d", actorIndex + 1);
                MDC.put("playerId", playerId);
                MDC.put("strand", Strand.currentStrand().getName());
                String logfileName;
                if (Config.obj().getCommon().isLoggingOnSameFile()) {
                    logfileName = String.format("%s", playerId);
                } else {
                    Date now = new Date();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
                    String format = formatter.format(now);
                    logfileName = String.format("%s_%s", format, playerId);
                }
                MDC.put("logfileName", logfileName);
                logger.debug("Load Scenario [" + m.scenarioFile + "]");
                listScenarioPacket.addAll(loadScenario(m.scenarioFile, m.userId));
                Messages finishMessage = new Messages(this.ref, MessageType.TestPrepared);
                m.sender.send(finishMessage);
            }
            // Handle execute of test start
            if (m.type == MessageType.TestStart) {
                ScenarioExecutor scenarioExecutor = new ScenarioExecutor(userId);
                StreamPacket.obj().initialize(userId);
                scenarioExecutor.addCommunication(new RestCommunication());
                scenarioExecutor.addCommunication(new JmxCommunication());
                scenarioExecutor.addCommunication(new NettyCommunication(userId, actorIndex));
                ScenarioExecutionResult result = scenarioExecutor.runScenario(listScenarioPacket);
                listScenarioPacket.clear();
                Messages finishMessage = new Messages(this.ref, MessageType.TestFinished);
                finishMessage.userId = userId;
                finishMessage.scenarioExecutionResult = result;
                m.sender.send(finishMessage);
                break;
            }
        } else {
            Strand.sleep(1);
        }
    }
    logger.info("Test is finished");
    MDC.remove("playerId");
    MDC.remove("strand");
    MDC.remove("logfileName");
    return null;
}
Also used : ScenarioUnit(com.nhnent.eat.entity.ScenarioUnit) Messages(com.nhnent.eat.entity.Messages) ScenarioExecutor(com.nhnent.eat.handler.ScenarioExecutor) ArrayList(java.util.ArrayList) RestCommunication(com.nhnent.eat.communication.communicator.RestCommunication) Date(java.util.Date) NettyCommunication(com.nhnent.eat.communication.communicator.NettyCommunication) JmxCommunication(com.nhnent.eat.communication.communicator.JmxCommunication) ScenarioExecutionResult(com.nhnent.eat.entity.ScenarioExecutionResult) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with ScenarioUnit

use of com.nhnent.eat.entity.ScenarioUnit in project eat by nhnent.

the class ScenarioExecutorTest method execute.

@Test
public void execute() throws Exception {
    ScenarioExecutor scenExec = new ScenarioExecutor("test");
    List<ScenarioUnit> listScenPck;
    ScenarioLoader scenLoader = new ScenarioLoader();
    String scenPath = Config.obj().getScenario().getScenarioPath();
    String scenFile = scenPath + "\\chat_test.scn";
    System.out.println(scenPath);
    listScenPck = scenLoader.loadScenario(scenFile, "");
    scenExec.runScenario(listScenPck);
    listScenPck.clear();
}
Also used : ScenarioUnit(com.nhnent.eat.entity.ScenarioUnit) ScenarioExecutor(com.nhnent.eat.handler.ScenarioExecutor) ScenarioLoader(com.nhnent.eat.handler.ScenarioLoader) Test(org.junit.Test)

Aggregations

ScenarioUnit (com.nhnent.eat.entity.ScenarioUnit)4 ScenarioExecutor (com.nhnent.eat.handler.ScenarioExecutor)2 ArrayList (java.util.ArrayList)2 JmxCommunication (com.nhnent.eat.communication.communicator.JmxCommunication)1 NettyCommunication (com.nhnent.eat.communication.communicator.NettyCommunication)1 RestCommunication (com.nhnent.eat.communication.communicator.RestCommunication)1 Messages (com.nhnent.eat.entity.Messages)1 ScenarioExecutionResult (com.nhnent.eat.entity.ScenarioExecutionResult)1 ScenarioLoader (com.nhnent.eat.handler.ScenarioLoader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1