<commit_before>//===--- DataBenchmarks.swift ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils
import Foundation

let d: [BenchmarkCategory] =  [.validation, .api, .Data]

public let DataBenchmarks = [
  BenchmarkInfo(name: "DataCreateEmpty",
    runFunction: { for _ in 0..<$0*10_000 { blackHole(Data()) } },
    tags: d, legacyFactor: 10),
  BenchmarkInfo(name: "DataCreateSmall",
    runFunction: { for _ in 0..<$0*10_000 { blackHole(sampleData(.small)) } },
    tags: d, legacyFactor: 10),
  BenchmarkInfo(name: "DataCreateMedium",
    runFunction: { for _ in 0..<$0*100 { blackHole(sampleData(.medium)) } },
    tags: d, legacyFactor: 100),

  BenchmarkInfo(name: "DataCreateEmptyArray",
    runFunction: { for _ in 0..<$0*2_000 { blackHole(Data([])) } }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataCreateSmallArray",
    runFunction: { for _ in 0..<$0*2_000 { blackHole(Data(
      [0, 1, 2, 3, 4, 5, 6])) } }, tags: d, legacyFactor: 50),
  BenchmarkInfo(name: "DataCreateMediumArray",
    runFunction: { for _ in 0..<$0*500 { blackHole(Data([
      0, 1, 2, 3, 4, 5, 6,
      0, 1, 2, 3, 4, 5, 6,
      0, 1, 2, 3, 4, 5, 6,
      0, 1, 2, 3, 4, 5, 6,
    ])) } }, tags: d, legacyFactor: 20),

  BenchmarkInfo(name: "Data.init.Sequence.ExactCount", runFunction: {
    for _ in 0..<$0*100 {
      blackHole(Data(repeatElement(UInt8(0xA0), count: 809)))
    } }, tags: d),
  BenchmarkInfo(name: "Data.init.Sequence.UnderestimatedCount", runFunction: {
    for _ in 0..<$0*100 { blackHole(Data(repeatElementSeq(809))) } }, tags: d),

  BenchmarkInfo(name: "DataSubscriptSmall",
    runFunction: { let data = small
      for _ in 0..<$0*10_000 { blackHole(data[1]) } }, tags: d),
  BenchmarkInfo(name: "DataSubscriptMedium",
    runFunction: { let data = medium
      for _ in 0..<$0*10_000 { blackHole(data[521]) } }, tags: d),

  BenchmarkInfo(name: "DataCountSmall",
    runFunction: { count($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "DataCountMedium",
    runFunction: { count($0*10_000, data: medium) }, tags: d),

  BenchmarkInfo(name: "DataSetCountSmall",
    runFunction: { setCount($0*10_000, data: small, extra: 3) }, tags: d),
  BenchmarkInfo(name: "DataSetCountMedium",
    runFunction: { setCount($0*1_000, data: medium, extra: 100) }, tags: d,
    legacyFactor: 10),

  BenchmarkInfo(name: "DataAccessBytesSmall",
    runFunction: { withUnsafeBytes($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "DataAccessBytesMedium",
    runFunction: { withUnsafeBytes($0*10_000, data: medium) }, tags: d),

  BenchmarkInfo(name: "DataMutateBytesSmall",
    runFunction: { withUnsafeMutableBytes($0*500, data: small) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataMutateBytesMedium",
    runFunction: { withUnsafeMutableBytes($0*500, data: medium) }, tags: d,
    legacyFactor: 20),

  BenchmarkInfo(name: "DataCopyBytesSmall",
    runFunction: { copyBytes($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "DataCopyBytesMedium",
    runFunction: { copyBytes($0*5_000, data: medium) }, tags: d,
    legacyFactor: 2),

  BenchmarkInfo(name: "DataAppendBytesSmall",
    runFunction: { append($0*10_000, bytes: 3, to: small) }, tags: d),
  BenchmarkInfo(name: "DataAppendBytesMedium",
    runFunction: { append($0*500, bytes: 809, to: medium) }, tags: d,
    legacyFactor: 20),

  BenchmarkInfo(name: "DataAppendArray",
    runFunction: { append($0*100, arraySize: 809, to: medium) }, tags: d,
    legacyFactor: 100),

  BenchmarkInfo(name: "DataReset",
    runFunction: { resetBytes($0*100, in: 431..<809, data: medium) },
    tags: d, legacyFactor: 100),

  BenchmarkInfo(name: "DataReplaceSmall", runFunction: {
    replace($0*100, data: medium, subrange:431..<809, with: small) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceMedium", runFunction: {
    replace($0*100, data: medium, subrange:431..<809, with: medium) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceLarge", runFunction: {
    replace($0*100, data: medium, subrange:431..<809, with: large) },
    tags: d, legacyFactor: 100),

  BenchmarkInfo(name: "DataReplaceSmallBuffer", runFunction: {
    replaceBuffer($0*100, data: medium, subrange:431..<809, with: small) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceMediumBuffer", runFunction: {
    replaceBuffer($0*100, data: medium, subrange:431..<809, with: medium) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceLargeBuffer", runFunction: {
    replaceBuffer($0*10, data: medium, subrange:431..<809, with: large) },
    tags: d),

  BenchmarkInfo(name: "DataAppendSequence",
    runFunction: { append($0*100, sequenceLength: 809, to: medium) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "Data.append.Sequence.ExactCount", runFunction: {
    append($0*100, sequence: repeatElement(UInt8(0xA0), count: 809),
           to: medium) }, tags: d),
  BenchmarkInfo(name: "Data.append.Sequence.UnderestimatedCount", runFunction: {
    append($0*100, sequence: repeatElementSeq(809), to: medium) },
    tags: d),

  BenchmarkInfo(name: "DataAppendDataSmallToSmall",
    runFunction: { append($0*500, data: small, to: small) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataSmallToMedium",
    runFunction: { append($0*500, data: small, to: medium) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataSmallToLarge",
    runFunction: { append($0*50, data: small, to: large) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataMediumToSmall",
    runFunction: { append($0*500, data: medium, to: small) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataMediumToMedium",
    runFunction: { append($0*500, data: medium, to: medium) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataMediumToLarge",
    runFunction: { append($0*50, data: medium, to: large) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataLargeToSmall",
    runFunction: { append($0*50, data: large, to: small) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataLargeToMedium",
    runFunction: { append($0*50, data: large, to: medium) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataLargeToLarge",
    runFunction: { append($0*50, data: large, to: large) }, tags: d,
    legacyFactor: 200),

  BenchmarkInfo(name: "DataToStringEmpty",
    runFunction: { string($0*200, from: emptyData) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataToStringSmall",
    runFunction: { string($0*200, from: smallData) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataToStringMedium",
    runFunction: { string($0*200, from: mediumData) }, tags: d,
    legacyFactor: 50),

  BenchmarkInfo(name: "StringToDataEmpty",
    runFunction: { data($0*200, from: emptyString) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "StringToDataSmall",
    runFunction: { data($0*200, from: smallString) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "StringToDataMedium",
    runFunction: { data($0*200, from: mediumString) }, tags: d,
    legacyFactor: 50),

  BenchmarkInfo(name: "Data.hash.Empty",
    runFunction: { hash($0*10_000, data: Data()) }, tags: d),
  BenchmarkInfo(name: "Data.hash.Small",
    runFunction: { hash($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "Data.hash.Medium",
    runFunction: { hash($0*1_000, data: medium) }, tags: d),
]

let emptyString = ""
let smallString = "\r\n"
let mediumString = "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
let emptyData = Data()
let smallData = Data(smallString.utf8)
let mediumData = Data(mediumString.utf8)
<commit_msg>[benchmark] Add large unicode String<->Data benchmark.<commit_after>//===--- DataBenchmarks.swift ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils
import Foundation

let d: [BenchmarkCategory] =  [.validation, .api, .Data]

public let DataBenchmarks = [
  BenchmarkInfo(name: "DataCreateEmpty",
    runFunction: { for _ in 0..<$0*10_000 { blackHole(Data()) } },
    tags: d, legacyFactor: 10),
  BenchmarkInfo(name: "DataCreateSmall",
    runFunction: { for _ in 0..<$0*10_000 { blackHole(sampleData(.small)) } },
    tags: d, legacyFactor: 10),
  BenchmarkInfo(name: "DataCreateMedium",
    runFunction: { for _ in 0..<$0*100 { blackHole(sampleData(.medium)) } },
    tags: d, legacyFactor: 100),

  BenchmarkInfo(name: "DataCreateEmptyArray",
    runFunction: { for _ in 0..<$0*2_000 { blackHole(Data([])) } }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataCreateSmallArray",
    runFunction: { for _ in 0..<$0*2_000 { blackHole(Data(
      [0, 1, 2, 3, 4, 5, 6])) } }, tags: d, legacyFactor: 50),
  BenchmarkInfo(name: "DataCreateMediumArray",
    runFunction: { for _ in 0..<$0*500 { blackHole(Data([
      0, 1, 2, 3, 4, 5, 6,
      0, 1, 2, 3, 4, 5, 6,
      0, 1, 2, 3, 4, 5, 6,
      0, 1, 2, 3, 4, 5, 6,
    ])) } }, tags: d, legacyFactor: 20),

  BenchmarkInfo(name: "Data.init.Sequence.ExactCount", runFunction: {
    for _ in 0..<$0*100 {
      blackHole(Data(repeatElement(UInt8(0xA0), count: 809)))
    } }, tags: d),
  BenchmarkInfo(name: "Data.init.Sequence.UnderestimatedCount", runFunction: {
    for _ in 0..<$0*100 { blackHole(Data(repeatElementSeq(809))) } }, tags: d),

  BenchmarkInfo(name: "DataSubscriptSmall",
    runFunction: { let data = small
      for _ in 0..<$0*10_000 { blackHole(data[1]) } }, tags: d),
  BenchmarkInfo(name: "DataSubscriptMedium",
    runFunction: { let data = medium
      for _ in 0..<$0*10_000 { blackHole(data[521]) } }, tags: d),

  BenchmarkInfo(name: "DataCountSmall",
    runFunction: { count($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "DataCountMedium",
    runFunction: { count($0*10_000, data: medium) }, tags: d),

  BenchmarkInfo(name: "DataSetCountSmall",
    runFunction: { setCount($0*10_000, data: small, extra: 3) }, tags: d),
  BenchmarkInfo(name: "DataSetCountMedium",
    runFunction: { setCount($0*1_000, data: medium, extra: 100) }, tags: d,
    legacyFactor: 10),

  BenchmarkInfo(name: "DataAccessBytesSmall",
    runFunction: { withUnsafeBytes($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "DataAccessBytesMedium",
    runFunction: { withUnsafeBytes($0*10_000, data: medium) }, tags: d),

  BenchmarkInfo(name: "DataMutateBytesSmall",
    runFunction: { withUnsafeMutableBytes($0*500, data: small) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataMutateBytesMedium",
    runFunction: { withUnsafeMutableBytes($0*500, data: medium) }, tags: d,
    legacyFactor: 20),

  BenchmarkInfo(name: "DataCopyBytesSmall",
    runFunction: { copyBytes($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "DataCopyBytesMedium",
    runFunction: { copyBytes($0*5_000, data: medium) }, tags: d,
    legacyFactor: 2),

  BenchmarkInfo(name: "DataAppendBytesSmall",
    runFunction: { append($0*10_000, bytes: 3, to: small) }, tags: d),
  BenchmarkInfo(name: "DataAppendBytesMedium",
    runFunction: { append($0*500, bytes: 809, to: medium) }, tags: d,
    legacyFactor: 20),

  BenchmarkInfo(name: "DataAppendArray",
    runFunction: { append($0*100, arraySize: 809, to: medium) }, tags: d,
    legacyFactor: 100),

  BenchmarkInfo(name: "DataReset",
    runFunction: { resetBytes($0*100, in: 431..<809, data: medium) },
    tags: d, legacyFactor: 100),

  BenchmarkInfo(name: "DataReplaceSmall", runFunction: {
    replace($0*100, data: medium, subrange:431..<809, with: small) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceMedium", runFunction: {
    replace($0*100, data: medium, subrange:431..<809, with: medium) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceLarge", runFunction: {
    replace($0*100, data: medium, subrange:431..<809, with: large) },
    tags: d, legacyFactor: 100),

  BenchmarkInfo(name: "DataReplaceSmallBuffer", runFunction: {
    replaceBuffer($0*100, data: medium, subrange:431..<809, with: small) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceMediumBuffer", runFunction: {
    replaceBuffer($0*100, data: medium, subrange:431..<809, with: medium) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "DataReplaceLargeBuffer", runFunction: {
    replaceBuffer($0*10, data: medium, subrange:431..<809, with: large) },
    tags: d),

  BenchmarkInfo(name: "DataAppendSequence",
    runFunction: { append($0*100, sequenceLength: 809, to: medium) },
    tags: d, legacyFactor: 100),
  BenchmarkInfo(name: "Data.append.Sequence.ExactCount", runFunction: {
    append($0*100, sequence: repeatElement(UInt8(0xA0), count: 809),
           to: medium) }, tags: d),
  BenchmarkInfo(name: "Data.append.Sequence.UnderestimatedCount", runFunction: {
    append($0*100, sequence: repeatElementSeq(809), to: medium) },
    tags: d),

  BenchmarkInfo(name: "DataAppendDataSmallToSmall",
    runFunction: { append($0*500, data: small, to: small) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataSmallToMedium",
    runFunction: { append($0*500, data: small, to: medium) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataSmallToLarge",
    runFunction: { append($0*50, data: small, to: large) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataMediumToSmall",
    runFunction: { append($0*500, data: medium, to: small) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataMediumToMedium",
    runFunction: { append($0*500, data: medium, to: medium) }, tags: d,
    legacyFactor: 20),
  BenchmarkInfo(name: "DataAppendDataMediumToLarge",
    runFunction: { append($0*50, data: medium, to: large) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataLargeToSmall",
    runFunction: { append($0*50, data: large, to: small) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataLargeToMedium",
    runFunction: { append($0*50, data: large, to: medium) }, tags: d,
    legacyFactor: 200),
  BenchmarkInfo(name: "DataAppendDataLargeToLarge",
    runFunction: { append($0*50, data: large, to: large) }, tags: d,
    legacyFactor: 200),

  BenchmarkInfo(name: "DataToStringEmpty",
    runFunction: { string($0*200, from: emptyData) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataToStringSmall",
    runFunction: { string($0*200, from: smallData) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataToStringMedium",
    runFunction: { string($0*200, from: mediumData) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "DataToStringLargeUnicode",
    runFunction: { string($0*200, from: largeUnicodeData) }, tags: d,
    legacyFactor: 50),

  BenchmarkInfo(name: "StringToDataEmpty",
    runFunction: { data($0*200, from: emptyString) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "StringToDataSmall",
    runFunction: { data($0*200, from: smallString) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "StringToDataMedium",
    runFunction: { data($0*200, from: mediumString) }, tags: d,
    legacyFactor: 50),
  BenchmarkInfo(name: "StringToDataLargeUnicode",
    runFunction: { data($0*200, from: largeUnicodeString) }, tags: d,
    legacyFactor: 50),

  BenchmarkInfo(name: "Data.hash.Empty",
    runFunction: { hash($0*10_000, data: Data()) }, tags: d),
  BenchmarkInfo(name: "Data.hash.Small",
    runFunction: { hash($0*10_000, data: small) }, tags: d),
  BenchmarkInfo(name: "Data.hash.Medium",
    runFunction: { hash($0*1_000, data: medium) }, tags: d),
]

let emptyString = ""
let smallString = "\r\n"
let mediumString = "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
let largeUnicodeString = 
  "Swiftに大幅な改良が施され、𓀀𓀁𓀂𓀃, 🇺🇸🇨🇦🇲🇽" + mediumString
let emptyData = Data()
let smallData = Data(smallString.utf8)
let mediumData = Data(mediumString.utf8)
let largeUnicodeData = Data(largeUnicodeString.utf8)

{'fraction_non_alphanumeric': 0.17362349863241766, 'fraction_numerical': 0.05369247235105244, 'mean_word_length': 4.179858330766861, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 4, 'https://': 4, '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': '3621127', 'n_tokens_mistral': 6469, 'n_tokens_neox': 5841, 'n_words': 1357}