Skip to content

Add simple benchmarks for JSON coding #20886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ set(SWIFT_BENCH_MODULES
single-source/InsertCharacter
single-source/Integrate
single-source/IterateData
single-source/JSON
single-source/Join
single-source/LazyFilter
single-source/LinkedList
Expand Down
88 changes: 88 additions & 0 deletions benchmark/single-source/JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//===--- JSON.swift -------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 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

public let JSON = [
BenchmarkInfo(name: "JSONPerfEncode", runFunction: run_JSONPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_coder),
BenchmarkInfo(name: "JSONPerfDecode", runFunction: run_JSONPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_coder),
]


struct Tove: Codable {
var slithy: Bool = true
var gyreInRadians: Double = 0.3
}

struct Borogove : Codable {
var mimsyness: Int = Int.max
}

struct Wabe : Codable {
var toves: [Tove]
var borogoves: [Borogove]?
static var canonical: Wabe {
return Wabe(toves: [Tove(), Tove(), Tove(), Tove(),
Tove(slithy: false, gyreInRadians: 1.8)],
borogoves: [Borogove(mimsyness: 18), Borogove(mimsyness: 74),
Borogove(), Borogove()])
}
}

struct Beast : Codable {
var name: String
}


struct Jabberwocky : Codable {
var time = "brillig"
var wabe = Wabe.canonical
var beware: [Beast] = [ Beast(name: "Jabberwock"), Beast(name: "Bandersnatch"), Beast(name: "Jubjub Bird")]
var swordType = "vörpal"
}

struct JSONPerfTester {
var poems = Array(repeating: Jabberwocky(), count: 6)
var encoder: JSONEncoder = JSONEncoder()
var decoder: JSONDecoder = JSONDecoder()
var data: Data! = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to give this a default value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not


init() {
data = try! encoder.encode(Array(poems.prefix(3)))
//pre-warm everything to try to reduce variance
let _ = try! encoder.encode(poems)
let _ = try! decoder.decode(Array<Jabberwocky>.self,
from: data)
}
}

var tester: JSONPerfTester! = nil

public func setup_coder() {
tester = JSONPerfTester()
}

@inline(never)
public func run_JSONPerfEncode(_ N: Int) {
for _ in 0 ..< N {
let _ = try! tester.encoder.encode(tester.poems)
}
}

@inline(never)
public func run_JSONPerfDecode(_ N: Int) {
for _ in 0 ..< N {
let _ = try! tester.decoder.decode(Array<Jabberwocky>.self,
from: tester.data)
}
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import Histogram
import InsertCharacter
import Integrate
import IterateData
import JSON
import Join
import LazyFilter
import LinkedList
Expand Down Expand Up @@ -254,6 +255,7 @@ registerBenchmark(Histogram)
registerBenchmark(InsertCharacter)
registerBenchmark(IntegrateTest)
registerBenchmark(IterateData)
registerBenchmark(JSON)
registerBenchmark(Join)
registerBenchmark(LazyFilter)
registerBenchmark(LinkedList)
Expand Down