All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.acceptance.TestMetricMaker Maven / Gradle / Ivy

There is a newer version: 3.11.0-rc4
Show newest version
// Copyright (C) 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.acceptance;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.common.UsedAt;
import com.google.gerrit.metrics.Counter0;
import com.google.gerrit.metrics.Counter1;
import com.google.gerrit.metrics.Counter2;
import com.google.gerrit.metrics.Counter3;
import com.google.gerrit.metrics.Description;
import com.google.gerrit.metrics.DisabledMetricMaker;
import com.google.gerrit.metrics.Field;
import com.google.gerrit.metrics.Timer1;
import com.google.inject.Singleton;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.mutable.MutableLong;

/**
 * {@link com.google.gerrit.metrics.MetricMaker} to be bound in tests.
 *
 * 

Records how often counter metrics are invoked. Metrics of other types are not recorded. * *

Allows test to check how much a counter metrics is increased by an operation. * *

Example: * *

 * public class MyTest extends AbstractDaemonTest {
 *   {@literal @}Inject private TestMetricMaker testMetricMaker;
 *
 *   ...
 *
 *   {@literal @}Test
 *   public void testFoo() throws Exception {
 *     testMetricMaker.reset();
 *     doSomething();
 *     assertThat(testMetricMaker.getCount("foo/bar_count")).isEqualsTo(1);
 *   }
 * }
 * 
*/ @Singleton public class TestMetricMaker extends DisabledMetricMaker { private final ConcurrentHashMap counts = new ConcurrentHashMap<>(); private final ConcurrentHashMap timers = new ConcurrentHashMap<>(); public long getCount(String counterName, Object... fieldValues) { return getCounterValue(CounterKey.create(counterName, fieldValues)).longValue(); } public long getTimer(String timerName) { return getTimerValue(CounterKey.create(timerName)).longValue(); } public void reset() { counts.clear(); timers.clear(); } private MutableLong getCounterValue(CounterKey counterKey) { return counts.computeIfAbsent(counterKey, name -> new MutableLong(0)); } private MutableLong getTimerValue(CounterKey timerName) { return counts.computeIfAbsent(timerName, name -> new MutableLong(0)); } @Override public Counter0 newCounter(String name, Description desc) { return new Counter0() { @Override public void incrementBy(long value) { getCounterValue(CounterKey.create(name)).add(value); } @Override public void remove() {} }; } @Override @UsedAt(UsedAt.Project.PLUGIN_PULL_REPLICATION) public Timer1 newTimer(String name, Description desc, Field field1) { return new Timer1<>(name, field1) { @Override protected void doRecord(F1 field1, long value, TimeUnit unit) { getTimerValue(CounterKey.create(name)).add(value); } @Override public void remove() {} }; } @Override public Counter1 newCounter(String name, Description desc, Field field1) { return new Counter1<>() { @Override public void incrementBy(F1 field1, long value) { getCounterValue(CounterKey.create(name, field1)).add(value); } @Override public void remove() {} }; } @Override public Counter2 newCounter( String name, Description desc, Field field1, Field field2) { return new Counter2<>() { @Override public void incrementBy(F1 field1, F2 field2, long value) { getCounterValue(CounterKey.create(name, field1, field2)).add(value); } @Override public void remove() {} }; } @Override public Counter3 newCounter( String name, Description desc, Field field1, Field field2, Field field3) { return new Counter3<>() { @Override public void incrementBy(F1 field1, F2 field2, F3 field3, long value) { getCounterValue(CounterKey.create(name, field1, field2, field3)).add(value); } @Override public void remove() {} }; } @AutoValue abstract static class CounterKey { abstract String name(); abstract ImmutableList fieldValues(); static CounterKey create(String name, Object... fieldValues) { return new AutoValue_TestMetricMaker_CounterKey( name, ImmutableList.copyOf(Arrays.asList(fieldValues))); } } }