Skip to content

Commit

Permalink
Performance tests
Browse files Browse the repository at this point in the history
* Created performance tests to compare Vuckt's `Float3` performance to Swift `simd.float`'s to `GLKVector3` to C/Obj-C `simd_float3`.
* Added new `VucktPerformanceTests.swift` and `VucktCPerformanceTests.m` to Xcode project & SPM package.
* Minor: Removed old placeholder `testPerformanceExample()`.
  • Loading branch information
capnslipp committed Mar 17, 2023
1 parent ea15527 commit 32dd81d
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 19 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ let package = Package(
swiftSettings: [ .define("NO_OBJC_BRIDGE") ]
),
.testTarget(name: "VucktTests", dependencies: ["Vuckt"], path: "Tests/",
sources: [ "VucktTests.swift", "VucktPerformanceTests.swift" ],
swiftSettings: [ .define("NO_OBJC_BRIDGE") ]
),
],
Expand Down
72 changes: 72 additions & 0 deletions Tests/VucktCPerformanceTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// VucktCTests.m
// Vuckt
//
// Created by Cap'n Slipp on 5/13/20.
//

#import <XCTest/XCTest.h>
#import <simd/simd.h>



float randomFloatFromNegOneToPosOne() {
float randomFloat = (float)(arc4random() % ((uint32_t)RAND_MAX + 1)) / RAND_MAX;
return randomFloat * 2.0 - 1.0;
}

static const int kIterationCount = 1000000;



@interface VucktCTests : XCTestCase
@end


@implementation VucktCTests

- (void)setUp {
}

- (void)tearDown {
}

- (void)testSimdFloat3Performance
{
simd_float3 *testSetA = calloc(kIterationCount, sizeof(simd_float3));
simd_float3 *testSetB = calloc(kIterationCount, sizeof(simd_float3));
for (int i = 0; i < 1000000; ++i) {
float value = (float)i;
testSetA[i] = (simd_float3){
value + (0 + randomFloatFromNegOneToPosOne()),
value - (500 + randomFloatFromNegOneToPosOne()),
value * (2.5 + randomFloatFromNegOneToPosOne())
};
testSetB[i] = (simd_float3){
value + (500 + randomFloatFromNegOneToPosOne()),
-value * (10.0 + randomFloatFromNegOneToPosOne()),
value / (2.0 + randomFloatFromNegOneToPosOne())
};
}

[self measureBlock:^{
for (int i = 0; i < kIterationCount; ++i) {
simd_float3 result;
result = testSetA[i] + testSetB[i];
result = testSetA[i] - testSetB[i];
result = testSetA[i] * testSetB[i];
result = testSetA[i] / testSetB[i];
(void)result;
bool resultB;
resultB = simd_all(testSetA[i] < testSetB[i]);
resultB = simd_all(testSetA[i] <= testSetB[i]);
resultB = simd_all(testSetA[i] == testSetB[i]);
(void)resultB;
}
}];

free(testSetA);
free(testSetB);
}

@end
129 changes: 129 additions & 0 deletions Tests/VucktPerformanceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Vuckt
// @author: Slipp Douglas Thompson
// @license: Public Domain per The Unlicense. See accompanying LICENSE file or <http://unlicense.org/>.

import XCTest
import Vuckt
import simd
import SceneKit



class VucktPerformanceTests : XCTestCase
{
static let iterationCount = 1_000_000


override func setUp()
{
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown()
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
}


func testFloat3Performance()
{
var testSetA:[Float3] = []
var testSetB:[Float3] = []
(0..<Self.iterationCount).forEach{ i in
let value = Float(i)
testSetA.append(Float3(
x: value,
y: value - 500,
z: value * 2.5
))
testSetB.append(Float3(
x: value + 500,
y: -value * 10.0,
z: value / 2.0
))
}


self.measure {
(0..<Self.iterationCount).forEach{ i in
_ = testSetA[i] + testSetB[i]
_ = testSetA[i] - testSetB[i]
_ = testSetA[i] * testSetB[i]
_ = testSetA[i] / testSetB[i]
_ = testSetA[i] < testSetB[i]
_ = testSetA[i] <= testSetB[i]
_ = testSetA[i] == testSetB[i]
}
}
}

private func randomFloatFromNegOneToPosOne() -> Float {
let randomFloat = Float(arc4random() % (UInt32(RAND_MAX) + 1)) / Float(RAND_MAX)
return randomFloat * 2.0 - 1.0;
}

func testSIMDFloat3Performance()
{
var testSetA:[simd.float3] = []
var testSetB:[simd.float3] = []
(0..<Self.iterationCount).forEach{ i in
let value = Float(i)
testSetA.append(simd.float3(
x: value + (0 + randomFloatFromNegOneToPosOne()),
y: value - (500 + randomFloatFromNegOneToPosOne()),
z: value * (2.5 + randomFloatFromNegOneToPosOne())
))
testSetB.append(simd.float3(
x: value + (500 + randomFloatFromNegOneToPosOne()),
y: -value * (10.0 + randomFloatFromNegOneToPosOne()),
z: value / (2.0 + randomFloatFromNegOneToPosOne())
))
}


self.measure {
(0..<Self.iterationCount).forEach{ i in
_ = testSetA[i] + testSetB[i]
_ = testSetA[i] - testSetB[i]
_ = testSetA[i] * testSetB[i]
_ = testSetA[i] / testSetB[i]
_ = all(testSetA[i] .< testSetB[i])
_ = all(testSetA[i] .<= testSetB[i])
_ = testSetA[i] == testSetB[i]
}
}
}

func testGLKVector3Performance()
{
var testSetA:[GLKVector3] = []
var testSetB:[GLKVector3] = []
(0..<Self.iterationCount).forEach{ i in
let value = Float(i)
testSetA.append(GLKVector3Make(
value,
value - 500,
value * 2.5
))
testSetB.append(GLKVector3Make(
value + 500,
-value * 10.0,
value / 2.0
))
}


self.measure {
(0..<Self.iterationCount).forEach{ i in
_ = GLKVector3Add(testSetA[i], testSetB[i])
_ = GLKVector3Subtract(testSetA[i], testSetB[i])
_ = GLKVector3Multiply(testSetA[i], testSetB[i])
_ = GLKVector3Divide(testSetA[i], testSetB[i])
_ = GLKVector3AllGreaterThanVector3(testSetB[i], testSetA[i])
_ = GLKVector3AllGreaterThanOrEqualToVector3(testSetB[i], testSetA[i])
_ = GLKVector3AllEqualToVector3(testSetA[i], testSetB[i])
}
}
}

}
9 changes: 0 additions & 9 deletions Tests/VucktTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,4 @@ class VucktTests : XCTestCase
}
}
}

