0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir

ระบุคอขวดด้านประสิทธิภาพในโค้ด Elixir ด้วยเครื่องมือวัดประสิทธิภาพและเทคนิคการทำโพรไฟล์

การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Performance: Speed & Efficiency

In software development, performance refers to how fast and efficiently your application runs. It's crucial for a good user experience and managing server resources.

Slow applications can frustrate users, leading to abandonment. For backend systems, poor performance can mean higher infrastructure costs or an inability to handle user loads.

  • Responsiveness: How quickly the system responds to user input.
  • Throughput: How many operations it can handle over time.
  • Resource Usage: How much CPU, memory, or network it consumes.

What is Benchmarking?

Benchmarking is the process of measuring the performance of a piece of code or a system under specific conditions. It helps you understand how fast different parts of your code execute.

You often use benchmarking to compare different implementations of the same logic. For example, which way of processing a list is faster? By running them many times and averaging the results, you get reliable data.

Simple Manual Timing

Elixir provides basic tools to measure execution time. We can use System.monotonic_time/0 and System.convert_time_unit/3 to get a rough idea.

Let's try timing a simple list operation. The time is given in native units, then converted to microseconds.

defmodule MyTimer do
  def run_and_time(fun) do
    start_time = System.monotonic_time(:nanosecond)
    result = fun.()
    end_time = System.monotonic_time(:nanosecond)
    duration = System.convert_time_unit(end_time - start_time, :nanosecond, :microsecond)
    IO.puts "Function returned: #{inspect(result)}"
    IO.puts "Execution took: #{duration} µs"
  end

  def example_task do
    1..1_000_000 |> Enum.map(fn x -> x * 2 end)
  end
end

# To run this in an IEx session or script:
MyTimer.run_and_time(fn -> MyTimer.example_task() end)

Meet Benchee: Elixir's Benchmarker

While manual timing is useful, it's not robust enough for serious benchmarking. Factors like garbage collection, CPU load, and warm-up times can skew results.

Benchee is a popular Elixir library designed for accurate and reliable benchmarking. It runs your code many times, calculates statistics, and presents clear results.

  • Handles warm-up periods.
  • Performs statistical analysis (average, standard deviation).
  • Compares multiple functions easily.

First Benchee Benchmark

To use Benchee, you'd typically add {:benchee, "~> 1.0", only: :dev} to your mix.exs dependencies and run mix deps.get.

Here's how you define a simple benchmark. This code would usually be in a file like bench/my_benchmark.exs and run with mix bench.

defmodule MyBenchmarks do
  use Benchee.Benchmark

  def run do
    Benchee.run %{
      "list_sum" => fn -> Enum.sum(1..10_000) end
    },
    time: 1,
    memory_time: 0.1, # Shorten for quick example
    print: [fast_warning: false]
  end
end

# To run this directly (after adding Benchee to mix.exs and running mix deps.get):
# MyBenchmarks.run()

Comparing Implementations

One of Benchee's strengths is comparing different approaches. Let's benchmark two ways to append an element to a list: using ++ (concatenation) vs. [new_element | list] (prepense, then reverse if order matters).

For appending to the *end* effectively, ++ is common, but prepending is often faster. Benchee helps confirm this.

defmodule ListAppendBenchmarks do
  use Benchee.Benchmark

  def run do
    long_list = Enum.to_list(1..10_000)
    new_element = 10_001

    Benchee.run %{
      "append_with_++" => fn -> long_list ++ [new_element] end,
      "prepend_and_reverse" => fn -> [new_element | long_list] |> Enum.reverse() end
    },
    time: 1,
    memory_time: 0.1
  end
end

# ListAppendBenchmarks.run()

Deciphering Benchee Results

When Benchee runs, it outputs a table of statistics:

  • ips (iterations per second): How many times the function can execute in one second. Higher is better.
  • average: The average execution time per iteration. Lower is better.
  • std dev (standard deviation): How much the execution times vary. A lower standard deviation means more consistent results.
  • median: The middle value of all execution times.
  • memory usage: How much memory the operation consumes.

Focus on ips and average for speed, and std dev for consistency.

What is Profiling?

While benchmarking tells you how fast your code is, profiling tells you where your code is spending its time. It helps pinpoint specific functions or lines of code that are bottlenecks.

A profiler typically tracks:

  • CPU time: Which functions consume the most processing power.
  • Memory usage: Which parts allocate the most memory.
  • Function calls: The call stack, showing who calls whom.

This is crucial for optimizing, as you want to focus your efforts on the slowest parts.

Basic Profiling with :eprof

Elixir, running on the Erlang VM, has access to Erlang's powerful built-in profiler, :eprof. It helps you find CPU hotspots in your code.

You typically use :eprof interactively in an IEx session. It measures the execution time of functions called within a profiled block.

defmodule MyProfiledCode do
  def slow_function(n) do
    Enum.map(1..n, fn x -> :math.pow(x, 0.5) |> round end)
  end

  def entry_point do
    slow_function(5_000)
  end
end

# To profile in IEx:
# :eprof.start_profiling()
# MyProfiledCode.entry_point()
# :eprof.stop_profiling()
# :eprof.analyze()

Reading :eprof Reports

After running :eprof.analyze(), you'll get a report showing:

  • Function calls: How many times each function was called.
  • Total time: The total CPU time spent in that function and its children.
  • Self time: The CPU time spent directly in that function, excluding calls to other functions.

Look for functions with high 'self time' or 'total time' as potential bottlenecks. This indicates where the most work is being done.

Performance Tool Check

You've learned about both benchmarking and profiling. Now, let's test your understanding!

Summary: Optimize Your Code

Congratulations! You've explored essential tools for understanding and improving your Elixir application's performance.

  • Benchmarking (with Benchee) measures how fast code runs and compares different implementations.
  • Profiling (with :eprof) identifies where your code spends most of its time, pinpointing bottlenecks.

By using these techniques, you can write more efficient, faster, and scalable Elixir applications. Keep practicing to make performance analysis a regular part of your development workflow!

คำถามที่พบบ่อย

บทเรียน “การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir”

ระบุคอขวดด้านประสิทธิภาพในโค้ด Elixir ด้วยเครื่องมือวัดประสิทธิภาพและเทคนิคการทำโพรไฟล์ คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การวัดประสิทธิภาพและการทำโพรไฟล์ Elixir
  2. การตรวจติดตามด้วย Telemetry และเมตริก
  3. การจัดการข้อผิดพลาดและการบันทึกล็อกแบบมีโครงสร้าง
  4. การติดตามแบบกระจายด้วย OpenTelemetry
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development