package com.tinkerpop.gremlin.structure;

import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
import com.carrotsearch.junitbenchmarks.annotation.LabelType;
import com.tinkerpop.gremlin.AbstractGremlinTest;
import com.tinkerpop.gremlin.algorithm.generator.Distribution;
import com.tinkerpop.gremlin.algorithm.generator.DistributionGenerator;
import com.tinkerpop.gremlin.algorithm.generator.PowerLawDistribution;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
@RunWith(Enclosed.class)
public class GraphReadPerformanceTest {
    @AxisRange(min = 0, max = 1)
    @BenchmarkMethodChart(filePrefix = "gremlin-read")
    @BenchmarkHistoryChart(labelWith = LabelType.CUSTOM_KEY, maxRuns = 20, filePrefix = "hx-gremlin-read")
    public static class ReadFromGraph extends AbstractGremlinTest {

        @Rule
        public TestRule benchmarkRun = new BenchmarkRule();

        private Set<Object> ids = new HashSet<>();

        @Override
        protected void afterLoadGraphWith(final Graph g) throws Exception {
            ids.clear();
            final int numVertices = 10000;
            final Random r = new Random(854939487556l);
            for (int i = 0; i < numVertices; i++) {
                final Vertex v = g.addVertex("oid", i, "name", RandomStringUtils.randomAlphabetic(r.nextInt(1024)));
                ids.add(v.id());
            }

            final Distribution inDist = new PowerLawDistribution(2.3);
            final Distribution outDist = new PowerLawDistribution(2.8);
            final DistributionGenerator generator = DistributionGenerator.build(g)
                    .label("knows")
                    .seedGenerator(r::nextLong)
                    .outDistribution(outDist)
                    .inDistribution(inDist)
                    .expectedNumEdges(numVertices * 3).create();
            generator.generate();
        }

        @Test
        @BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 0, concurrency = BenchmarkOptions.CONCURRENCY_SEQUENTIAL)
        public void readAllProperties() throws Exception {
            final AtomicInteger counter = new AtomicInteger(0);

            // read the vertices 10 times over
            for (int ix = 0; ix < 10; ix++) {
                g.iterators().vertexIterator(ids).forEachRemaining(vertex -> {
                    assertNotNull(vertex.value("name"));
                    counter.incrementAndGet();
                });

                assertEquals(10000, counter.get());
                counter.set(0);
            }
        }
    }
}

@@ -1623,16 +1623,51 @@
 Set%3C%3E();
+%0A        private int edgeCount = 0;
 %0A%0A      
@@ -2491,24 +2491,102 @@
 ion(inDist)%0A
+                    .edgeProcessor(e -%3E e.property(%22weight%22, r.nextDouble()))%0A
             
@@ -2649,16 +2649,28 @@
         
+ edgeCount =
  generat
@@ -2857,16 +2857,27 @@
  readAll
+VerticesAnd
 Properti
@@ -3103,19 +3103,16 @@
 terator(
-ids
 ).forEac
@@ -3255,24 +3255,24 @@
        %7D);%0A%0A
-
             
@@ -3362,21 +3362,691 @@
  %7D%0A        %7D
+%0A%0A        @Test%0A        @BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 0, concurrency = BenchmarkOptions.CONCURRENCY_SEQUENTIAL)%0A        public void readAllEdgesAndProperties() throws Exception %7B%0A            final AtomicInteger counter = new AtomicInteger(0);%0A%0A            // read the vertices 10 times over%0A            for (int ix = 0; ix %3C 10; ix++) %7B%0A                g.iterators().edgeIterator().forEachRemaining(edge -%3E %7B%0A                    assertNotNull(edge.value(%22weight%22));%0A                    counter.incrementAndGet();%0A                %7D);%0A%0A                assertEquals(edgeCount, counter.get());%0A                counter.set(0);%0A            %7D%0A        %7D
 %0A    %7D%0A%7D%0A

Add an edge iteration performance test #231