• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nshkrdotcom / ElixirScope / 997dd201c48cfc5a93ab9d8799c4f839ab1728ae

29 May 2025 07:59PM UTC coverage: 58.374% (-0.05%) from 58.426%
997dd201c48cfc5a93ab9d8799c4f839ab1728ae

push

github

NSHkr
refactor performance_optimizer

0 of 159 new or added lines in 6 files covered. (0.0%)

14 existing lines in 1 file now uncovered.

6082 of 10419 relevant lines covered (58.37%)

3184.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/elixir_scope/ast_repository/performance_optimizer.ex
1
# ==============================================================================
2
# Main Performance Optimizer Coordinator
3
# ==============================================================================
4

5
defmodule ElixirScope.ASTRepository.PerformanceOptimizer do
6
  @moduledoc """
7
  Main coordinator for performance optimization features.
8

9
  Delegates specialized optimization tasks to focused components while
10
  maintaining a unified interface for the repository system.
11
  """
12

13
  use GenServer
14
  require Logger
15

16
  alias ElixirScope.ASTRepository.{MemoryManager, EnhancedRepository}
17
  alias ElixirScope.ASTRepository.Enhanced.{EnhancedModuleData, EnhancedFunctionData}
18
  alias ElixirScope.ASTRepository.PerformanceOptimizer.{
19
    CacheManager,
20
    BatchProcessor,
21
    LazyLoader,
22
    OptimizationScheduler,
23
    StatisticsCollector
24
  }
25

26
  defstruct [
27
    :optimization_stats,
28
    :cache_stats,
29
    :batch_stats,
30
    :lazy_loading_stats,
31
    :enabled
32
  ]
33

34
  @type optimization_stats :: %{
35
    modules_optimized: non_neg_integer(),
36
    functions_optimized: non_neg_integer(),
37
    cache_optimizations: non_neg_integer(),
38
    memory_optimizations: non_neg_integer(),
39
    query_optimizations: non_neg_integer(),
40
    total_time_saved_ms: non_neg_integer()
41
  }
42

43
  # GenServer API
44

45
  def start_link(opts \\ []) do
46
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
×
47
  end
48

49
  def init(opts) do
50
    # Initialize components
NEW
51
    {:ok, _} = CacheManager.start_link(opts)
×
NEW
52
    {:ok, _} = BatchProcessor.start_link(opts)
×
NEW
53
    {:ok, _} = LazyLoader.start_link(opts)
×
NEW
54
    {:ok, _} = OptimizationScheduler.start_link(opts)
×
NEW
55
    {:ok, _} = StatisticsCollector.start_link(opts)
×
56

UNCOV
57
    state = %__MODULE__{
×
58
      optimization_stats: StatisticsCollector.init_optimization_stats(),
59
      cache_stats: StatisticsCollector.init_cache_stats(),
60
      batch_stats: StatisticsCollector.init_batch_stats(),
61
      lazy_loading_stats: StatisticsCollector.init_lazy_loading_stats(),
62
      enabled: Keyword.get(opts, :enabled, true)
63
    }
64

65
    Logger.info("PerformanceOptimizer started with optimizations enabled: #{state.enabled}")
×
66
    {:ok, state}
67
  end
68

69
  # Public API
70

71
  @doc """
72
  Optimizes module storage with intelligent caching and batching.
73
  """
74
  @spec store_module_optimized(atom(), term(), keyword()) :: {:ok, EnhancedModuleData.t()} | {:error, term()}
75
  def store_module_optimized(module_name, ast, opts \\ []) do
76
    GenServer.call(__MODULE__, {:store_module_optimized, module_name, ast, opts})
×
77
  end
78

79
  @doc """
80
  Optimizes function storage with CFG/DFG lazy loading.
81
  """
82
  @spec store_function_optimized(atom(), atom(), non_neg_integer(), term(), keyword()) ::
