0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · درس

تقنيات تحليل أداء Erlang

استخدم أدوات تحليل الأداء المضمّنة في Erlang لتحديد اختناقات الأداء وتحسين شيفرة تطبيقك

تقنيات تحليل أداء Erlang درس مجاني في Erlang OTP: Distributed & Fault-Tolerant Systems Programming على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Erlang OTP: Distributed & Fault-Tolerant Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Erlang OTP: Distributed & Fault-Tolerant Systems Programming 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Profile Erlang Code?

Ever wonder why your Erlang application feels a bit sluggish? That's where profiling comes in!

Profiling is like giving your code an X-ray. It helps you:

  • Spot performance bottlenecks.
  • Understand how functions spend their time.
  • Optimize resource usage (CPU, memory).

In Erlang, profiling is crucial for building efficient, high-performance systems.

Meet `fprof`: CPU & Memory

One of Erlang's most powerful built-in profiling tools is fprof. It's designed to give you detailed insights into how your program uses system resources.

fprof can profile:

  • CPU usage: Which functions are consuming the most processing time?
  • Memory usage: How much memory each function allocates.

It's great for deep dives into specific parts of your code.

Profiling CPU with `fprof`

Let's see fprof in action! This example creates a CPU-intensive calculation and then uses fprof to measure where the time is spent.

Run this code and observe the output, which will be the fprof report.

-module(cpu_profiler).
-export([run/0, long_calculation/1]).

% A function designed to consume CPU time
long_calculation(N) ->
    lists:foldl(fun(I, Acc) -> math:sqrt(I) + Acc end, 0.0, lists:seq(1, N)).

run() ->
    io:format("~n--- Starting fprof CPU profiling ---~n"),
    fprof:start([cpu]), % Start profiling for CPU
    _Result = long_calculation(100000), % Call the function to profile
    fprof:stop(), % Stop collecting data
    fprof:profile(), % Process collected data
    io:format("~n--- fprof CPU Report ---~n"),
    fprof:analyse({dest, user}), % Print the analysis to the console
    io:format("--- End fprof CPU Report ---~n"),
    ok.

Deciphering `fprof` Reports

The fprof report can look a bit intimidating at first! Here are the key columns to focus on:

  • acc (Accumulated): Total time spent in a function, including time in functions it calls.
  • self (Self time): Time spent directly in this function, excluding time in functions it calls. This helps pinpoint the exact bottleneck.
  • calls: How many times the function was called.

Look for functions with high self times to find areas for optimization.

`fprof` for Memory Usage

While we focused on CPU, fprof can also help with memory usage. By calling fprof:start([memory]), you can track memory allocation per function.

Memory profiling helps identify "memory leaks" or functions that allocate excessively large data structures, which can be critical for long-running systems.

The report structure is similar, but focuses on bytes allocated rather than CPU cycles.

`eprof`: Time-Based Profiling

Another useful tool is eprof. While fprof is very detailed, eprof provides a simpler, more high-level overview of execution times.

eprof is excellent for quickly identifying which functions take the longest to run, without the deep call-graph analysis of fprof.

It's often used for a quick check before diving into more detailed profiling.

Running an `eprof` Test

Let's use eprof to measure the execution time of a list manipulation operation. This gives a clear picture of how long the function itself takes.

Run this example and check the output for execution statistics.

-module(time_profiler).
-export([run/0, quick_operation/1]).

% A function that processes a list
quick_operation(N) ->
    lists:map(fun(I) -> I * 2 end, lists:seq(1, N)).

run() ->
    io:format("~n--- Starting eprof execution time profiling ---~n"),
    eprof:start(), % Start eprof
    _Result = quick_operation(50000), % Call the function
    eprof:stop(), % Stop collecting data
    io:format("~n--- eprof Report ---~n"),
    eprof:log({dest, user}), % Print the log to console
    eprof:stop_profiling(), % Clean up eprof
    io:format("--- End eprof Report ---~n"),
    ok.

Interpreting `eprof` Statistics

eprof output is simpler than fprof. It typically shows you:

  • {, , }: The function being reported.
  • {calls, N}: How many times this function was called.
  • {total, Time}: The total execution time for all calls to this function.
  • {average, Time}: The average execution time per call.

This helps you quickly see which functions are cumulatively taking the most time.

Basic Tracing with `dbg`

While primarily a debugging tool, dbg can also be used for basic tracing to see function calls in real-time. It's less about performance metrics and more about understanding flow.

This example shows how to set a trace on a function and then call it. The trace output typically appears directly in the shell as the code runs.

-module(dbg_example).
-export([run/0, simple_func/1]).

simple_func(X) ->
    io:format("Inside simple_func with ~p~n", [X]),
    X * 2.

run() ->
    io:format("~n--- Starting dbg trace for simple_func ---~n"),
    dbg:tracer(), % Start the tracer process
    dbg:p(all, call), % Trace all process calls (optional, but good for context)
    dbg:tp(dbg_example, simple_func, []), % Set trace pattern on simple_func/1
    io:format("Calling simple_func(5)...~n"),
    _Result = simple_func(5),
    io:format("Calling simple_func(10)...~n"),
    _Result2 = simple_func(10),
    dbg:stop_clear(), % Stop tracing and clear patterns
    io:format("--- dbg trace finished ---~n"),
    ok.

Profiling Tool Check

You've learned about Erlang's powerful profiling tools. Now, let's test your understanding!

Consider a scenario where you suspect a specific function is causing high CPU load due to complex calculations within its own body, rather than in functions it calls.

Profiling Power-Up!

Great job! You've taken your first steps into Erlang profiling.

We covered:

  • fprof: For detailed CPU and memory profiling, using self time to pinpoint bottlenecks.
  • eprof: For a high-level overview of function execution times (total and average).
  • dbg: A brief look at its use for basic function call tracing to understand program flow.

These tools are your allies in building efficient and robust Erlang applications. Keep exploring them!

الأسئلة الشائعة

هل درس «تقنيات تحليل أداء Erlang» مجاني؟

نعم — نص درس «تقنيات تحليل أداء Erlang» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Erlang OTP: Distributed & Fault-Tolerant Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Erlang OTP: Distributed & Fault-Tolerant Systems Programming 4 دروس في المجموع.

ماذا ستتعلم في «تقنيات تحليل أداء Erlang»؟

استخدم أدوات تحليل الأداء المضمّنة في Erlang لتحديد اختناقات الأداء وتحسين شيفرة تطبيقك تتمرن على Erlang OTP: Distributed & Fault-Tolerant Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Erlang OTP: Distributed & Fault-Tolerant Systems Programming؟

لا تُشترط خبرة سابقة. Erlang OTP: Distributed & Fault-Tolerant Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «تقنيات تحليل أداء Erlang»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Erlang OTP: Distributed & Fault-Tolerant Systems Programming هذا؟

نعم. كل درس في Erlang OTP: Distributed & Fault-Tolerant Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تقنيات تحليل أداء Erlang
  2. تتبّع الأنظمة الموزعة وتصحيح أخطائها
  3. تكامل المقاييس والمراقبة
  4. تحليل الذاكرة وضبط جمع البيانات المهملة
← العودة إلى Erlang OTP: Distributed & Fault-Tolerant Systems Programming