<commit_before>// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:async';
import 'dart:io';
import 'dart:math' as math;

import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as p;

import 'npm.dart';
import 'standalone.dart';
import 'utils.dart';

@Task('Generate benchmark files.')
benchmarkGenerate() async {
  var sources = new Directory("build/benchmark");
  if (!await sources.exists()) await sources.create(recursive: true);

  await _writeNTimes("${sources.path}/small_plain.scss", ".foo {a: b}", 4);
  await _writeNTimes(
      "${sources.path}/large_plain.scss", ".foo {a: b}", math.pow(2, 17));
  await _writeNTimes("${sources.path}/preceding_sparse_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      header: '.x {@extend .y}', footer: '.y {a: b}');
  await _writeNTimes("${sources.path}/following_sparse_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      header: '.y {a: b}', footer: '.x {@extend .y}');
  await _writeNTimes("${sources.path}/preceding_dense_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      header: '.bar {@extend .foo}');
  await _writeNTimes("${sources.path}/following_dense_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      footer: '.bar {@extend .foo}');

  await cloneOrCheckout("https://github.com/twbs/bootstrap", "v4.1.3");
  await _writeNTimes("${sources.path}/bootstrap.scss",
      "@import '../bootstrap/scss/bootstrap';", 16);

  await cloneOrCheckout("https://github.com/alex-page/sass-a11ycolor",
      "2e7ef93ec06f8bbec80b632863e4b2811618af89");
  new File("${sources.path}/a11ycolor.scss").writeAsStringSync("""
    @import '../sass-a11ycolor/dist';

    x {
      // Adapted from a11ycolor's test1.scss, which at one point was much slower
      // in JS than in the Dart VM.
      y: AU-a11ycolor(red, blue)
         AU-a11ycolor(#646464, #E0E0E0)
         AU-a11ycolor(green, blue)
         AU-a11ycolor(pink, blue)
         AU-a11ycolor(blue, blue)
         AU-a11ycolor(#c0c0c0, #c0c0c0)
         AU-a11ycolor(#231284, #ccc)
         AU-a11ycolor(#fff, #fff);
    }
  """);

  var susy = await cloneOrCheckout("https://github.com/oddbird/susy", "v3.0.5");
  await runAsync("npm", arguments: ["install"], workingDirectory: susy);
  new File("${sources.path}/susy.scss")
      .writeAsStringSync("@import '../susy/test/scss/test.scss'");
}

/// Writes [times] instances of [text] to [path].
///
/// If [header] is passed, it's written before [text]. If [footer] is passed,
/// it's written after [text]. If the file already exists and is the expected
/// length, it's not written.
Future _writeNTimes(String path, String text, num times,
    {String header, String footer}) async {
  var file = new File(path);
  var expectedLength = (header == null ? 0 : header.length + 1) +
      (text.length + 1) * times +
      (footer == null ? 0 : footer.length + 1);
  if (file.existsSync() && file.lengthSync() == expectedLength) {
    log("$path already exists.");
    return;
  }

  log("Generating $path...");
  var sink = file.openWrite();
  if (header != null) sink.writeln(header);
  for (var i = 0; i < times; i++) {
    sink.writeln(text);
  }
  if (footer != null) sink.writeln(footer);
  await sink.close();
}

@Task('Run benchmarks for Sass compilation speed.')
@Depends(benchmarkGenerate, snapshot, releaseAppSnapshot, npmPackage)
benchmark() async {
  var libsass = await cloneOrPull('https://github.com/sass/libsass');
  var sassc = await cloneOrPull('https://github.com/sass/sassc');<commit_msg>Run benchmarks against the release-compiled JS<commit_after>// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:async';
import 'dart:io';
import 'dart:math' as math;

import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as p;

import 'npm.dart';
import 'standalone.dart';
import 'utils.dart';

@Task('Generate benchmark files.')
benchmarkGenerate() async {
  var sources = new Directory("build/benchmark");
  if (!await sources.exists()) await sources.create(recursive: true);

  await _writeNTimes("${sources.path}/small_plain.scss", ".foo {a: b}", 4);
  await _writeNTimes(
      "${sources.path}/large_plain.scss", ".foo {a: b}", math.pow(2, 17));
  await _writeNTimes("${sources.path}/preceding_sparse_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      header: '.x {@extend .y}', footer: '.y {a: b}');
  await _writeNTimes("${sources.path}/following_sparse_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      header: '.y {a: b}', footer: '.x {@extend .y}');
  await _writeNTimes("${sources.path}/preceding_dense_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      header: '.bar {@extend .foo}');
  await _writeNTimes("${sources.path}/following_dense_extend.scss",
      ".foo {a: b}", math.pow(2, 17),
      footer: '.bar {@extend .foo}');

  await cloneOrCheckout("https://github.com/twbs/bootstrap", "v4.1.3");
  await _writeNTimes("${sources.path}/bootstrap.scss",
      "@import '../bootstrap/scss/bootstrap';", 16);

  await cloneOrCheckout("https://github.com/alex-page/sass-a11ycolor",
      "2e7ef93ec06f8bbec80b632863e4b2811618af89");
  new File("${sources.path}/a11ycolor.scss").writeAsStringSync("""
    @import '../sass-a11ycolor/dist';

    x {
      // Adapted from a11ycolor's test1.scss, which at one point was much slower
      // in JS than in the Dart VM.
      y: AU-a11ycolor(red, blue)
         AU-a11ycolor(#646464, #E0E0E0)
         AU-a11ycolor(green, blue)
         AU-a11ycolor(pink, blue)
         AU-a11ycolor(blue, blue)
         AU-a11ycolor(#c0c0c0, #c0c0c0)
         AU-a11ycolor(#231284, #ccc)
         AU-a11ycolor(#fff, #fff);
    }
  """);

  var susy = await cloneOrCheckout("https://github.com/oddbird/susy", "v3.0.5");
  await runAsync("npm", arguments: ["install"], workingDirectory: susy);
  new File("${sources.path}/susy.scss")
      .writeAsStringSync("@import '../susy/test/scss/test.scss'");
}

/// Writes [times] instances of [text] to [path].
///
/// If [header] is passed, it's written before [text]. If [footer] is passed,
/// it's written after [text]. If the file already exists and is the expected
/// length, it's not written.
Future _writeNTimes(String path, String text, num times,
    {String header, String footer}) async {
  var file = new File(path);
  var expectedLength = (header == null ? 0 : header.length + 1) +
      (text.length + 1) * times +
      (footer == null ? 0 : footer.length + 1);
  if (file.existsSync() && file.lengthSync() == expectedLength) {
    log("$path already exists.");
    return;
  }

  log("Generating $path...");
  var sink = file.openWrite();
  if (header != null) sink.writeln(header);
  for (var i = 0; i < times; i++) {
    sink.writeln(text);
  }
  if (footer != null) sink.writeln(footer);
  await sink.close();
}

@Task('Run benchmarks for Sass compilation speed.')
@Depends(benchmarkGenerate, snapshot, releaseAppSnapshot, npmReleasePackage)
benchmark() async {
  var libsass = await cloneOrPull('https://github.com/sass/libsass');
  var sassc = await cloneOrPull('https://github.com/sass/sassc');
{'fraction_non_alphanumeric': 0.1772342169991801, 'fraction_numerical': 0.028696365127083904, 'mean_word_length': 3.606041535556954, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6928253', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2566, 'n_words': 628}