1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
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 http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
import TSCBasic
import TSCTestSupport
struct ByteSequence: Sequence {
let bytes16 = [UInt8](repeating: 0, count: 1 << 4)
func makeIterator() -> ByteSequenceIterator {
return ByteSequenceIterator(bytes16: bytes16)
}
}
extension ByteSequence: ByteStreamable {
func write(to stream: WritableByteStream) {
stream.write(sequence: self)
}
}
struct ByteSequenceIterator: IteratorProtocol {
let bytes16: [UInt8]
var index: Int
init(bytes16: [UInt8]) {
self.bytes16 = bytes16
index = 0
}
mutating func next() -> UInt8? {
if index == bytes16.count { return nil }
defer { index += 1 }
return bytes16[index]
}
}
class OutputByteStreamPerfTests: XCTestCasePerf {
func test1MBOfSequence_X10() {
#if canImport(Darwin)
let sequence = ByteSequence()
measure {
for _ in 0..<10 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 16) {
stream.send(sequence)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOfByte_X10() {
#if canImport(Darwin)
let byte = UInt8(0)
measure {
for _ in 0..<10 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 20) {
stream.send(byte)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOfCharacters_X1() {
#if canImport(Darwin)
measure {
for _ in 0..<1 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 20) {
stream.send(Character("X"))
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOf16ByteArrays_X100() {
#if canImport(Darwin)
// Test writing 1MB worth of 16 byte strings.
let bytes16 = [UInt8](repeating: 0, count: 1 << 4)
measure {
for _ in 0..<100 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 16) {
stream.send(bytes16)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
// This should give same performance as 16ByteArrays_X100.
func test1MBOf16ByteArraySlice_X100() {
#if canImport(Darwin)
let bytes32 = [UInt8](repeating: 0, count: 1 << 5)
// Test writing 1MB worth of 16 byte strings.
let bytes16 = bytes32.suffix(from: bytes32.count/2)
measure {
for _ in 0..<100 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 16) {
stream.send(bytes16)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOf1KByteArrays_X1000() {
#if canImport(Darwin)
// Test writing 1MB worth of 1K byte strings.
let bytes1k = [UInt8](repeating: 0, count: 1 << 10)
measure {
for _ in 0..<1000 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 10) {
stream.send(bytes1k)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOf16ByteStrings_X10() {
#if canImport(Darwin)
// Test writing 1MB worth of 16 byte strings.
let string16 = String(repeating: "X", count: 1 << 4)
measure {
for _ in 0..<10 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 16) {
stream.send(string16)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOf1KByteStrings_X100() {
#if canImport(Darwin)
// Test writing 1MB worth of 1K byte strings.
let bytes1k = String(repeating: "X", count: 1 << 10)
measure {
for _ in 0..<100 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 10) {
stream.send(bytes1k)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func test1MBOfJSONEncoded16ByteStrings_X10() {
#if canImport(Darwin)
// Test writing 1MB worth of JSON encoded 16 byte strings.
let string16 = String(repeating: "X", count: 1 << 4)
measure {
for _ in 0..<10 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 16) {
stream.writeJSONEscaped(string16)
}
XCTAssertEqual(stream.bytes.count, 1 << 20)
}
}
#endif
}
func testFormattedJSONOutput() {
#if canImport(Darwin)
// Test the writing of JSON formatted output using stream operators.
struct Thing {
var value: String
init(_ value: String) { self.value = value }
}
let listOfStrings: [String] = (0..<10).map { "This is the number: \($0)!\n" }
let listOfThings: [Thing] = listOfStrings.map(Thing.init)
measure {
for _ in 0..<10 {
let stream = BufferedOutputByteStream()
for _ in 0..<(1 << 10) {
for string in listOfStrings {
stream.send(Format.asJSON(string))
}
stream.send(Format.asJSON(listOfStrings))
stream.send(Format.asJSON(listOfThings, transform: { $0.value }))
}
XCTAssertGreaterThan(stream.bytes.count, 1000)
}
}
#endif
}
func testJSONToString_X100() {
#if canImport(Darwin)
let foo = JSON.dictionary([
"foo": .string("bar"),
"bar": .int(2),
"baz": .array([1, 2, 3].map(JSON.int)),
])
let bar = JSON.dictionary([
"poo": .array([foo, foo, foo]),
"foo": .int(1),
])
let baz = JSON.dictionary([
"poo": .array([foo, bar, foo]),
"foo": .int(1),
])
let json = JSON.array((0..<100).map { _ in baz })
measure {
for _ in 0..<100 {
let result = json.toString()
XCTAssertGreaterThan(result.utf8.count, 10)
}
}
#endif
}
}
|