循环中Bot变得缓慢

时间:2017-03-22 06:42:08

标签: bots botframework

我有一个案例,我的机器人反复询问测验问题,只有当用户无法正确回答或超时时,自定义提示才会结束。如果我清除了与用户的所有会话数据,那么最初的交互非常顺畅,但是当我超过50个问题时,它变得越来越慢,直到机器人无法使用。在用户单击按钮和确认按钮单击之间存在明显的延迟。这将是我的自定义提示的代码。任何想法发生了什么

请参阅此VISUALLY HERE

'use strict';

const
    builder = require('botbuilder'),
    engine = require('./engine'),
    emoji = require('node-emoji'),
    strings = require('./madmathstrings')

const
    MADMATH_ROUND_TIMEOUT = parseInt(process.env.MADMATH_ROUND_TIMEOUT) || 10,
    MADMATH_ROUND_TIMEOUT_BUFFER = parseInt(process.env.MADMATH_ROUND_TIMEOUT_BUFFER) || 2,
    DIALOG_NAME = 'madmath'


module.exports.beginDialog = function (session, options) {
    session.beginDialog(DIALOG_NAME, options);
}

module.exports.create = function (bot) {
    let prompt = new builder.IntentDialog()
        .onBegin(onQuestion)
        .onDefault(onAnswer);
    bot.dialog(DIALOG_NAME, prompt);
}

function onQuestion(session, args) {
    /**Record start time for logging purposes */
    let start = new Date().getTime()
    let buttons = []
    let dialogData = session.dialogData

    /**What is the difficulty level, 0 or 1 or 2 */
    dialogData.difficulty = args ? args.difficulty : 0

    /**The current index of the question */
    dialogData.index = args ? args.index : 0

    /**A maximum value used to generate random numbers below it */
    dialogData.max = args ? args.max : 10

    /**A minimum value used to generate random numbers above it */
    dialogData.min = args ? args.min : 0

    /**The total points scored by the user so far */
    dialogData.score = args ? args.score : 0

    /**Text indicating the level number */
    let level

    /**Check if it is the 5th question, 10th question, 15th ... */
    if ((dialogData.index + 1) % 5 === 0) {

        /**Increase the difficulty level by 1 so that it goes from 0 to 1 or 1 to 2 or 2 to 0 */
        dialogData.difficulty = (dialogData.difficulty + 1) % 3
        dialogData.min = dialogData.min + 1
        dialogData.max = dialogData.max + 5
    }

    /**Displays Level 0 on top of the 1st question*/
    if (dialogData.index === 0) {
        level = `Level 1`
    }
    /**Displays level N at the top of every 5th question */
    else if ((dialogData.index + 1) % 5 === 0) {
        level = `Level ${((dialogData.index + 1) / 5) + 1}`
    }

    /**No levels are displayed for intermediate questions */
    else {
        level = ''
    }

    /**Get the minimum and maximum as negative or positive numbers in a random fashion */
    let min, max

    /**a positive min number and a positive maximum number  */
    if (Math.floor(Math.random() * 2) === 1) {
        min = dialogData.min
        max = dialogData.max
    }

    /**A negative minimum number and a negative maximum number */
    else {
        min = -dialogData.max
        max = -dialogData.min
    }

    /**Generate a Math problem with 3 choices for an answer at the given difficulty level in the range supplied by minimum and maximum */
    let question = engine.puzzleMath(3, dialogData.difficulty, min, max)

    /**Convert the choices from Number to String to be used inside a button */
    question.choices.map(
        item => buttons.push(
            new builder.CardAction.imBack(
                session, item.toString(), `  ${item.toString()}  `
            )
        )
    )

    /**Send the question with the level information if available, the index and the Math expression along with a countdown timer as GIF attachment */
    session.send(new builder.Message(session)
        .text(level ? level + '  \n' + strings.question : strings.question, dialogData.index + 1, question.expression)
        .addAttachment(
        new builder.AnimationCard(session)
            .media([{
                profile: "image/gif",
                url: 'https://media.giphy.com/media/l3q2silev6exF53pu/200w.gif'
            }])
            .buttons(buttons)
        ))

    /**Store the question to be referenced later while checking for an answer */
    dialogData.question = question

    /**Record the current time as the time when the question was presented to the user*/
    dialogData.startTime = new Date().getTime()

    /**Record the end time for running all the code inside this method for logging purposes */
    let end = new Date().getTime()

    console.log(`${(end - start) / 1000} seconds`)
}

