JAL-2674 Removing most calls to getHiddenColumnsCopy
[jalview.git] / benchmarking / src / main / java / org / jalview / HiddenColumnsBenchmark.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.annotations.Scope;
33 import org.openjdk.jmh.annotations.Param;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Random;
38 import java.util.concurrent.TimeUnit;
39
40 import jalview.datamodel.ColumnSelection;
41 import jalview.datamodel.HiddenColumns;
42
43 /*
44  * A class to benchmark hidden columns performance
45  */
46 @Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
48 @Fork(1)
49 public class HiddenColumnsBenchmark 
50 {       
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 benchAdjustForHiddenColumns(HiddenColsAndStartState tstate)
104         {
105                 return tstate.h.adjustForHiddenColumns(tstate.visibleColumn);
106         }
107         
108         @Benchmark
109         @BenchmarkMode({Mode.Throughput})
110         public int benchFindColumnPosition(HiddenColsAndStartState tstate)
111         {
112                 return tstate.h.findColumnPosition(tstate.visibleColumn);
113         }
114         
115         /*@Benchmark
116         @BenchmarkMode({Mode.Throughput})
117         public List<Integer> benchFindHiddenRegionPositions(HiddenColsAndStartState tstate)
118         {
119                 return tstate.h.findHiddenRegionPositions();
120         }*/
121         
122         @Benchmark
123         @BenchmarkMode({Mode.Throughput})
124     public int benchGetSize(HiddenColsAndStartState tstate)
125     {
126                 return tstate.h.getSize();
127     }
128
129    @Benchmark
130     @BenchmarkMode({Mode.Throughput})
131     public HiddenColumns benchHideCols(HiddenColsAndStartState tstate) 
132     {
133         tstate.h.hideColumns(tstate.visibleColumn,
134                         tstate.visibleColumn+2000); 
135         return tstate.h;
136     }
137    
138         @Benchmark
139         @BenchmarkMode({Mode.Throughput})
140     public boolean benchIsVisible(HiddenColsAndStartState tstate) 
141     {
142         return tstate.h.isVisible(tstate.hiddenColumn); 
143     }
144         
145         @Benchmark
146         @BenchmarkMode({Mode.Throughput})
147     public HiddenColumns benchReveal(HiddenColsAndStartState tstate) 
148     {
149                 ColumnSelection sel = new ColumnSelection();
150         tstate.h.revealHiddenColumns(tstate.hiddenColumn, sel);
151         return tstate.h;
152     }
153         
154         @Benchmark
155         @BenchmarkMode({Mode.Throughput})
156     public HiddenColumns benchRevealAll(HiddenColsAndStartState tstate) 
157     {
158                 ColumnSelection sel = new ColumnSelection();
159         tstate.h.revealAllHiddenColumns(sel);
160         return tstate.h;
161     }
162         
163         
164 }