<commit_before>var _ = require('underscore');
var carto = require('./index');

carto.Renderer = function Renderer(env, options) {
    this.env = env || {};
    this.options = options || {};
    this.options.mapnik_version = this.options.mapnik_version || 'latest';
};

// Prepare a MSS document (given as an string) into a
// XML Style fragment (mostly useful for debugging)
//
// - @param {String} str the mss contents as a string.
// - @param {Object} env renderer environment options.
carto.Renderer.prototype.renderMSS = function render(data, callback) {
    // effects is a container for side-effects, which currently
    // are limited to FontSets.
    var env = _(this.env).defaults({
        benchmark: false,
        validation_data: false,
        effects: []
    });

    if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) {
        return callback(new Error("Could not set mapnik version to " + this.options.mapnik_version), null);
    }

    var output = [];
    var styles = [];
    var time = +new Date();
    var parser = (carto.Parser(env)).parse(data);
    if (env.benchmark) {
        console.warn('Parsing time: ' + (new Date() - time) + 'ms');
    }
    time = +new Date();
    var rule_list = parser.toList(env);
    var rules = inheritRules(rule_list,env);
    if (env.benchmark) {
        console.warn('Rule generation time: ' + (new Date() - time) + 'ms');
    }
    time = +new Date();
    var sorted = sortStyles(rules,env);
    if (env.benchmark) {
        console.warn('Sorted time: ' + (new Date() - time) + 'ms');
    }
    var time = +new Date();
    for (var k = 0, rule, style_name; k < sorted.length; k++) {
        rule = sorted[k];
        style_name = 'layer' + (rule.attachment !== '__default__' ? '-' + rule.attachment : '');
        styles.push(style_name);

        var time_to_xml = +new Date();
        // env.effects can be modified by this call
        output.push(carto.tree.Style.toXML(style_name, rule.attachment, rule, env));
        if (env.benchmark) {
            console.warn('Calling toXML for style "' + style_name + '" (#'+k + '): ' + (new Date() - time_to_xml) + 'ms');
        }
    }
    if (env.benchmark) {
        console.warn('Total Style toXML time: ' + (new Date() - time) + 'ms');
    }
    if (env.errors) return callback(env.errors);
    return callback(null, output.join('\n'));<commit_msg>use console.time for mss benchmarking<commit_after>var _ = require('underscore');
var carto = require('./index');

carto.Renderer = function Renderer(env, options) {
    this.env = env || {};
    this.options = options || {};
    this.options.mapnik_version = this.options.mapnik_version || 'latest';
};

// Prepare a MSS document (given as an string) into a
// XML Style fragment (mostly useful for debugging)
//
// - @param {String} str the mss contents as a string.
// - @param {Object} env renderer environment options.
carto.Renderer.prototype.renderMSS = function render(data, callback) {
    // effects is a container for side-effects, which currently
    // are limited to FontSets.
    var env = _(this.env).defaults({
        benchmark: false,
        validation_data: false,
        effects: []
    });

    if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) {
        return callback(new Error("Could not set mapnik version to " + this.options.mapnik_version), null);
    }

    var output = [];
    var styles = [];

    if (env.benchmark) console.time('Parsing MSS');
    var parser = (carto.Parser(env)).parse(data);
    if (env.benchmark) console.timeEnd('Parsing MSS');

    if (env.benchmark) console.time('Rule generation');
    var rule_list = parser.toList(env);
    if (env.benchmark) console.timeEnd('Rule generation');

    if (env.benchmark) console.time('Rule inheritance');
    var rules = inheritRules(rule_list,env);
    if (env.benchmark) console.timeEnd('Rule inheritance');

    if (env.benchmark) console.time('Style sort');
    var sorted = sortStyles(rules,env);
    if (env.benchmark) console.timeEnd('Style sort');

    if (env.benchmark) console.time('Total Style generation');
    for (var k = 0, rule, style_name; k < sorted.length; k++) {
        rule = sorted[k];
        style_name = 'style-' + (rule.attachment !== '__default__' ? '-' + rule.attachment : '');
        styles.push(style_name);
        var bench_name = '\tStyle "'+style_name+'" (#'+k+') toXML';
        if (env.benchmark) console.time(bench_name);
        // env.effects can be modified by this call
        output.push(carto.tree.Style.toXML(style_name, rule.attachment, rule, env));
        if (env.benchmark) console.timeEnd(bench_name);
    }
    if (env.benchmark) console.timeEnd('Total Style generation');
    if (env.errors) return callback(env.errors);
    return callback(null, output.join('\n'));
{'fraction_non_alphanumeric': 0.15408608858390518, 'fraction_numerical': 0.0004158868787689748, 'mean_word_length': 3.0217391304347827, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5285908', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1467, 'n_words': 392}