Loading...
Searching...
No Matches
TestGAMSPerformance.cs
1using System;
2using System.IO;
3using GAMS;
4using NLog;
5using NUnit.Framework;
6
7namespace GamsApiTests
8{
9 [TestFixture]
11 {
12 // number of items for huge tests
13 static int NR_ITEMS_HUGE_TEST = 1500000;
14
15 // number of items for big tests (eg. testAddingDatabases, testAddingJobs, ...)
16 static int NR_ITEMS_BIG_TEST = 1500;
17
18 // reusable test set, used for iteration
19 static GAMSSet hugeTestSet;
20
21 [OneTimeSetUp]
22 public void OneTimeSetup()
23 {
25
26 // filling hugeTestSet
27 GAMSDatabase db = ws.AddDatabase();
28 hugeTestSet = db.AddSet("set", 1, "test set");
29
30 DateTimeOffset startTime = DateTimeOffset.UtcNow;
31 for (int i = 0; i < NR_ITEMS_HUGE_TEST; i++)
32 {
33 hugeTestSet.AddRecord("rec" + i);
34 }
35 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
36 Console.WriteLine("Elapsed time: " + totalTime + "s");
37 Assert.True(true);
38 }
39
40 // test adding many many (>1e6) records to a symbol
41 [Test]
42 public void testAddingRecords()
43 {
45 GAMSDatabase db = ws.AddDatabase();
46
47 GAMSSet set = db.AddSet("set", 1, "test set");
48
49 DateTimeOffset startTime = DateTimeOffset.UtcNow;
50 for (int i = 0; i < NR_ITEMS_HUGE_TEST; i++)
51 {
52 set.AddRecord("rec" + i);
53 }
54 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
55 Console.WriteLine("Elapsed time: " + totalTime + "s");
56 Assert.True(true);
57 }
58
59 // test adding many many symbols to a database
60 [Test]
61 public void testAddingSymbols()
62 {
63
65 GAMSDatabase db = ws.AddDatabase();
66
67 DateTimeOffset startTime = DateTimeOffset.UtcNow;
68 for (int i = 0; i < NR_ITEMS_HUGE_TEST; i++)
69 {
70 GAMSSet set = db.AddSet("set" + i, 1, "test set");
71 }
72 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
73 Console.WriteLine("Elapsed time: " + totalTime + "s");
74 Assert.True(true);
75 }
76
77 // test adding many databases to a workspace
78 [Test]
79 public void testAddingDatabases()
80 {
81
83
84 DateTimeOffset startTime = DateTimeOffset.UtcNow;
85 for (int i = 0; i < NR_ITEMS_BIG_TEST; i++)
86 {
87 GAMSDatabase db = ws.AddDatabase();
88 db.AddSet("someset", 1).AddRecord("rec1");
89 Assert.AreEqual(1, db.GetSet("someset").Dim);
90 }
91 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
92 Console.WriteLine("Elapsed time: " + totalTime + "s");
93 Assert.True(true);
94 }
95
96 // test adding many jobs to a workspace
97 [Test]
98 public void testAddingJobs()
99 {
100
101 GAMSWorkspace ws = new GAMSWorkspace();
102
103 DateTimeOffset startTime = DateTimeOffset.UtcNow;;
104 for (int i = 0; i < NR_ITEMS_BIG_TEST; i++)
105 {
106 GAMSJob j = ws.AddJobFromGamsLib("diet");
107 }
108 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
109 Console.WriteLine("Elapsed time: " + totalTime + "s");
110 Assert.True(true);
111 }
112
113 // test adding many options to a workspace
114 [Test]
115 public void testAddingOptions()
116 {
117
118 GAMSWorkspace ws = new GAMSWorkspace();
119
120 DateTimeOffset startTime = DateTimeOffset.UtcNow;;
121 for (int i = 0; i < NR_ITEMS_BIG_TEST; i++)
122 {
123 GAMSOptions o = ws.AddOptions();
124 }
125 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
126 Console.WriteLine("Elapsed time: " + totalTime + "s");
127 Assert.True(true);
128 }
129
130 // test iterating over all records for a very large symbol
131 [Test]
132 public void testIteratingRecords()
133 {
134
135 DateTimeOffset startTime = DateTimeOffset.UtcNow;;
136 int i = 0;
137 foreach (GAMSSetRecord rec in hugeTestSet)
138 {
139 i++;
140 }
141 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
142 Console.WriteLine("Elapsed time: " + totalTime + "s");
143 Assert.AreEqual(NR_ITEMS_HUGE_TEST, i);
144 }
145
146 // running many (>1e3) small GAMSJobs (or running the same job many times)
147 [Test]
148 public void testRunningJobs()
149 {
150
151 GAMSWorkspace ws = new GAMSWorkspace();
152 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
153
154 DateTimeOffset startTime = DateTimeOffset.UtcNow;;
155 for (int i = 0; i < NR_ITEMS_BIG_TEST; i++)
156 {
157 job.Run();
158 }
159 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
160 Console.WriteLine("Elapsed time: " + totalTime + "s");
161 Assert.True(true);
162 }
163
164 // test duplicating many GAMSModelInstances and solving them
165 [Test]
166 public void testDuplicatingModelInstances()
167 {
168
169 GAMSWorkspace ws = new GAMSWorkspace();
171 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
172 job.Run(cp);
173
175 mi.Instantiate("transport use lp min z");
176 mi.Solve();
177
178 DateTimeOffset startTime = DateTimeOffset.UtcNow;;
179 for (int i = 0; i < NR_ITEMS_BIG_TEST; i++)
180 {
182 copy.Solve();
183 }
184 double totalTime = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
185 Console.WriteLine("Elapsed time: " + totalTime + "s");
186 Assert.True(true);
187 }
188
189 }
190}
GAMSModelInstance AddModelInstance(string modelInstanceName=null)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSSet GetSet(string setIdentifier)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
GAMSModelInstance CopyModelinstance(string modelInstanceName=null)
void Solve(SymbolUpdateType updateType=SymbolUpdateType.BaseCase, TextWriter output=null, GAMSModelInstanceOpt miOpt=null)
void Instantiate(string modelDefinition, params GAMSModifier[] modifiers)
new GAMSSetRecord AddRecord(params string[] keys)
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)