Sub topic 18
Benchmarking changes fairly
This chapter assumes you can read a query plan and the Query Profile, and that you know the tuning levers from earlier chapters: Photon, OPTIMIZE, clustering, statistics, join hints and built-in functions. Here you learn to prove that one of those changes helped.
Why single runs mislead
A query's first run is often slower than later runs. The disk cache is cold, the cluster may still be starting executors, and Photon or Spark has not yet loaded the files. A second run of the same query can look much faster for reasons unrelated to your change. Other users on the same cluster add noise too.
Common benchmarking errors:
- Comparing a cold run of the old version with a warm run of the new version.
- Changing two things at once, such as enabling Photon and running OPTIMIZE.
- Running on different data, cluster sizes or runtime versions.
- Trusting one measurement.
- Hitting the query result cache and measuring a cache lookup instead of the query.
A fair-comparison method
- Write down one hypothesis and change only one thing.
- Keep the data, cluster size, node type and Databricks Runtime version identical for both runs. For a Photon test, create two otherwise identical clusters that differ only in
runtime_engine. - Use a dedicated cluster or warehouse with no other load.
- Decide up front whether you are measuring cold or warm performance, and apply that choice to both versions.
- Run a warm-up pass and discard it. Then run the query at least five times.
- Record the median and the spread, not the best run.
- Compare more than elapsed time. Check bytes read, files read, spill and shuffle size in the Query Profile.
Metrics like files read explain why a change worked. If duration drops but files read is unchanged, something else caused the gain.
A small timing harness
This Python notebook code times a query several times and reports the median. It calls .collect() so the work runs fully.
import statistics
import time
spark.conf.set("spark.databricks.io.cache.enabled", "true")
def time_query(sql_text, runs=5, warmup=1):
for _ in range(warmup):
spark.sql(sql_text).collect()
durations = []
for _ in range(runs):
start = time.perf_counter()
spark.sql(sql_text).collect()
durations.append(time.perf_counter() - start)
return durations
query = """
SELECT c.region, SUM(o.amount) AS total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY c.region
"""
times = time_query(query)
print("runs:", [round(t, 2) for t in times])
print("median:", round(statistics.median(times), 2), "s")
print("range:", round(min(times), 2), "-", round(max(times), 2), "s")Run it once per variant, with the same cluster configuration, and compare the medians. If the ranges overlap heavily, the difference may be noise.
Judging whether it is worth it
A faster query is not automatically a better choice. Weigh the speedup against cost, as covered in the sizing chapter. A Photon cluster that finishes in half the time but costs more per hour only wins if the total cost of the job falls. Compute cost as runtime multiplied by hourly rate, and compare that figure across variants.
Exercise
You enable Photon on a cluster and re-run a query. It takes 40 seconds, down from 90 seconds on the previous run. Name two reasons this result may not prove Photon helped, and one step to make the comparison fair.
Show answer
The earlier run may have been cold (empty disk cache, starting executors) while the later one was warm, and the result cache may have returned a stored answer. Other load on the cluster or a single measurement could also explain it. To make it fair, run both variants on identical clusters that differ only in runtime_engine, discard a warm-up run, disable the result cache, run each at least five times, and compare medians and the cost of each run.