### Question:
ServiceHeight implements ModelRunner { public static Map<String, Integer> calculate(Graph graph) { TraceTraversal<Vertex, Vertex> leafs = graph.traversal(TraceTraversalSource.class) .leafSpans(); Map<String, Integer> heights = new LinkedHashMap<>(); while (leafs.hasNext()) { int height = 0; Vertex node = leafs.next(); Vertex parent = Util.parent(node); Span nodeSpan = GraphCreator.toSpan(node); Integer heightNode = heights.get(nodeSpan.serviceName); if (heightNode == null) { heights.put(nodeSpan.serviceName, height); } while (parent != null) { Span parentSpan = GraphCreator.toSpan(parent); if (!nodeSpan.serviceName.equals(parentSpan.serviceName)) { height++; } heightNode = heights.get(parentSpan.serviceName); if (heightNode == null || height > heightNode) { heights.put(parentSpan.serviceName, height); } node = parent; nodeSpan = GraphCreator.toSpan(node); parent = Util.parent(parent); } } return heights; }  @Override void runWithMetrics(Graph graph); static Map<String, Integer> calculate(Graph graph);  }### Answer:
@Test public void calculateOnlyRoot() { Span root = Util.newTrace("root", "root"); Trace trace = new Trace(); trace.spans = Arrays.asList(root); Graph graph = GraphCreator.create(trace); Map<String, Integer> depths = ServiceHeight.calculate(graph); Assert.assertEquals(1, depths.size()); Assert.assertEquals(new Integer(0), depths.get("root")); }
@Test public void calculate() { Span root = Util.newTrace("root", "root"); Span child = Util.newChild("child", "child", root); Span child2 = Util.newChild("child2", "child2", root); Span child2Child = Util.newChild("child2", "child2Child", child2); Span child2ChildChild = Util.newChild("child2ChildChild", "child2ChildChild", child2Child); Span childChild = Util.newChild("childChild", "childChild", child); Span childChildChild = Util.newChild("childChild", "childChildChild", childChild); Trace trace = new Trace(); trace.spans = Arrays.asList(root, child, child2, childChild, child2Child, childChildChild, child2ChildChild); Graph graph = GraphCreator.create(trace); Map<String, Integer> depths = ServiceHeight.calculate(graph); Assert.assertEquals(5, depths.size()); Assert.assertEquals(new Integer(2), depths.get("root")); Assert.assertEquals(new Integer(1), depths.get("child")); Assert.assertEquals(new Integer(1), depths.get("child2")); Assert.assertEquals(new Integer(0), depths.get("childChild")); Assert.assertEquals(new Integer(0), depths.get("child2ChildChild")); }