func testPerformanceExample()
{
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
22 changes: 21 additions & 1 deletion Vuckt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
FA32AEAC20BD1C4C008A2111 /* Int2.h in Headers */ = {isa = PBXBuildFile; fileRef = FA9E2CD41EAD6053005C5402 /* Int2.h */; settings = {ATTRIBUTES = (Public, ); }; };
FA32AEAF20BD1C4C008A2111 /* Int3.mm in Sources */ = {isa = PBXBuildFile; fileRef = FAC5EDCB1E550B1900E116B3 /* Int3.mm */; };
FA32AEB020BD1C4C008A2111 /* Int2.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA9E2CD51EAD608C005C5402 /* Int2.mm */; };
FA38107F246CF763005FAC15 /* VucktCPerformanceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = FA38107E246CF763005FAC15 /* VucktCPerformanceTests.m */; };
FA381080246CF763005FAC15 /* VucktCPerformanceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = FA38107E246CF763005FAC15 /* VucktCPerformanceTests.m */; };
FA3E5E4921B7C33C0073FA6D /* Int3+_ObjectiveCBridgeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3E5E4621B7C33C0073FA6D /* Int3+_ObjectiveCBridgeable.swift */; };
FA3E5E4B21B7C33C0073FA6D /* Int2+_ObjectiveCBridgeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3E5E4721B7C33C0073FA6D /* Int2+_ObjectiveCBridgeable.swift */; };
FA3E5E4C21B7C3870073FA6D /* Int2.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD0BF0620CFBC2200261EE4 /* Int2.swift */; };
Expand All @@ -35,6 +37,8 @@
FA3E5E8621B8A0180073FA6D /* Float4+_ObjectiveCBridgeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3E5E8221B8A0180073FA6D /* Float4+_ObjectiveCBridgeable.swift */; };
FA3E5E8821B8A0180073FA6D /* Float2+_ObjectiveCBridgeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3E5E8321B8A0180073FA6D /* Float2+_ObjectiveCBridgeable.swift */; };
FA3E5E8A21B8A0180073FA6D /* Float3+_ObjectiveCBridgeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3E5E8421B8A0180073FA6D /* Float3+_ObjectiveCBridgeable.swift */; };
FA499D22295C880D007FD580 /* VucktPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA499D21295C8804007FD580 /* VucktPerformanceTests.swift */; };
FA499D23295C880D007FD580 /* VucktPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA499D21295C8804007FD580 /* VucktPerformanceTests.swift */; };
FA4F6C4224370683009A8579 /* Float3x3.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA4F6C4124370683009A8579 /* Float3x3.mm */; };
FA4F6C4424371B29009A8579 /* Float3x3.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA4F6C4324371B29009A8579 /* Float3x3.swift */; };
FA4F6C4824371D0A009A8579 /* Float3x3+_ObjectiveCBridgeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA4F6C4724371D0A009A8579 /* Float3x3+_ObjectiveCBridgeable.swift */; };
Expand Down Expand Up @@ -97,6 +101,7 @@
FA35362823F50789000B5EC9 /* Float2_NoObjCBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Float2_NoObjCBridge.swift; sourceTree = "<group>"; };
FA35362923F50792000B5EC9 /* Float3_NoObjCBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Float3_NoObjCBridge.swift; sourceTree = "<group>"; };
FA35362A23F5079A000B5EC9 /* Float4_NoObjCBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Float4_NoObjCBridge.swift; sourceTree = "<group>"; };
FA38107E246CF763005FAC15 /* VucktCPerformanceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VucktCPerformanceTests.m; sourceTree = "<group>"; };
FA3E5E4421B7C2170073FA6D /* Int2+_ObjectiveCBridgeable.swift.gyb */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Int2+_ObjectiveCBridgeable.swift.gyb"; sourceTree = "<group>"; };
FA3E5E4521B7C2980073FA6D /* Int3+_ObjectiveCBridgeable.swift.gyb */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Int3+_ObjectiveCBridgeable.swift.gyb"; sourceTree = "<group>"; };
FA3E5E4621B7C33C0073FA6D /* Int3+_ObjectiveCBridgeable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Int3+_ObjectiveCBridgeable.swift"; path = "Int3/Int3+_ObjectiveCBridgeable.swift"; sourceTree = "<group>"; };
Expand All @@ -121,6 +126,7 @@
FA3E5E8221B8A0180073FA6D /* Float4+_ObjectiveCBridgeable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Float4+_ObjectiveCBridgeable.swift"; path = "Float4/Float4+_ObjectiveCBridgeable.swift"; sourceTree = "<group>"; };
FA3E5E8321B8A0180073FA6D /* Float2+_ObjectiveCBridgeable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Float2+_ObjectiveCBridgeable.swift"; path = "Float2/Float2+_ObjectiveCBridgeable.swift"; sourceTree = "<group>"; };
FA3E5E8421B8A0180073FA6D /* Float3+_ObjectiveCBridgeable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Float3+_ObjectiveCBridgeable.swift"; path = "Float3/Float3+_ObjectiveCBridgeable.swift"; sourceTree = "<group>"; };
FA499D21295C8804007FD580 /* VucktPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VucktPerformanceTests.swift; sourceTree = "<group>"; };
FA4F6C402437066F009A8579 /* Float3x3.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Float3x3.h; sourceTree = "<group>"; };
FA4F6C4124370683009A8579 /* Float3x3.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Float3x3.mm; sourceTree = "<group>"; };
FA4F6C4324371B29009A8579 /* Float3x3.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Float3x3.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -308,8 +314,10 @@
FAED6C2C21B32E370083A2B5 /* Tests */ = {
isa = PBXGroup;
children = (
FAED6C2D21B32E370083A2B5 /* VucktTests.swift */,
FAED6C2F21B32E370083A2B5 /* Info.plist */,
FAED6C2D21B32E370083A2B5 /* VucktTests.swift */,
FA499D21295C8804007FD580 /* VucktPerformanceTests.swift */,
FA38107E246CF763005FAC15 /* VucktCPerformanceTests.m */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -446,11 +454,13 @@
};
FAED6C2A21B32E370083A2B5 = {
CreatedOnToolsVersion = 10.1;
LastSwiftMigration = 1140;
ProvisioningStyle = Automatic;
};
FAFB401621B34F2700D4A8B5 = {
CreatedOnToolsVersion = 10.1;
DevelopmentTeam = UQQE2H6P34;
LastSwiftMigration = 1140;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -533,6 +543,8 @@
buildActionMask = 2147483647;
files = (
FAED6C2E21B32E370083A2B5 /* VucktTests.swift in Sources */,
FA38107F246CF763005FAC15 /* VucktCPerformanceTests.m in Sources */,
FA499D22295C880D007FD580 /* VucktPerformanceTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -541,6 +553,8 @@
buildActionMask = 2147483647;
files = (
FAFB401F21B34F4A00D4A8B5 /* VucktTests.swift in Sources */,
FA381080246CF763005FAC15 /* VucktCPerformanceTests.m in Sources */,
FA499D23295C880D007FD580 /* VucktPerformanceTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -651,6 +665,8 @@
SDKROOT = macosx;
SUPPORTED_PLATFORMS = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGET_NAME = VucktTests;
};
name = Debug;
Expand Down Expand Up @@ -687,6 +703,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SUPPORTED_PLATFORMS = macosx;
SWIFT_VERSION = 5.0;
TARGET_NAME = VucktTests;
};
name = Release;
Expand Down Expand Up @@ -727,6 +744,8 @@
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = VucktTests;
};
Expand Down Expand Up @@ -764,6 +783,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = VucktTests;
VALIDATE_PRODUCT = YES;
Expand Down
9 changes: 0 additions & 9 deletions Vuckt.xcodeproj/xcshareddata/xcschemes/Vuckt.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FA32AEA920BD1C4C008A2111"
BuildableName = "IntN.framework"
BlueprintName = "IntN - Swift 4"
ReferencedContainer = "container:IntN.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand Down

0 comments on commit 32dd81d

Please sign in to comment.