Search in sources :

Example 1 with Counter

use of edu.princeton.cs.algs4.Counter in project AlgorithmsSolutions by Allenskoo856.

the class Flips method main.

public static void main(String[] args) {
    int T = Integer.parseInt(args[0]);
    Counter heads = new Counter("heads");
    Counter tails = new Counter("tails");
    for (int t = 0; t < T; t++) {
        if (StdRandom.bernoulli(0.5)) {
            heads.increment();
        } else {
            tails.increment();
        }
    }
    StdOut.println(heads);
    StdOut.println(tails);
    int d = heads.tally() - tails.tally();
    StdOut.println("delta: " + Math.abs(d));
}
Also used : Counter(edu.princeton.cs.algs4.Counter)

Example 2 with Counter

use of edu.princeton.cs.algs4.Counter in project AlgorithmsSolutions by Allenskoo856.

the class FlipsMax method main.

public static void main(String[] args) {
    int T = Integer.parseInt(args[0]);
    Counter heads = new Counter("heads");
    Counter tails = new Counter("tails");
    for (int t = 0; t < T; t++) {
        if (StdRandom.bernoulli(0.5)) {
            heads.increment();
        } else {
            tails.increment();
        }
    }
    if (heads.tally() == tails.tally()) {
        StdOut.println("Tie");
    } else {
        StdOut.println(max(heads, tails) + " wins");
    }
}
Also used : Counter(edu.princeton.cs.algs4.Counter)

Example 3 with Counter

use of edu.princeton.cs.algs4.Counter in project instrumentation-java by census-instrumentation.

the class DropWizardMetricsTest method collect.