83
    {:ok, EnhancedFunctionData.t()} | {:error, term()}
84
  def store_function_optimized(module_name, function_name, arity, ast, opts \\ []) do
85
    GenServer.call(__MODULE__, {:store_function_optimized, module_name, function_name, arity, ast, opts})
×
86
  end
87

88
  @doc """
89
  Performs batch storage operations for multiple modules.
90
  """
91
  @spec store_modules_batch([{atom(), term()}], keyword()) :: {:ok, [EnhancedModuleData.t()]} | {:error, term()}
92
  def store_modules_batch(modules, opts \\ []) do
NEW
93
    BatchProcessor.process_modules(modules, opts)
×
94
  end
95

96
  @doc """
97
  Retrieves module with intelligent caching.
98
  """
99
  @spec get_module_optimized(atom()) :: {:ok, EnhancedModuleData.t()} | {:error, term()}
100
  def get_module_optimized(module_name) do
NEW
101
    CacheManager.get_module_cached(module_name)
×
102
  end
103

104
  @doc """
105
  Retrieves function with lazy analysis loading.
106
  """
107
  @spec get_function_optimized(atom(), atom(), non_neg_integer()) :: {:ok, EnhancedFunctionData.t()} | {:error, term()}
108
  def get_function_optimized(module_name, function_name, arity) do
NEW
109
    LazyLoader.get_function_lazy(module_name, function_name, arity)
×
110
  end
111

112
  @doc """
113
  Performs optimized analysis queries with result caching.
114
  """
115
  @spec query_analysis_optimized(atom(), map()) :: {:ok, term()} | {:error, term()}
116
  def query_analysis_optimized(query_type, params) do
117
    GenServer.call(__MODULE__, {:query_analysis_optimized, query_type, params})
×
118
  end
119

120
  @doc """
121
  Warms up caches with frequently accessed data.
122
  """
123
  @spec warm_caches() :: :ok
124
  def warm_caches() do
NEW
125
    CacheManager.warm_caches()
×
126
  end
127

128
  @doc """
129
  Optimizes ETS table structures and indexes.
130
  """
131
  @spec optimize_ets_tables() :: :ok
132
  def optimize_ets_tables() do
NEW
133
    OptimizationScheduler.optimize_ets_tables()
×
134
  end
135

136
  @doc """
137
  Gets comprehensive optimization statistics.
138
  """
139
  @spec get_optimization_stats() :: {:ok, map()}
140
  def get_optimization_stats() do
141
    GenServer.call(__MODULE__, :get_optimization_stats)
×
142
  end
143

144
  @doc """
145
  Enables or disables performance optimizations.
146
  """
147
  @spec set_optimization_enabled(boolean()) :: :ok
148
  def set_optimization_enabled(enabled) do
149
    GenServer.call(__MODULE__, {:set_optimization_enabled, enabled})
×
150
  end
151

152
  # GenServer Callbacks
153

154
  def handle_call({:store_module_optimized, module_name, ast, opts}, _from, state) do
155
    if state.enabled do
×
156
      start_time = System.monotonic_time(:microsecond)
×
157

158
      try do
×
UNCOV
159
        lazy_analysis = Keyword.get(opts, :lazy_analysis, true)
×
160
        batch_mode = Keyword.get(opts, :batch_mode, false)
×
161

162
        result = if batch_mode do
×
NEW
163
          BatchProcessor.queue_module(module_name, ast, opts)
×
164
          {:ok, :queued_for_batch}
165
        else
UNCOV
166
          store_module_with_optimizations(module_name, ast, lazy_analysis)
×
167
        end
168

UNCOV
169
        end_time = System.monotonic_time(:microsecond)
×
170
        duration = end_time - start_time
×
171

NEW
172
        new_stats = StatisticsCollector.update_optimization_stats(state.optimization_stats, :module_storage, duration)
×
UNCOV
173
        new_state = %{state | optimization_stats: new_stats}
×
174

175
        {:reply, result, new_state}
×
176
      rescue
177
        error ->
×
178
          Logger.error("Optimized module storage failed: #{inspect(error)}")
×
179
          {:reply, {:error, {:optimization_failed, error}}, state}
×
180
      end
181
    else
UNCOV
182
      result = EnhancedRepository.store_enhanced_module(module_name, ast, opts)
×
183
      {:reply, result, state}
×
184
    end
185
  end
186

187
  def handle_call({:store_function_optimized, module_name, function_name, arity, ast, opts}, _from, state) do
188
    if state.enabled do
×
189
      start_time = System.monotonic_time(:microsecond)
×
190

191
      try do
×
NEW
192
        result = LazyLoader.store_function_lazy(module_name, function_name, arity, ast, opts)
×
193

194
        end_time = System.monotonic_time(:microsecond)
×
195
        duration = end_time - start_time
×
196

NEW
197
        new_stats = StatisticsCollector.update_optimization_stats(state.optimization_stats, :function_storage, duration)
×
UNCOV
198
        new_state = %{state | optimization_stats: new_stats}
×
199

200
        {:reply, result, new_state}
×
201
      rescue
202
        error ->
×
203
          Logger.error("Optimized function storage failed: #{inspect(error)}")
×
204
          {:reply, {:error, {:optimization_failed, error}}, state}
×
205
      end
206
    else
UNCOV
207
      result = EnhancedRepository.store_enhanced_function(module_name, function_name, arity, ast, opts)
×
208
      {:reply, result, state}
×
209
    end
210
  end
211

212
  def handle_call({:query_analysis_optimized, query_type, params}, _from, state) do
213
    if state.enabled do
×
214
      start_time = System.monotonic_time(:microsecond)
×
215

NEW
216
      result = CacheManager.query_with_cache(query_type, params)
×
217

218
      end_time = System.monotonic_time(:microsecond)
×
219
      duration = end_time - start_time
×
220

NEW
221
      new_stats = StatisticsCollector.update_optimization_stats(state.optimization_stats, :query_optimization, duration)
×
UNCOV
222
      new_state = %{state | optimization_stats: new_stats}
×
223

224
      {:reply, result, new_state}
×
225
    else
UNCOV
226
      result = EnhancedRepository.query_analysis(query_type, params)
×
227
      {:reply, result, state}
×
228
    end
229
  end
230

231
  def handle_call(:get_optimization_stats, _from, state) do
232
    stats = %{
×
233
      optimization: state.optimization_stats,
×
234
      cache: state.cache_stats,
×
235
      batch: state.batch_stats,
×
236
      lazy_loading: state.lazy_loading_stats,
×
237
      enabled: state.enabled
×
238
    }
239

240
    {:reply, {:ok, stats}, state}
×
241
  end
242

243
  def handle_call({:set_optimization_enabled, enabled}, _from, state) do
244
    new_state = %{state | enabled: enabled}
×
245
    Logger.info("Performance optimizations #{if enabled, do: "enabled", else: "disabled"}")
×
246
    {:reply, :ok, new_state}
×
247
  end
248

249
  # Private helpers
250
  defp store_module_with_optimizations(module_name, ast, lazy_analysis) do
UNCOV
251
    optimized_ast = preprocess_ast_for_storage(ast)
×
252

UNCOV
253
    immediate_analysis = if lazy_analysis do
×
254
      [:basic_metrics, :dependencies]
255
    else
256
      [:all]
257
    end
258

UNCOV
259
    opts = [analysis_level: immediate_analysis, optimized: true]
×
260
    EnhancedRepository.store_enhanced_module(module_name, optimized_ast, opts)
×
261
  end
262

263
  defp preprocess_ast_for_storage(ast) do
264
    # Optimize AST structure for storage
UNCOV
265
    ast
×
266
  end
267
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc