JAL-2759 Rename getBoundedStartIterator
[jalview.git] / benchmarking / src / main / java / org / jalview / HiddenColsIteratorsBenchmark.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package org.jalview;
23
24 import org.openjdk.jmh.annotations.Benchmark;
25 import org.openjdk.jmh.annotations.BenchmarkMode;
26 import org.openjdk.jmh.annotations.Fork;
27 import org.openjdk.jmh.annotations.Measurement;
28 import org.openjdk.jmh.annotations.Mode;
29 import org.openjdk.jmh.annotations.Setup;
30 import org.openjdk.jmh.annotations.State;
31 import org.openjdk.jmh.annotations.Warmup;
32 import org.openjdk.jmh.infra.Blackhole;
33 import org.openjdk.jmh.annotations.Scope;
34 import org.openjdk.jmh.annotations.Param;
35
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Random;
39 import java.util.concurrent.TimeUnit;
40
41 import jalview.datamodel.ColumnSelection;
42 import jalview.datamodel.HiddenColumns;
43
44 /*
45  * A class to benchmark hidden columns performance
46  */
47 @Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
48 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
49 @Fork(1)
50 public class HiddenColsIteratorsBenchmark {
51         /*
52          * State with multiple hidden columns and a start position set
53          */
54         @State(Scope.Thread)
55         public static class HiddenColsAndStartState
56         {
57                 @Param({"300", "10000", "100000"})
58                 public int maxcols;
59                 
60                 @Param({"1", "50", "90"})
61                 public int startpcnt; // position as percentage of maxcols
62                 
63                 @Param({"1","15","100"})
64                 public int hide;
65                 
66                 HiddenColumns h = new HiddenColumns();
67                 Random rand = new Random();
68                 
69                 public int hiddenColumn;
70                 public int visibleColumn;
71         
72                 @Setup
73                 public void setup()
74                 {
75                         rand.setSeed(1234);
76                         int lastcol = 0;
77                 while (lastcol < maxcols)
78                 {
79                         int count = rand.nextInt(100); 
80                         lastcol += count;
81                         h.hideColumns(lastcol, lastcol+hide);
82                         lastcol+=hide;
83                 }
84                 
85                 // make sure column at start is hidden
86                 hiddenColumn = (int)(maxcols * startpcnt/100.0);
87                 h.hideColumns(hiddenColumn, hiddenColumn);
88                 
89                 // and column <hide> after start is visible
90                 ColumnSelection sel = new ColumnSelection();
91                 h.revealHiddenColumns(hiddenColumn+hide, sel);
92                 visibleColumn = hiddenColumn+hide;
93                 
94                 System.out.println("Maxcols: " + maxcols + " HiddenCol: " + hiddenColumn + " Hide: " + hide);
95                 System.out.println("Number of hidden columns: " + h.getSize());
96                 }
97         }
98         
99         /* Convention: functions in alphabetical order */
100         
101         @Benchmark
102         @BenchmarkMode({Mode.Throughput})
103         public int benchStartIterator(HiddenColsAndStartState tstate)
104         {
105                 int res = 0;
106                 int startx = tstate.visibleColumn;
107                 Iterator<Integer> it = tstate.h.getStartRegionIterator(startx,
108                                 startx+60);
109         while (it.hasNext())
110         {
111           res = it.next() - startx;
112           Blackhole.consumeCPU(5);
113         }
114         return res;
115         }
116         
117         @Benchmark
118         @BenchmarkMode({Mode.Throughput})
119         public int benchBoundedIterator(HiddenColsAndStartState tstate)
120         {
121                 int startx = tstate.visibleColumn;
122                 int blockStart = startx;
123                 int blockEnd;
124                 int screenY = 0;
125                 Iterator<int[]> it = tstate.h.getBoundedIterator(startx,
126                                 startx+60);
127         while (it.hasNext())
128         {
129                 int[] region = it.next();
130           
131                 blockEnd = Math.min(region[0] - 1, blockStart + 60 - screenY);
132               
133                 screenY += blockEnd - blockStart + 1;
134                 blockStart = region[1] + 1;
135                 
136                 Blackhole.consumeCPU(5);
137         }
138         return blockStart;
139         }
140 }