Connect via MCP →

Enter Calculation

Formula

Advertisement

Results

Bug Density (per KLOC)
2.5
bugs/KLOC
Test Density (per LOC) 0.05 tests/LOC
Test Density (per KLOC) 50 tests/KLOC
Bug Density (per LOC) 0.0025 bugs/LOC
Bug Density (per KLOC) 2.5 bugs/KLOC
Normalized Code Size 10,000 LOC

What This Calculator Does

This tool computes two widely used software-engineering quality metrics: test density (how many test cases you have relative to the size of your codebase) and bug density (how many defects were found relative to the codebase size). Both are reported per LOC (Lines Of Code) and per KLOC (thousands of lines of code), since KLOC is the conventional industry reporting unit. It is a universal metric with no region-specific rules.

How to Use It

Enter the number of test cases executed, the number of bugs found, and the size of your source code. Choose whether the code-size figure is already in raw lines (LOC) or in thousands of lines (KLOC) — the calculator normalizes it to raw LOC before computing. All densities are then derived automatically.

The Formula Explained

First the code size is normalized: \(\text{locLines} = \text{loc} \times \text{factor}\), where the factor is 1 for LOC and 1000 for KLOC. Then

$$\text{Test Density per LOC} = \frac{\text{Test Count}}{\text{locLines}}, \qquad \text{Bug Density per LOC} = \frac{\text{Bug Count}}{\text{locLines}}$$

The per-KLOC versions simply multiply the per-LOC value by 1000. Test count and bug count are plain integer counts and are never scaled.

Diagram of bug density and test density as fractions over lines of code
Both metrics divide a count (bugs or tests) by code size, scaled to per-KLOC.

Worked Example

Suppose you ran 500 tests, found 25 bugs, and your codebase is 10 KLOC. Normalized size = \(10 \times 1000 = 10{,}000\) LOC. Test density = \(500 / 10{,}000 = 0.05\) tests/LOC, or 50 tests/KLOC. Bug density = \(25 / 10{,}000 = 0.0025\) bugs/LOC, or 2.5 bugs/KLOC.

FAQ

Why provide both per-LOC and per-KLOC? Per-LOC values are tiny and hard to read, so the industry conventionally reports per KLOC. They carry identical information — one is just the other times 1000.

Why is the result blank or an error? If LOC is zero, density is mathematically undefined (division by zero), so the calculator shows an error instead of an infinite value.

Can I compare these numbers between projects? Only if the LOC counting rules match. Decide consistently whether comment lines and blank lines are counted, because that choice materially changes the density values.

Last updated: