JAL-2662 tweak README, format code
[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
68     Random rand = new Random();
69
70     public int hiddenColumn;
71
72     public int visibleColumn;
73
74     @Setup
75     public void setup()
76     {
77       rand.setSeed(1234);
78       int lastcol = 0;
79       while (lastcol < maxcols)
80       {
81         int count = rand.nextInt(100);
82         lastcol += count;
83         h.hideColumns(lastcol, lastcol + hide);
84         lastcol += hide;
85       }
86
87       // make sure column at start is hidden
88       hiddenColumn = (int) (maxcols * startpcnt / 100.0);
89       h.hideColumns(hiddenColumn, hiddenColumn);
90
91       // and column <hide> after start is visible
92       ColumnSelection sel = new ColumnSelection();
93       h.revealHiddenColumns(hiddenColumn + hide, sel);
94       visibleColumn = hiddenColumn + hide;
95
96       System.out.println("Maxcols: " + maxcols + " HiddenCol: "
97               + hiddenColumn + " Hide: " + hide);
98       System.out.println("Number of hidden columns: " + h.getSize());
99     }
100   }
101
102   /* Convention: functions in alphabetical order */
103
104   @Benchmark
105   @BenchmarkMode({ Mode.Throughput })
106   public int benchAdjustForHiddenColumns(HiddenColsAndStartState tstate)
107   {
108     return tstate.h.adjustForHiddenColumns(tstate.visibleColumn);
109   }
110
111   @Benchmark
112   @BenchmarkMode({ Mode.Throughput })
113   public int benchFindColumnPosition(HiddenColsAndStartState tstate)
114   {
115     return tstate.h.findColumnPosition(tstate.visibleColumn);
116   }
117
118   @Benchmark
119   @BenchmarkMode({ Mode.Throughput })
120   public List<Integer> benchFindHiddenRegionPositions(
121           HiddenColsAndStartState tstate)
122   {
123     return tstate.h.findHiddenRegionPositions();
124   }
125
126   @Benchmark
127   @BenchmarkMode({ Mode.Throughput })
128   public ArrayList<int[]> benchGetHiddenColumnsCopy(
129           HiddenColsAndStartState tstate)
130   {
131     return tstate.h.getHiddenColumnsCopy();
132   }
133
134   @Benchmark
135   @BenchmarkMode({ Mode.Throughput })
136   public int benchGetSize(HiddenColsAndStartState tstate)
137   {
138     return tstate.h.getSize();
139   }
140
141   @Benchmark
142   @BenchmarkMode({ Mode.Throughput })
143   public HiddenColumns benchHideCols(HiddenColsAndStartState tstate)
144   {
145     tstate.h.hideColumns(tstate.visibleColumn, tstate.visibleColumn + 2000);
146     return tstate.h;
147   }
148
149   @Benchmark
150   @BenchmarkMode({ Mode.Throughput })
151   public boolean benchIsVisible(HiddenColsAndStartState tstate)
152   {
153     return tstate.h.isVisible(tstate.hiddenColumn);
154   }
155
156   @Benchmark
157   @BenchmarkMode({ Mode.Throughput })
158   public HiddenColumns benchReveal(HiddenColsAndStartState tstate)
159   {
160     ColumnSelection sel = new ColumnSelection();
161     tstate.h.revealHiddenColumns(tstate.hiddenColumn, sel);
162     return tstate.h;
163   }
164
165   @Benchmark
166   @BenchmarkMode({ Mode.Throughput })
167   public HiddenColumns benchRevealAll(HiddenColsAndStartState tstate)
168   {
169     ColumnSelection sel = new ColumnSelection();
170     tstate.h.revealAllHiddenColumns(sel);
171     return tstate.h;
172   }
173
174 }