javascript lambda_performance_monitor.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript lambda_performance_monitor.js相关的知识,希望对你有一定的参考价值。

var WebPageTest = require('webpagetest');

var AWS = require('aws-sdk');
var zlib = require('zlib');

AWS.config.update({region:'us-east-1'});

var s3 = new AWS.S3();
var cloudwatch = new AWS.CloudWatch();

function sendtocloudwatch (metric, scanurl, tester, viewtype,  value, unit ) {
         var params = {
          MetricData: [ /* required */
            {
              MetricName: metric, 
              Dimensions: [
                {
                  Name: 'URL',
                  Value: scanurl
                },
                {
                  Name: 'VIEWTYPE',
                  Value: viewtype
                }
                /* exclude tester for now
                {
                  Name: 'TESTER',
                  Value: tester
                }, */
                /* more items */
              ],
              Unit: unit,
              Value: value
            },
            /* more items */
          ],
          Namespace: 'THV' /* required */
        };
        cloudwatch.putMetricData(params, function(err, data) {
          if (err) console.log(err, err.stack); // an error occurred
          else     console.log(data);           // successful response
        });
        
}

exports.handler = function(event, context) { 
    
    BUCKET_NAME = event.bucketname;
    KEY = event.key;
    SCANURL = event.url;
    RUNS = event.runcount;
    
    var wpt = new WebPageTest('www.webpagetest.org',KEY);
    
    wpt.runTest(SCANURL, {pollResults: 20, runs : RUNS} , function(err, data) {
       if (err) { // an error occurred
            console.log("Error execution test " + err);
            context.fail(err);
       }
       //console.log(data);   

       var outputFilename = (new Date()).toISOString().slice(0,10).replace(/-/g,"") + "_" + data.data.id + ".json";
        
       var outFolder = (new Date()).toISOString().slice(0,7).replace(/-/g,"");
        
        var summary = new Object();
        summary.id = data.data.id;
        summary.url = data.data.url;
        summary.tester = data.data.tester;
        summary.runs = [];
        for (var i = 1; i <= RUNS; i++) {
            var summaryobject = new Object();
            summaryobject.firstView  =  new Object();
            summaryobject.firstView.loadTime = data.data.runs[i].firstView.loadTime;
            summaryobject.firstView.TTFB = data.data.runs[i].firstView.TTFB;
            summaryobject.firstView.render = data.data.runs[i].firstView.render;
            summaryobject.firstView.docTime = data.data.runs[i].firstView.docTime;
            summaryobject.firstView.fullyLoaded = data.data.runs[i].firstView.fullyLoaded;
            summaryobject.firstView.SpeedIndex = data.data.runs[i].firstView.SpeedIndex;
            summaryobject.repeatView  =  new Object();
            summaryobject.repeatView.loadTime = data.data.runs[i].repeatView.loadTime;
            summaryobject.repeatView.TTFB = data.data.runs[i].repeatView.TTFB;
            summaryobject.repeatView.render = data.data.runs[i].repeatView.render;
            summaryobject.repeatView.docTime = data.data.runs[i].repeatView.docTime;
            summaryobject.repeatView.fullyLoaded = data.data.runs[i].repeatView.fullyLoaded;
            summaryobject.repeatView.SpeedIndex = data.data.runs[i].repeatView.SpeedIndex;
            summary.runs.push(summaryobject);
        }
   
        
        sendtocloudwatch("TTFB", data.data.url, data.data.tester, 'firstView', Math.round(data.data.average.firstView.TTFB), 'Milliseconds' );
        sendtocloudwatch("render", data.data.url, data.data.tester, 'firstView', Math.round(data.data.average.firstView.render), 'Milliseconds' );
        sendtocloudwatch("fullyLoaded", data.data.url, data.data.tester, 'firstView', Math.round(data.data.average.firstView.fullyLoaded), 'Milliseconds' );
        sendtocloudwatch("documentLoaded", data.data.url, data.data.tester, 'firstView', Math.round(data.data.average.firstView.docTime), 'Milliseconds' );
        sendtocloudwatch("loadTime", data.data.url, data.data.tester, 'firstView', Math.round(data.data.average.firstView.loadTime), 'Milliseconds' );
        sendtocloudwatch("SpeedIndex", data.data.url, data.data.tester, 'firstView', Math.round(data.data.average.firstView.SpeedIndex), 'None' );
        
        sendtocloudwatch("TTFB", data.data.url, data.data.tester, 'repeatView', Math.round(data.data.average.repeatView.TTFB), 'Milliseconds' );
        sendtocloudwatch("render", data.data.url, data.data.tester, 'repeatView', Math.round(data.data.average.repeatView.render), 'Milliseconds' );
        sendtocloudwatch("fullyLoaded", data.data.url, data.data.tester, 'repeatView',  Math.round(data.data.average.repeatView.fullyLoaded), 'Milliseconds' );
        sendtocloudwatch("documentLoaded", data.data.url, data.data.tester, 'repeatView', Math.round(data.data.average.repeatView.docTime), 'Milliseconds' );
        sendtocloudwatch("loadTime", data.data.url, data.data.tester, 'repeatView', Math.round(data.data.average.repeatView.loadTime), 'Milliseconds' );
        sendtocloudwatch("SpeedIndex", data.data.url, data.data.tester,'repeatView', Math.round(data.data.average.repeatView.SpeedIndex), 'None' );
        
        
   
        
        //save to S3
        var params = {
            Bucket: BUCKET_NAME,
            Key: outFolder + '/summary/' + outputFilename,
            Body: JSON.stringify(summary, null, 2),
            StorageClass: 'REDUCED_REDUNDANCY'
        }

        s3.putObject(params, function(err, output) {
          if (err) {
              console.log(err, err.stack); // an error occurred
              context.fail(err);
          }
          else {   
                    zlib.gzip(JSON.stringify(data, null, 2), function(err, buffer) {
                    if (!err) {
                            //upload to S3
                            var params = {
                                Bucket: BUCKET_NAME,
                                Key: outFolder + '/' + outputFilename + ".gz",
                                ContentType: 'application/json',
                                ContentEncoding: 'gzip',
                                Body: buffer,
                                StorageClass: 'REDUCED_REDUNDANCY'
                            }
                            s3.putObject(params, function(err, data) {
                              if (err) {
                                  console.log(err, err.stack); // an error occurred
                                  context.fail(err);
                              }
                              else {   
                                  context.succeed('succesful test of ' + SCANURL);
                              }
                            });                                               
                      } else {
                            console.log('error compressing' + err);
                            content.fail(err);
                      }
                    });
          }
        });
    }); 
}

以上是关于javascript lambda_performance_monitor.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript JavaScript isset()等效: - JavaScript

JavaScript 使用JavaScript更改CSS(JavaScript)

JavaScript之基础-1 JavaScript(概述基础语法)

前端基础-JavaScript的基本概述和语法

JavaScript

JavaScript