-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subscriber only benchmark and order test
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
- Loading branch information
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import NatsSwift | ||
import Foundation | ||
|
||
let nats = Client(url: URL(string: "nats://localhost:4222")!) | ||
print("Connecting...") | ||
try! await nats.connect() | ||
print("Connected!") | ||
|
||
let data = "foo".data(using: .utf8)! | ||
// Warmup | ||
print("Starting benchmark...") | ||
print("Waiting for first message...") | ||
var now = DispatchTime.now() | ||
let numMsgs = 1_000_000 | ||
let sub = try await nats.subscribe(to: "foo") | ||
var hm = HeaderMap() | ||
hm.append(try! HeaderName("foo"), HeaderValue("bar")) | ||
hm.append(try! HeaderName("foo"), HeaderValue("baz")) | ||
for i in 1...numMsgs { | ||
let msg = await sub.next() | ||
if i == 0 { | ||
print("Received first message! Starting the timer") | ||
now = DispatchTime.now() | ||
} | ||
guard let payload = msg?.payload else { | ||
print("empty payload!") | ||
continue | ||
} | ||
if String(data: payload, encoding: .utf8) != "\(i)" { | ||
print("invalid payload; expected: \(i); got: \(String(data: payload, encoding: .utf8))") | ||
} | ||
guard let headers = msg?.headers else { | ||
print("empty headers!") | ||
continue | ||
} | ||
if i%1000 == 0 { | ||
print("received \(i) msgs") | ||
} | ||
} | ||
|
||
let elapsed = DispatchTime.now().uptimeNanoseconds - now.uptimeNanoseconds | ||
let msgsPerSec: Double = Double(numMsgs)/(Double(elapsed)/1_000_000_000) | ||
print("Elapsed: \(elapsed / 1000000)ms") | ||
print("\(msgsPerSec) msgs/s") |