public double calculateAveragePercentageUsedBy(NormalizedResources used, double totalMemoryMb, double usedMemoryMb) {
        int skippedResourceTypes = 0;
        double total = 0.0;
        if (usedMemoryMb > totalMemoryMb) {
            throwBecauseUsedIsNotSubsetOfTotal(used, totalMemoryMb, usedMemoryMb);
        }
        if (totalMemoryMb != 0.0) {
            total += usedMemoryMb / totalMemoryMb;
        } else {
            skippedResourceTypes++;
        }
        double totalCpu = getTotalCpu();
        if (used.getTotalCpu() > getTotalCpu()) {
            throwBecauseUsedIsNotSubsetOfTotal(used, totalMemoryMb, usedMemoryMb);
        }
        if (totalCpu != 0.0) {
            total += used.getTotalCpu() / getTotalCpu();
        } else {
            skippedResourceTypes++;
        }

        if (used.otherResources.length > otherResources.length) {
            throwBecauseUsedIsNotSubsetOfTotal(used, totalMemoryMb, usedMemoryMb);
        }

        for (int i = 0; i < otherResources.length; i++) {
            double totalValue = otherResources[i];
            double usedValue;
            if (i >= used.otherResources.length) {
                //Resources missing from used are using none of that resource
                usedValue = 0.0;
            } else {
                usedValue = used.otherResources[i];
            }
            if (usedValue > totalValue) {
                throwBecauseUsedIsNotSubsetOfTotal(used, totalMemoryMb, usedMemoryMb);
            }
            if (totalValue == 0.0) {
                //Skip any resources where the total is 0, the percent used for this resource isn't meaningful.
                //We fall back to prioritizing by cpu, memory and any other resources by ignoring this value
                skippedResourceTypes++;
                continue;
            }

            total += usedValue / totalValue;
        }
        //Adjust the divisor for the average to account for any skipped resources (those where the total was 0)
        int divisor = 2 + otherResources.length - skippedResourceTypes;
        if (divisor == 0) {
            /*
             * This is an arbitrary choice to make the result consistent with calculateMin. Any value would be valid here, becase there are
             * no (non-zero) resources in the total set of resources, so we're trying to average 0 values.
             */
            return 100.0;
        } else {
            return (total * 100.0) / divisor;
        }
    }
@Test
    public void testCalculateAvgThrowsIfTotalIsMissingCpu() {
        NormalizedResources resources = new NormalizedResources(normalize(Collections.singletonMap(Constants.COMMON_CPU_RESOURCE_NAME, 2)));
        NormalizedResources usedResources = new NormalizedResources(normalize(Collections.singletonMap(Constants.COMMON_CPU_RESOURCE_NAME, 5)));

        assertThrows(IllegalArgumentException.class, () ->
                resources.calculateAveragePercentageUsedBy(usedResources, 0, 0));
    }