@Test
public void collect() throws InterruptedException {
    // create dropwizard metrics
    Map<String, String> tags = new HashMap<>();
    tags.put("tag1", "value1");
    tags.put("tag2", "value2");
    List<LabelKey> labelKeys = new ArrayList<>();
    List<LabelValue> labelValues = new ArrayList<>();
    for (Map.Entry<String, String> e : tags.entrySet()) {
        labelKeys.add(LabelKey.create(e.getKey(), e.getKey()));
        labelValues.add(LabelValue.create(e.getValue()));
    }
    Counter evictions = metricRegistry.counter(new MetricName("cache_evictions", tags));
    evictions.inc();
    evictions.inc(3);
    evictions.dec();
    evictions.dec(2);
    metricRegistry.gauge(new MetricName("boolean_gauge", tags), BooleanGauge::new);
    metricRegistry.gauge(new MetricName("double_gauge", tags), DoubleGauge::new);
    metricRegistry.gauge(new MetricName("float_gauge", tags), FloatGauge::new);
    metricRegistry.gauge(new MetricName("integer_gauge", tags), IntegerGauge::new);
    metricRegistry.gauge(new MetricName("long_gauge", tags), LongGauge::new);
    metricRegistry.gauge(new MetricName("notags_boolean_gauge", Collections.emptyMap()), BooleanGauge::new);
    Meter getRequests = metricRegistry.meter(new MetricName("get_requests", tags));
    getRequests.mark();
    getRequests.mark();
    Histogram resultCounts = metricRegistry.histogram(new MetricName("result", tags));
    resultCounts.update(200);
    Timer timer = metricRegistry.timer(new MetricName("requests", tags));
    Timer.Context context = timer.time();
    Thread.sleep(1L);
    context.stop();
    ArrayList<Metric> metrics = new ArrayList<>(dropWizardMetrics.getMetrics());
    assertThat(metrics.size()).isEqualTo(10);
    assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_cache_evictions_counter", "Collected from dropwizard5 (metric=cache_evictions, " + "type=io.dropwizard.metrics5.Counter)", DEFAULT_UNIT, Type.GAUGE_INT64, labelKeys));
    assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    // boolean gauge with tags
    assertThat(metrics.get(1).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_boolean_gauge_gauge", "Collected from dropwizard5 (metric=boolean_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$BooleanGauge)", DEFAULT_UNIT, Type.GAUGE_INT64, labelKeys));
    assertThat(metrics.get(1).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(2).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_double_gauge_gauge", "Collected from dropwizard5 (metric=double_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$DoubleGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(2).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(1.234));
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(3).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_float_gauge_gauge", "Collected from dropwizard5 (metric=float_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$FloatGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(3).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(0.1234000027179718));
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(4).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_integer_gauge_gauge", "Collected from dropwizard5 (metric=integer_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$IntegerGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(4).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(1234.0));
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(5).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_long_gauge_gauge", "Collected from dropwizard5 (metric=long_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$LongGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(5).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(1234.0));
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    // boolean gauge with tags
    assertThat(metrics.get(6).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_notags_boolean_gauge_gauge", "Collected from dropwizard5 (metric=notags_boolean_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$BooleanGauge)", DEFAULT_UNIT, Type.GAUGE_INT64, Collections.emptyList()));
    assertThat(metrics.get(6).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(0);
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(7).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_get_requests_meter", "Collected from dropwizard5 (metric=get_requests, " + "type=io.dropwizard.metrics5.Meter)", DEFAULT_UNIT, Type.CUMULATIVE_INT64, labelKeys));
    assertThat(metrics.get(7).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(2));
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getStartTimestamp()).isNotNull();
    assertThat(metrics.get(8).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_result_histogram", "Collected from dropwizard5 (metric=result, " + "type=io.dropwizard.metrics5.Histogram)", DEFAULT_UNIT, Type.SUMMARY, labelKeys));
    assertThat(metrics.get(8).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.summaryValue(Summary.create(1L, 0.0, Snapshot.create(1L, 0.0, Arrays.asList(ValueAtPercentile.create(50.0, 200.0), ValueAtPercentile.create(75.0, 200.0), ValueAtPercentile.create(98.0, 200.0), ValueAtPercentile.create(99.0, 200.0), ValueAtPercentile.create(99.9, 200.0))))));
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getStartTimestamp()).isInstanceOf(Timestamp.class);
    assertThat(metrics.get(9).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_requests_timer", "Collected from dropwizard5 (metric=requests, " + "type=io.dropwizard.metrics5.Timer)", NS_UNIT, Type.SUMMARY, labelKeys));
    assertThat(metrics.get(9).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.summaryValue(Summary.create(1L, 0.0, Snapshot.create(1L, 0.0, Arrays.asList(ValueAtPercentile.create(50.0, timer.getSnapshot().getMedian()), ValueAtPercentile.create(75.0, timer.getSnapshot().get75thPercentile()), ValueAtPercentile.create(98.0, timer.getSnapshot().get98thPercentile()), ValueAtPercentile.create(99.0, timer.getSnapshot().get99thPercentile()), ValueAtPercentile.create(99.9, timer.getSnapshot().get999thPercentile()))))));
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getStartTimestamp()).isInstanceOf(Timestamp.class);
}
Also used : Histogram(io.dropwizard.metrics5.Histogram) LabelValue(io.opencensus.metrics.LabelValue) HashMap(java.util.HashMap) Meter(io.dropwizard.metrics5.Meter) ArrayList(java.util.ArrayList) MetricName(io.dropwizard.metrics5.MetricName) Counter(io.dropwizard.metrics5.Counter) Timer(io.dropwizard.metrics5.Timer) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Counter

use of edu.princeton.cs.algs4.Counter in project AlgorithmsSolutions by Allenskoo856.

the class Ex09 method main.

public static void main(String[] args) {
    System.out.print("N: ");
    int N = StdIn.readInt();
    int[] a = new int[N];
    System.out.print("list: ");
    for (int i = 0; i < N; i++) {
        a[i] = StdIn.readInt();
    }
    Arrays.sort(a);
    System.out.print("key: ");
    int key = StdIn.readInt();
    Counter counter = new Counter("keys");
    System.out.println(rank(key, a, counter));
    System.out.println(counter);
}
Also used : Counter(edu.princeton.cs.algs4.Counter)

Example 5 with Counter

use of edu.princeton.cs.algs4.Counter in project AlgorithmsSolutions by Allenskoo856.

the class Rolls method main.

public static void main(String[] args) {
    int T = Integer.parseInt(args[0]);
    int SIDES = 6;
    Counter[] rolls = new Counter[SIDES + 1];
    for (int i = 1; i <= SIDES; i++) {
        rolls[i] = new Counter(i + "'s");
    }
    for (int t = 0; t < T; t++) {
        int result = StdRandom.uniform(1, SIDES + 1);
        rolls[result].increment();
    }
    for (int i = 1; i <= SIDES; i++) {
        StdOut.println(rolls[i]);
    }
}
Also used : Counter(edu.princeton.cs.algs4.Counter)

Aggregations

Counter (edu.princeton.cs.algs4.Counter)5 Counter (io.dropwizard.metrics5.Counter)1 Histogram (io.dropwizard.metrics5.Histogram)1 Meter (io.dropwizard.metrics5.Meter)1 MetricName (io.dropwizard.metrics5.MetricName)1 Timer (io.dropwizard.metrics5.Timer)1 LabelKey (io.opencensus.metrics.LabelKey)1 LabelValue (io.opencensus.metrics.LabelValue)1 Metric (io.opencensus.metrics.export.Metric)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1