function onAnswer(session) {

    /**Record start time of this method for logging purposes */
    let start = new Date().getTime()
    let dialogData = session.dialogData, guess, isCorrect, isValid, onTime = false, speedMultiplier = 0

    /**Get the time when the user sent the answer */
    dialogData.endTime = new Date(session.message.timestamp).getTime()

    /**Find the difference between the time the user saw the question and answered it */
    let duration = Math.floor((dialogData.endTime - dialogData.startTime) / 1000)

    //If the difference is less than 5 seconds, we give a 1000 points
    if (duration <= MADMATH_ROUND_TIMEOUT_BUFFER) {
        speedMultiplier = 10;
        onTime = true;
    }

    //If the difference is between 5 - 15 seconds, we give them 100 points per second of speed
    else if (duration < MADMATH_ROUND_TIMEOUT + MADMATH_ROUND_TIMEOUT_BUFFER) {

        /**
         * If the total timeout is 10 seconds and we have a buffer of 5 seconds,
         * we calculate 10/10 * (10 + 5 - 7) = 8, 8 x 100 = 800 gives the number of points to be assigned to the user for this answer
         */
        speedMultiplier = (10 / MADMATH_ROUND_TIMEOUT) * (MADMATH_ROUND_TIMEOUT + MADMATH_ROUND_TIMEOUT_BUFFER - duration)

        /**The person has answered on time */
        onTime = true;
    }

    //Assign the user 100 points at the 15th second
    else if (duration === MADMATH_ROUND_TIMEOUT + MADMATH_ROUND_TIMEOUT_BUFFER) {
        speedMultiplier = 1
        onTime = true;
    }

    //The person did not answer on time
    else {
        speedMultiplier = 0
        onTime = false
    }

    /**The value entered by the user */
    guess = builder.EntityRecognizer.parseNumber(session.message.text)

    /**Check if the user answered the question correctly */
    isCorrect = dialogData.question.answer === guess

    /**Check if the answer is a number */
    isValid = !isNaN(guess)

    /**If the person answered with the correct number on time */
    if (isCorrect && isValid && onTime) {

        /**Increase the index of the question */
        dialogData.index++;

        /**Increase the score of the user so far */
        dialogData.score = dialogData.score + speedMultiplier * 100

        /**Fire the next question */
        session.replaceDialog(DIALOG_NAME, dialogData)
    }

    /**If the person did not answer on time */
    else if (!onTime) {

        /**How late is the person after 15 seconds */
        let howLate = duration - (MADMATH_ROUND_TIMEOUT + MADMATH_ROUND_TIMEOUT_BUFFER)

        /**Get the delay message to be shown to the user */
        let text = strings.timeout(howLate)

        /**Show a GIF image representing timeout */
        let message = new builder.Message(session)
            .attachments([
                new builder.AnimationCard(session)
                    .title(text.title)
                    .subtitle(text.subtitle)
                    .media([{
                        profile: "image/gif",
                        url: 'http://media1.giphy.com/media/26uTt2zN11nFuyH1C/200w.gif'
                    }])
            ])
        session.send(message)

        /**End the round indicating the score and the longest streak */
        session.endDialogWithResult({
            score: dialogData.score,
            streak: dialogData.index
        })
    }

    /**If the person answered incorrectly or with an invalid response */
    else {

        /**End the round indicating the score and the longest streak */
        session.endDialogWithResult({
            score: dialogData.score,
            streak: dialogData.index
        })
    }

    /**Record end time of this method for logging purposes */
    let end = new Date().getTime()
    console.log(`${(end - start) / 1000} seconds`)
}

我是否在任何地方泄露数据。任何想法如何解决这种不断增加的延迟

更新1

我将autoBatchDelay设置为0并且问题仍然存在

后尝试了此操作

0 个答案:

没有答案