Skip to content

Commit a6abafb

Browse files
committed
test: structure test scripts into tests directory and ignore artifacts
1 parent 64df241 commit a6abafb

9 files changed

Lines changed: 100 additions & 0 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ DerivedData/
1414
.vscode/
1515
.idea/
1616
mlx-swift-lm
17+
18+
# Temporary Artifacts & Logs
19+
*.log
20+
*.metallib
21+
*.pid
22+
curl_out.txt
23+
sample.txt

tests/measure_speed.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
import urllib.request
3+
import json
4+
5+
print("Starting inference request...", flush=True)
6+
start = time.time()
7+
try:
8+
data = json.dumps({
9+
"model": "mlx-community/Qwen3.5-122B-A10B-4bit",
10+
"messages": [{"role": "user", "content": "Say hi"}],
11+
"max_tokens": 5
12+
}).encode('utf-8')
13+
req = urllib.request.Request(
14+
"http://127.0.0.1:5413/v1/chat/completions",
15+
data=data,
16+
headers={"Content-Type": "application/json"}
17+
)
18+
with urllib.request.urlopen(req, timeout=600) as response:
19+
result = response.read().decode('utf-8')
20+
duration = time.time() - start
21+
print(f"Status Code: {response.status}")
22+
print(f"Response: {result}")
23+
print(f"Duration: {duration:.2f} seconds")
24+
except Exception as e:
25+
duration = time.time() - start
26+
print(f"Request failed: {e}")
27+
print(f"Failed after {duration:.2f} seconds")

tests/test_assign.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import MLX; var a = MLXArray.zeros([5, 2]); a[MLXArray([0, 2])] = MLXArray.ones([2, 2]); print(a)

tests/test_children.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
3+
func sumBytes(_ p: Progress) -> Int64 {
4+
if p.children.isEmpty {
5+
return p.completedUnitCount
6+
}
7+
return p.children.reduce(0) { $0 + sumBytes($1) }
8+
}

tests/test_inference.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
echo "=== Starting inference at $(date) ==="
3+
START=$(python3 -c 'import time; print(time.time())')
4+
curl -sf --max-time 600 -X POST http://127.0.0.1:5413/v1/chat/completions \
5+
-H "Content-Type: application/json" \
6+
-d '{"model":"mlx-community/Qwen3.5-122B-A10B-4bit","messages":[{"role":"user","content":"Say hi"}],"max_tokens":5}' > curl_out.txt
7+
RC=$?
8+
END=$(python3 -c 'import time; print(time.time())')
9+
DUR=$(python3 -c "print(f'{$END - $START:.2f}')")
10+
echo "=== Done at $(date) ==="
11+
echo "cURL Exit code: $RC"
12+
echo "Duration: $DUR seconds"
13+
cat curl_out.txt
14+
echo ""

tests/test_leaf.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import MLXNN; class MLP: Module { @ModuleInfo(key: "proj") var proj = Linear(10, 10) }; let p = MLP(); print(p.leafModules().flattened().count)

tests/test_mirror.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
@propertyWrapper
3+
struct ModuleInfo<Value> {
4+
var wrappedValue: Value
5+
var key: String
6+
}
7+
class SwitchGLU {}
8+
class MLP {
9+
@ModuleInfo(key: "switch_mlp") var switchMLP: SwitchGLU = SwitchGLU()
10+
}
11+
let m = MLP()
12+
let mirror = Mirror(reflecting: m)
13+
for child in mirror.children { print(child.label!) }
14+

tests/test_progress.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Foundation
2+
3+
let p = Progress()
4+
print("throughput:", p.throughput ?? "nil")
5+
print("userInfo:", p.userInfo)

tests/test_speed.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
3+
func getSystemDownloadBytes() -> UInt64 {
4+
var ifaddr: UnsafeMutablePointer<ifaddrs>?
5+
guard getifaddrs(&ifaddr) == 0 else { return 0 }
6+
defer { freeifaddrs(ifaddr) }
7+
8+
var total: UInt64 = 0
9+
var ptr = ifaddr
10+
while let p = ptr {
11+
if p.pointee.ifa_addr != nil, p.pointee.ifa_addr.pointee.sa_family == UInt8(AF_LINK) {
12+
let data = p.pointee.ifa_data?.bindMemory(to: if_data.self, capacity: 1)
13+
total += UInt64(data?.pointee.ifi_ibytes ?? 0)
14+
}
15+
ptr = p.pointee.ifa_next
16+
}
17+
return total
18+
}
19+
20+
let start = getSystemDownloadBytes()
21+
Thread.sleep(forTimeInterval: 1.0)
22+
let end = getSystemDownloadBytes()
23+
print("Speed: \((Double(end - start) / 1048576.0)) MB/s")

0 commit comments

Comments
 (0)