private int getBalanceScore(Map<String, List<TopicPartition>> assignment) {
            int score = 0;

            Map<String, Integer> consumer2AssignmentSize = new HashMap<>();
            for (Entry<String, List<TopicPartition>> entry: assignment.entrySet())
                consumer2AssignmentSize.put(entry.getKey(), entry.getValue().size());

            Iterator<Entry<String, Integer>> it = consumer2AssignmentSize.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Integer> entry = it.next();
                int consumerAssignmentSize = entry.getValue();
                it.remove();
                for (Entry<String, Integer> otherEntry: consumer2AssignmentSize.entrySet())
                    score += Math.abs(consumerAssignmentSize - otherEntry.getValue());
            }

            return score;
        }
@return the balance score of the given assignment, as the sum of assigned partitions size difference of all consumer pairs.
A perfectly balanced assignment (with all consumers getting the same number of partitions) has a balance score of 0.
Lower balance score indicates a more balanced assignment.
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractStickyAssignor.java