<commit_before>#!/usr/bin/env python
# Copyright 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

import functools
import json
import os
import random
import subprocess
import sys


def _run_js_in_jsc(jit, js, env):
    return subprocess.check_output(
        ['jsc', '-e', """
            function now() {
                return preciseTime() * 1000;
            }
            function globalEval(code) {
                (0, eval)(code);
            }
            function report(label, time) {
                print(label + '_' + %(engine)s, time);
            }

            this.ENV = %(env)s;
            %(js)s
        """ % {
            'env': json.dumps(env),
            'js': js,
            'engine': json.dumps('jsc_' + ('jit' if jit else 'nojit')),
        }],
        env=dict(os.environ, JSC_useJIT='yes' if jit else 'no'),
    )

_run_js_in_jsc_jit = functools.partial(_run_js_in_jsc, True)
_run_js_in_jsc_nojit = functools.partial(_run_js_in_jsc, False)


def _run_js_in_node(js, env):
    return subprocess.check_output(
        ['node', '-e', """
            function now() {
                var hrTime = process.hrtime();
                return hrTime[0] * 1e3 + hrTime[1] * 1e-6;
            }
            function globalEval(code) {
                var vm = require('vm');
                // Hide "module" so UMD wrappers use the global
                vm.runInThisContext('(function(module){' + code + '\\n})()');
            }
            function readFile(filename) {
                var fs = require('fs');
                return fs.readFileSync(filename);
            }
            function report(label, time) {
                console.log(label + '_node', time);
            }

            global.ENV = %(env)s;
            %(js)s
        """ % {
            'env': json.dumps(env),
            'js': js
        }]
    )


def _measure_ssr_ms(engine, react_path, bench_name, bench_path, measure_warm):
    return engine(
        """
            var reactCode = readFile(ENV.react_path);
            var START = now();
            globalEval(reactCode);
            var END = now();
            if (typeof React !== 'object') throw new Error('React not laoded');
            report('factory_ms', END - START);

            globalEval(readFile(ENV.bench_path));
            if (typeof Benchmark !== 'function') {
              throw new Error('benchmark not loaded');
            }
            var START = now();
            var html = React.renderToString(React.createElement(Benchmark));
            html.charCodeAt(0);  // flatten ropes
            var END = now();
            report('ssr_' + ENV.bench_name + '_cold_ms', END - START);

            var warmup = ENV.measure_warm ? 80 : 0;
            var trials = ENV.measure_warm ? 40 : 0;

            for (var i = 0; i < warmup; i++) {
                React.renderToString(React.createElement(Benchmark));
            }

            for (var i = 0; i < trials; i++) {
                var START = now();
                var html = React.renderToString(React.createElement(Benchmark));
                html.charCodeAt(0);  // flatten ropes
                var END = now();<commit_msg>Make benchmark script work in React 15<commit_after>#!/usr/bin/env python
# Copyright 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

import functools
import json
import os
import random
import subprocess
import sys


def _run_js_in_jsc(jit, js, env):
    return subprocess.check_output(
        ['jsc', '-e', """
            function now() {
                return preciseTime() * 1000;
            }
            function globalEval(code) {
                (0, eval)(code);
            }
            function report(label, time) {
                print(label + '_' + %(engine)s, time);
            }

            this.ENV = %(env)s;
            %(js)s
        """ % {
            'env': json.dumps(env),
            'js': js,
            'engine': json.dumps('jsc_' + ('jit' if jit else 'nojit')),
        }],
        env=dict(os.environ, JSC_useJIT='yes' if jit else 'no'),
    )

_run_js_in_jsc_jit = functools.partial(_run_js_in_jsc, True)
_run_js_in_jsc_nojit = functools.partial(_run_js_in_jsc, False)


def _run_js_in_node(js, env):
    return subprocess.check_output(
        ['node', '-e', """
            function now() {
                var hrTime = process.hrtime();
                return hrTime[0] * 1e3 + hrTime[1] * 1e-6;
            }
            function globalEval(code) {
                var vm = require('vm');
                // Hide "module" so UMD wrappers use the global
                vm.runInThisContext('(function(module){' + code + '\\n})()');
            }
            function readFile(filename) {
                var fs = require('fs');
                return fs.readFileSync(filename);
            }
            function report(label, time) {
                console.log(label + '_node', time);
            }

            global.ENV = %(env)s;
            %(js)s
        """ % {
            'env': json.dumps(env),
            'js': js
        }]
    )


def _measure_ssr_ms(engine, react_path, bench_name, bench_path, measure_warm):
    return engine(
        """
            var reactCode = readFile(ENV.react_path);
            var START = now();
            globalEval(reactCode);
            var END = now();
            ReactDOMServer = React.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED || React;
            if (typeof React !== 'object') throw new Error('React not laoded');
            report('factory_ms', END - START);

            globalEval(readFile(ENV.bench_path));
            if (typeof Benchmark !== 'function') {
              throw new Error('benchmark not loaded');
            }
            var START = now();
            var html = ReactDOMServer.renderToString(React.createElement(Benchmark));
            html.charCodeAt(0);  // flatten ropes
            var END = now();
            report('ssr_' + ENV.bench_name + '_cold_ms', END - START);

            var warmup = ENV.measure_warm ? 80 : 0;
            var trials = ENV.measure_warm ? 40 : 0;

            for (var i = 0; i < warmup; i++) {
                ReactDOMServer.renderToString(React.createElement(Benchmark));
            }

            for (var i = 0; i < trials; i++) {
                var START = now();
                var html = ReactDOMServer.renderToString(React.createElement(Benchmark));
                html.charCodeAt(0);  // flatten ropes
                var END = now();
{'fraction_non_alphanumeric': 0.12195824334053276, 'fraction_numerical': 0.007487401007919367, 'mean_word_length': 1.724990192232248, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5291199', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2015, 'n_words': 520}