Wie schreibe ich in die CSV-Datei in Javascript

Ich habe ein Skript (mit PhantomJS), das testet, wie lange es dauert, eine Webseite zu laden. Ich versuche herauszufinden, wie das Ergebnis der Zeit geschrieben wird, die zum Laden der Seite in eine CSV-Datei benötigt wird. Führen Sie dann den Test erneut aus, um der CSV-Datei ein weiteres Ergebnis hinzuzufügen.

Code:

var page = require('webpage').create(),
    system = require('system'),
    t, address;
var pageLoadArray = [];
var csvContents = "";
fs = require('fs');

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit(1);
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } 
        else {
            t = Date.now() - t;
            console.log('Page title is ' + page.evaluate(function () {
                return document.title;
            }));

            if(t>7000){
                console.log('Loading time was too long... ' + t + "msec");
                pageLoadArray.push(t);
                console.log(pageLoadArray.length);
                console.log(pageLoadArray[0]);
                //store the time value to the .csv file
                phantom.exit(1);
            }
            else{
                console.log('Loading time ' + t + ' msec');
                pageLoadArray.push(t);
                console.log(pageLoadArray.length);
                console.log(pageLoadArray[0]);
                //store the time value to the .csv file
            }
        }
        phantom.exit();
    });

}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage