ContainerBenchmark.vala 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * ContainerBenchmark - Benchmarks for Container entity operations
  3. *
  4. * Measures performance of container creation, child enumeration,
  5. * and deletion operations.
  6. *
  7. * @version 0.1
  8. * @since 0.1
  9. */
  10. namespace Implexus.Tools.Perf {
  11. /**
  12. * Benchmark for Container entity operations.
  13. *
  14. * Tests:
  15. * - create_container: Creating new containers
  16. * - get_children: Enumerating container children
  17. * - get_child: Getting individual child by name
  18. * - delete_container: Deleting containers (tracked for deferred cleanup)
  19. */
  20. public class ContainerBenchmark : Benchmark {
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public override string name { get { return "Container"; } }
  25. /**
  26. * Creates a new ContainerBenchmark.
  27. *
  28. * @param engine The engine to benchmark
  29. * @param config The benchmark configuration
  30. */
  31. public ContainerBenchmark(Core.Engine engine, BenchmarkConfig config) {
  32. base(engine, config);
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public override async void run_async(Results results) throws Error {
  38. var parent_path = new Core.EntityPath("/perf-test");
  39. var containers_path = parent_path.append_child("containers");
  40. // Ensure parent container exists
  41. yield ensure_container_async(parent_path, "perf-test");
  42. _cleanup_tracker.add_container(parent_path.to_string());
  43. // Create a dedicated container for our tests
  44. yield cleanup_path_async(containers_path);
  45. var root = yield _engine.get_root_async();
  46. var perf_test = yield root.get_child_async("perf-test");
  47. var containers = yield perf_test.create_container_async("containers");
  48. _cleanup_tracker.add_container(containers_path.to_string());
  49. int iterations = _config.iterations;
  50. // Test 1: Create containers
  51. print(" Testing create_container (%d iterations)...\n", iterations);
  52. var create_op = new CreateContainerOperation(this, containers_path, containers);
  53. var create_result = yield measure_async("create_container", iterations, create_op);
  54. results.add(name, create_result);
  55. // Test 2: Get container children
  56. print(" Testing get_children (%d iterations)...\n", iterations);
  57. var get_op = new GetChildrenOperation(containers);
  58. var get_result = yield measure_async("get_children", iterations, get_op);
  59. results.add(name, get_result);
  60. // Test 3: Get individual child by name
  61. print(" Testing get_child (%d iterations)...\n", iterations);
  62. var get_child_op = new GetChildOperation(containers, iterations);
  63. var get_child_result = yield measure_async("get_child", iterations, get_child_op);
  64. results.add(name, get_child_result);
  65. // Test 4: Delete containers (still tracked for deferred cleanup)
  66. print(" Testing delete_container (%d iterations)...\n", iterations);
  67. var delete_op = new DeleteContainerOperation(this, _engine, containers_path);
  68. var delete_result = yield measure_async("delete_container", iterations, delete_op);
  69. results.add(name, delete_result);
  70. }
  71. }
  72. // Operation classes for ContainerBenchmark
  73. private class CreateContainerOperation : AsyncOperation {
  74. private ContainerBenchmark _benchmark;
  75. private Core.EntityPath _containers_path;
  76. private Core.Entity _containers;
  77. public CreateContainerOperation(ContainerBenchmark benchmark, Core.EntityPath containers_path, Core.Entity containers) {
  78. _benchmark = benchmark;
  79. _containers_path = containers_path;
  80. _containers = containers;
  81. }
  82. public override async void execute_async() throws Error {
  83. var container_name = "container-%d".printf(iteration);
  84. var container_path = _containers_path.append_child(container_name);
  85. // Clean up before creating to ensure consistent state
  86. yield _benchmark.cleanup_path_async(container_path);
  87. yield _containers.create_container_async(container_name);
  88. // Track for deferred cleanup
  89. _benchmark._cleanup_tracker.add_container(container_path.to_string());
  90. }
  91. }
  92. private class GetChildrenOperation : AsyncOperation {
  93. private Core.Entity _containers;
  94. public GetChildrenOperation(Core.Entity containers) {
  95. _containers = containers;
  96. }
  97. public override async void execute_async() throws Error {
  98. var children = yield _containers.get_children_async();
  99. // Force enumeration by counting
  100. int count = 0;
  101. foreach (var child in children) {
  102. count++;
  103. }
  104. }
  105. }
  106. private class GetChildOperation : AsyncOperation {
  107. private Core.Entity _containers;
  108. private int _total_iterations;
  109. public GetChildOperation(Core.Entity containers, int total_iterations) {
  110. _containers = containers;
  111. _total_iterations = total_iterations;
  112. }
  113. public override async void execute_async() throws Error {
  114. var container_name = "container-%d".printf(iteration % _total_iterations);
  115. var child = yield _containers.get_child_async(container_name);
  116. }
  117. }
  118. private class DeleteContainerOperation : AsyncOperation {
  119. private ContainerBenchmark _benchmark;
  120. private Core.Engine _engine;
  121. private Core.EntityPath _containers_path;
  122. public DeleteContainerOperation(ContainerBenchmark benchmark, Core.Engine engine, Core.EntityPath containers_path) {
  123. _benchmark = benchmark;
  124. _engine = engine;
  125. _containers_path = containers_path;
  126. }
  127. public override async void execute_async() throws Error {
  128. var container_name = "container-%d".printf(iteration);
  129. var path = _containers_path.append_child(container_name);
  130. var entity = yield _engine.get_entity_or_null_async(path);
  131. if (entity != null) {
  132. yield ((!) entity).delete_async();
  133. }
  134. }
  135. }
  136. }