/** * @breif Add a new value into histogram_s */ void hsAdd(histogram_s *h, int64_t value) { int index = histogramGetIndex(value); int count = 1; h->total_value += value; h->total_count += count; efiAssertVoid(index < BOUND_LENGTH, "histogram issue"); h->values[index] += count; }
TEST(util, histogram) { print("******************************************* testHistogram\r\n"); initHistogramsModule(); ASSERT_EQ(80, histogramGetIndex(239)); ASSERT_EQ(223, histogramGetIndex(239239)); ASSERT_EQ(364, histogramGetIndex(239239239)); histogram_s h; initHistogram(&h, "test"); int result[5]; ASSERT_EQ(0, hsReport(&h, result)); hsAdd(&h, 10); ASSERT_EQ(1, hsReport(&h, result)); ASSERT_EQ(10, result[0]); // let's add same value one more time hsAdd(&h, 10); ASSERT_EQ(2, hsReport(&h, result)); ASSERT_EQ(10, result[0]); ASSERT_EQ(10, result[1]); hsAdd(&h, 10); hsAdd(&h, 10); hsAdd(&h, 10); hsAdd(&h, 1000); hsAdd(&h, 100); ASSERT_EQ(5, hsReport(&h, result)); ASSERT_EQ(5, result[0]); ASSERT_EQ(10, result[1]); ASSERT_EQ(10, result[2]); ASSERT_EQ(100, result[3]); // values are not expected to be exactly the same, it's the shape what matters ASSERT_EQ(1011, result[4]); }