summaryrefslogtreecommitdiff
path: root/ocaml/test/test_multicore.ml
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-06 23:18:59 +0700
committerpolwex <polwex@sortug.com>2025-10-06 23:18:59 +0700
commit5de3f7a3ad7b0cf63b4a6cbddfc1e26359dea161 (patch)
treeb55b2258123149bed40bd89bbaa58e7da54f3a26 /ocaml/test/test_multicore.ml
parentfdab65f6dac4ba85ed4749f61970660d1132d453 (diff)
cleaned up tests
Diffstat (limited to 'ocaml/test/test_multicore.ml')
-rw-r--r--ocaml/test/test_multicore.ml204
1 files changed, 0 insertions, 204 deletions
diff --git a/ocaml/test/test_multicore.ml b/ocaml/test/test_multicore.ml
deleted file mode 100644
index e2a846b..0000000
--- a/ocaml/test/test_multicore.ml
+++ /dev/null
@@ -1,204 +0,0 @@
-(* Multi-Core State Tests - Demonstrating true parallelism with OCaml 5
- *
- * Tests:
- * 1. Concurrent event increments across domains
- * 2. Parallel read-only queries (peek)
- * 3. Domain-safe state mutations
- *
- * This is THE breakthrough - proving that Urbit can run on multiple cores!
- *)
-
-open Nock_lib
-
-(* Test concurrent event increments across multiple domains *)
-let test_concurrent_increments _env =
- Printf.printf "Test: Concurrent event increments across domains...\n";
-
- let state = State.create () in
-
- (* Number of domains to spawn *)
- let num_domains = 4 in
- let increments_per_domain = 1000 in
-
- Printf.printf " Spawning %d domains, %d increments each\n"
- num_domains increments_per_domain;
-
- (* Spawn multiple domains, each incrementing the counter *)
- let domains = List.init num_domains (fun i ->
- Domain.spawn (fun () ->
- Printf.printf " Domain %d starting...\n" i;
- for _j = 1 to increments_per_domain do
- let _ = State.inc_event state in
- ()
- done;
- Printf.printf " Domain %d done!\n" i;
- ()
- )
- ) in
-
- (* Wait for all domains to complete *)
- List.iter Domain.join domains;
-
- (* Check final count *)
- let final_count = State.event_num state in
- let expected = Int64.of_int (num_domains * increments_per_domain) in
-
- Printf.printf " Final count: %Ld (expected %Ld)\n" final_count expected;
-
- if final_count = expected then
- Printf.printf " āœ“ All increments completed correctly!\n\n"
- else
- failwith (Printf.sprintf "Count mismatch! Got %Ld, expected %Ld"
- final_count expected)
-
-(* Test parallel read-only queries (peek) *)
-let test_parallel_reads _env =
- Printf.printf "Test: Parallel read-only queries...\n";
-
- let state = State.create () in
-
- (* Set up a kernel state *)
- let kernel = Noun.cell (Noun.atom 42) (Noun.atom 99) in
- State.boot state kernel;
-
- let num_domains = 8 in
- let reads_per_domain = 100 in
-
- Printf.printf " Spawning %d domains, %d reads each\n"
- num_domains reads_per_domain;
-
- (* Spawn domains that all read the state in parallel *)
- let domains = List.init num_domains (fun i ->
- Domain.spawn (fun () ->
- for _j = 1 to reads_per_domain do
- let result = State.peek state (Noun.atom 0) in
- match result with
- | Some _noun ->
- (* Peek returns [path kernel], not just kernel
- * For now, just verify it returns something *)
- ()
- | None ->
- failwith (Printf.sprintf "Domain %d peek failed!" i)
- done;
- i (* Return domain id *)
- )
- ) in
-
- (* Wait for all reads *)
- let results = List.map Domain.join domains in
-
- Printf.printf " Completed %d reads across %d domains\n"
- (num_domains * reads_per_domain) (List.length results);
- Printf.printf " āœ“ All parallel reads successful!\n\n"
-
-(* Test mixed read/write workload *)
-let test_mixed_workload _env =
- Printf.printf "Test: Mixed read/write workload...\n";
-
- let state = State.create () in
- let kernel = Noun.atom 100 in
- State.boot state kernel;
-
- let num_readers = 4 in
- let num_writers = 2 in
- let ops_per_domain = 500 in
-
- Printf.printf " %d reader domains + %d writer domains\n"
- num_readers num_writers;
-
- (* Spawn reader domains *)
- let readers = List.init num_readers (fun _i ->
- Domain.spawn (fun () ->
- for _j = 1 to ops_per_domain do
- let _ = State.peek state (Noun.atom 0) in
- ()
- done
- )
- ) in
-
- (* Spawn writer domains *)
- let writers = List.init num_writers (fun _i ->
- Domain.spawn (fun () ->
- for _j = 1 to ops_per_domain do
- let _ = State.inc_event state in
- ()
- done
- )
- ) in
-
- (* Wait for all domains *)
- List.iter Domain.join readers;
- List.iter Domain.join writers;
-
- (* Verify final state *)
- let final_count = State.event_num state in
- let expected = Int64.of_int (num_writers * ops_per_domain) in
-
- Printf.printf " Final event count: %Ld (expected %Ld)\n" final_count expected;
-
- if final_count = expected then
- Printf.printf " āœ“ Mixed workload completed correctly!\n\n"
- else
- failwith "Mixed workload count mismatch!"
-
-(* Benchmark: measure parallel speedup *)
-let test_parallel_speedup _env =
- Printf.printf "Test: Parallel speedup benchmark...\n";
-
- let total_ops = 10000 in
-
- (* Sequential baseline *)
- Printf.printf " Sequential baseline (%d ops)...\n" total_ops;
- let state_seq = State.create () in
- let start_seq = Unix.gettimeofday () in
- for _i = 1 to total_ops do
- let _ = State.inc_event state_seq in
- ()
- done;
- let time_seq = Unix.gettimeofday () -. start_seq in
- Printf.printf " Time: %.4f seconds\n" time_seq;
-
- (* Parallel with 4 domains *)
- let num_domains = 4 in
- let ops_per_domain = total_ops / num_domains in
- Printf.printf " Parallel with %d domains (%d ops each)...\n"
- num_domains ops_per_domain;
-
- let state_par = State.create () in
- let start_par = Unix.gettimeofday () in
-
- let domains = List.init num_domains (fun _i ->
- Domain.spawn (fun () ->
- for _j = 1 to ops_per_domain do
- let _ = State.inc_event state_par in
- ()
- done
- )
- ) in
-
- List.iter Domain.join domains;
- let time_par = Unix.gettimeofday () -. start_par in
- Printf.printf " Time: %.4f seconds\n" time_par;
-
- let speedup = time_seq /. time_par in
- Printf.printf " Speedup: %.2fx\n" speedup;
-
- if speedup > 1.0 then
- Printf.printf " āœ“ Parallel execution is faster!\n\n"
- else
- Printf.printf " Note: Speedup < 1x (mutex overhead dominates on this small workload)\n\n"
-
-let () =
- Eio_main.run @@ fun env ->
- Printf.printf "\nšŸš€ === MULTI-CORE URBIT RUNTIME TESTS === šŸš€\n\n";
- Printf.printf "OCaml %s with %d domains available\n\n"
- Sys.ocaml_version (Domain.recommended_domain_count ());
-
- test_concurrent_increments env;
- test_parallel_reads env;
- test_mixed_workload env;
- test_parallel_speedup env;
-
- Printf.printf "šŸŽ‰ === ALL MULTI-CORE TESTS PASSED! === šŸŽ‰\n";
- Printf.printf "\nThis is THE breakthrough: Urbit can now run on multiple CPU cores!\n";
- Printf.printf "Phase 1 (Event Log + State) complete. Ready for Phase 2 (Parallel Nock)!\n"