// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include #include namespace metrics { struct LoadTypeSetImpl : public LoadTypeSet { LoadTypeSetImpl() { push_back(LoadType(0, "default")); } LoadTypeSetImpl& add(uint32_t id, const char* name) { push_back(LoadType(id, name)); return *this; } const LoadType& operator[](const std::string& name) const { for (uint32_t i=0; i metric( loadTypes, LongValueMetric("put", "", "Put")); } namespace { struct MyMetricSet : public MetricSet { LongAverageMetric metric; MyMetricSet(MetricSet* owner = 0) : MetricSet("tick", "", "", owner), metric("tack", "", "", this) { } MetricSet* clone(std::vector &ownerList, CopyType copyType, MetricSet* owner, bool includeUnused = false) const override { if (copyType != CLONE) { return MetricSet::clone(ownerList, copyType, owner, includeUnused); } MyMetricSet * myset = new MyMetricSet(owner); myset->assignValues(*this); std::cerr << "org:" << this->toString(true) << std::endl; std::cerr << "clone:" << myset->toString(true) << std::endl; return myset; } }; } void LoadMetricTest::testClone(Metric::CopyType copyType) { LoadTypeSetImpl loadTypes; loadTypes.add(32, "foo").add(1000, "bar"); MetricSet top("top", "", ""); MyMetricSet myset; LoadMetric metric(loadTypes, myset, &top); metric[loadTypes["foo"]].metric.addValue(5); std::vector ownerList; MetricSet::UP copy(dynamic_cast(top.clone(ownerList, copyType, 0, true))); CPPUNIT_ASSERT(copy.get()); std::string expected = "top:\n" " tick:\n" " sum:\n" " tack average=5 last=5 min=5 max=5 count=1 total=5\n" " default:\n" " tack average=0 last=0 count=0 total=0\n" " foo:\n" " tack average=5 last=5 min=5 max=5 count=1 total=5\n" " bar:\n" " tack average=0 last=0 count=0 total=0"; CPPUNIT_ASSERT_EQUAL(expected, std::string(top.toString(true))); CPPUNIT_ASSERT_EQUAL(expected, std::string(copy->toString(true))); } void LoadMetricTest::testAdding() { LoadTypeSetImpl loadTypes; loadTypes.add(32, "foo").add(1000, "bar"); MetricSet top("top", "", ""); MyMetricSet myset; LoadMetric metric(loadTypes, myset, &top); metric[loadTypes["foo"]].metric.addValue(5); std::vector ownerList; MetricSet::UP copy(dynamic_cast( top.clone(ownerList, Metric::INACTIVE, 0, false))); CPPUNIT_ASSERT(copy.get()); top.reset(); top.addToSnapshot(*copy, ownerList); std::string expected = "top:\n" " tick:\n" " sum:\n" " tack average=5 last=5 min=5 max=5 count=1 total=5\n" " foo:\n" " tack average=5 last=5 min=5 max=5 count=1 total=5"; CPPUNIT_ASSERT_EQUAL(expected, std::string(copy->toString(true))); } } // documentapi