Skip to content

Commit

Permalink
test: memory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjl-mux committed Mar 15, 2024
1 parent 172079e commit 52a0242
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
19822A762A4CA69700CFA822 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19822A642A4CA69700CFA822 /* ContentView.swift */; };
19822A772A4CA69700CFA822 /* UploadCTA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19822A662A4CA69700CFA822 /* UploadCTA.swift */; };
19822A792A4CA69700CFA822 /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19822A682A4CA69700CFA822 /* ImagePicker.swift */; };
1991ADBB2BA442A400C3DF5E /* MemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1991ADBA2BA442A400C3DF5E /* MemoryTests.swift */; };
19DCD95B2A4CA567001FBBF6 /* MuxUploadSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 19DCD95A2A4CA567001FBBF6 /* MuxUploadSDK */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -57,6 +58,7 @@
19822A682A4CA69700CFA822 /* ImagePicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = "<group>"; };
19822A7B2A4CA6A300CFA822 /* SwiftUploadSDKExampleUITests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftUploadSDKExampleUITests.swift; sourceTree = "<group>"; };
19822A7C2A4CA6A300CFA822 /* SwiftUploadSDKExampleLaunchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftUploadSDKExampleLaunchTests.swift; sourceTree = "<group>"; };
1991ADBA2BA442A400C3DF5E /* MemoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryTests.swift; sourceTree = "<group>"; };
19DCD9592A4CA546001FBBF6 /* swift-upload-sdk */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "swift-upload-sdk"; path = ../..; sourceTree = "<group>"; };
358E3C7729A92167005261CB /* SwiftUploadSDKExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUploadSDKExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
358E3C9129A92168005261CB /* SwiftUploadSDKExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftUploadSDKExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -165,6 +167,7 @@
children = (
19822A7B2A4CA6A300CFA822 /* SwiftUploadSDKExampleUITests.swift */,
19822A7C2A4CA6A300CFA822 /* SwiftUploadSDKExampleLaunchTests.swift */,
1991ADBA2BA442A400C3DF5E /* MemoryTests.swift */,
);
path = SwiftUploadSDKExampleTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -328,6 +331,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1991ADBB2BA442A400C3DF5E /* MemoryTests.swift in Sources */,
191C59C52A69C9AC00D3AF05 /* SwiftUploadSDKExampleLaunchTests.swift in Sources */,
191C59C42A69C9A900D3AF05 /* SwiftUploadSDKExampleUITests.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//
// SwiftUploadSDKExampleUnitTests.swift
// SwiftUploadSDKExampleUnitTests
//
// Created by Tomislav Kordic on 22.2.24..
//

import XCTest
@testable import MuxUploadSDK

import MuxUploadSDK

final class MemoryTests: XCTestCase {

private let myServerBackend = FakeBackend(urlSession: URLSession(configuration: URLSessionConfiguration.default))

func memoryFootprint() -> mach_vm_size_t? {
// The `TASK_VM_INFO_COUNT` and `TASK_VM_INFO_REV1_COUNT` macros are too
// complex for the Swift C importer, so we have to define them ourselves.
let TASK_VM_INFO_COUNT = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size)
let TASK_VM_INFO_REV1_COUNT = mach_msg_type_number_t(MemoryLayout.offset(of: \task_vm_info_data_t.min_address)! / MemoryLayout<integer_t>.size)
var info = task_vm_info_data_t()
var count = TASK_VM_INFO_COUNT
let kr = withUnsafeMutablePointer(to: &info) { infoPtr in
infoPtr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { intPtr in
task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), intPtr, &count)
}
}
guard
kr == KERN_SUCCESS,
count >= TASK_VM_INFO_REV1_COUNT
else { return nil }
return info.phys_footprint
}

func getUploadFilePath() -> URL? {
let bundle = Bundle(for: MemoryTests.self)
let fileManager = FileManager.default
let cwd = bundle.bundlePath
guard let content = try? fileManager.contentsOfDirectory(atPath: cwd) else {
XCTFail("five_min.mov file not found")
return nil
}
guard let videoURL = try? bundle.url(forResource: "five_min", withExtension: "mov") else {
XCTFail("five_min.mov file not found")
return nil
}
return videoURL
}

func testChunkWorkerMemoryUsage() async throws {
let chunkSizeInBytes = 6 * 1024 * 1024
let videoURL = getUploadFilePath()
let uploadURL = try await self.myServerBackend.createDirectUpload()
let chunkedFile = ChunkedFile(chunkSize: chunkSizeInBytes)
try chunkedFile.openFile(fileURL: videoURL!)
try chunkedFile.seekTo(byte: 0)
let startMemory = memoryFootprint()
repeat {
let chunk = try chunkedFile.readNextChunk().get()
if (chunk.size() == 0) {
break;
}
let chunkProgress = Progress(totalUnitCount: Int64(chunk.size()))
let worker = ChunkWorker(
uploadURL: uploadURL,
chunkProgress: chunkProgress,
maxRetries: 3
)
try await worker.directUpload(chunk: chunk)
} while (true)
let endMemory = memoryFootprint()
if ((startMemory! * 2) < endMemory!) {
XCTFail("We have mem leak, started with \(startMemory!) bytes, ended up with \(endMemory!) bytes")
}
}

func testChunkedFileMemoryUsage() throws {
let videoURL = getUploadFilePath()
let chunkSizeInBytes = 6 * 1024 * 1024
let chunkedFile = ChunkedFile(chunkSize: chunkSizeInBytes)
try chunkedFile.openFile(fileURL: videoURL!)
try chunkedFile.seekTo(byte: 0)
let startMemory = memoryFootprint()
repeat {
let chunk = try chunkedFile.readNextChunk().get()
Swift.print("Got chunk at position: \(chunk.startByte)")
if (chunk.size() == 0) {
break;
}
} while (true)
let endMemory = memoryFootprint()
if ((startMemory! * 2) < endMemory!) {
XCTFail("We have mem leak, started with \(startMemory!) bytes, ended up with \(endMemory!) bytes")
}
}

func testLargeUpload() async throws {
// Construct custom upload options to upload a file in 6MB chunks
let chunkSizeInBytes = 6 * 1024 * 1024
let options = DirectUploadOptions(
inputStandardization: .skipped,
chunkSizeInBytes: chunkSizeInBytes,
retryLimitPerChunk: 5
)
let putURL = try await self.myServerBackend.createDirectUpload()
let videoURL = getUploadFilePath()

let muxDirectUpload = DirectUpload(
uploadURL: putURL,
inputFileURL: videoURL!,
options: options
)

muxDirectUpload.progressHandler = { state in
// TODO: print progress, print memory usage
Swift.print("Upload progress: " + (state.progress?.fractionCompleted.description)!)
}
let expectation = XCTestExpectation(description: "Upload task done(completed or failed)")
muxDirectUpload.resultHandler = { result in
switch result {
case .success(let success):
Swift.print("File uploaded successfully ")
expectation.fulfill()
case .failure(let error):
Swift.print("Failed to upload file")
expectation.fulfill()
}
}
Swift.print("Starting upload video")
muxDirectUpload.start()

let result = await XCTWaiter().fulfillment(of: [expectation], timeout: 9000.0)
switch result {
// case .completed: XCTAssertEqual(muxDirectUpload.complete, true)
case .timedOut: do {
Swift.print("Test timedout after 9000 seconds !!!")
XCTFail()
}
default: do {
Swift.print("Test default option is to fail !!!")
XCTFail()
}
}
Swift.print("All done !!!")
}

}

0 comments on commit 52a0242

Please sign in to comment.