- Timestamp:
- 11/27/15 14:23:05 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r13407 r13409 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using System.Diagnostics; 25 26 using System.Drawing; 26 27 using System.Globalization; … … 31 32 using System.Threading.Tasks; 32 33 using HeuristicLab.Algorithms.GeneticAlgorithm; 34 using HeuristicLab.Data; 33 35 using HeuristicLab.Persistence; 34 36 using HeuristicLab.Persistence.Auxiliary; … … 42 44 43 45 namespace HeuristicLab.PersistenceNew.Tests { 46 public static class EnumerableTimeSpanExtensions { 47 public static TimeSpan Average(this IEnumerable<TimeSpan> span) { 48 var avg = (long)Math.Round(span.Select(x => x.Ticks).Average()); 49 return new TimeSpan(avg); 50 } 51 } 52 44 53 #region Test Classes 45 54 [StorableClass("7D9672BD-703D-42BB-9080-9929885D4580")] … … 264 273 } 265 274 275 public class SimpleClass { 276 public double x { get; set; } 277 public int y { get; set; } 278 } 279 266 280 #endregion 267 281 … … 290 304 #endregion 291 305 306 #region Persistence 4.0 Profiling Helpers 307 public class PerformanceData { 308 public TimeSpan OldSerializingTime { get; set; } 309 public TimeSpan NewSerializingTime { get; set; } 310 public TimeSpan OldDeserializingTime { get; set; } 311 public TimeSpan NewDeserializingTime { get; set; } 312 public long OldFileSize { get; set; } 313 public long NewFileSize { get; set; } 314 public long OldSerializingMemoryConsumed { get; set; } 315 public long NewSerializingMemoryConsumed { get; set; } 316 public long OldDeserializingMemoryConsumed { get; set; } 317 public long NewDeserializingMemoryConsumed { get; set; } 318 } 319 320 private void SerializeNew(object o) { 321 ProtoBufSerializer serializer = new ProtoBufSerializer(); 322 serializer.Serialize(o, tempFile); 323 } 324 private void SerializeOld(object o) { 325 XmlGenerator.Serialize(o, tempFile); 326 } 327 private object DeserializeNew() { 328 ProtoBufSerializer serializer = new ProtoBufSerializer(); 329 return serializer.Deserialize(tempFile); 330 } 331 private object DeserialiezOld() { 332 return XmlParser.Deserialize(tempFile); 333 } 334 335 private void CollectGarbage() { 336 GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true); 337 GC.WaitForPendingFinalizers(); 338 } 339 340 public PerformanceData ProfileSingleRun(Func<object> GenerateDataFunc) { 341 PerformanceData performanceData = new PerformanceData(); 342 Stopwatch sw = new Stopwatch(); 343 object data = GenerateDataFunc(); 344 object result = null; 345 long startMem, endMem; 346 347 348 //old file format serializing 349 CollectGarbage(); 350 startMem = GC.GetTotalMemory(false); 351 sw.Start(); 352 SerializeOld(data); 353 sw.Stop(); 354 endMem = GC.GetTotalMemory(false); 355 performanceData.OldSerializingTime = sw.Elapsed; 356 performanceData.OldFileSize = new FileInfo(tempFile).Length; 357 performanceData.OldSerializingMemoryConsumed = endMem - startMem; 358 sw.Reset(); 359 360 361 //old file format deserializing 362 CollectGarbage(); 363 startMem = GC.GetTotalMemory(false); 364 sw.Start(); 365 result = DeserialiezOld(); 366 sw.Stop(); 367 endMem = GC.GetTotalMemory(false); 368 performanceData.OldDeserializingTime = sw.Elapsed; 369 performanceData.OldDeserializingMemoryConsumed = endMem - startMem; 370 sw.Reset(); 371 372 373 //new file format serializing 374 CollectGarbage(); 375 startMem = GC.GetTotalMemory(false); 376 sw.Start(); 377 SerializeNew(data); 378 sw.Stop(); 379 endMem = GC.GetTotalMemory(false); 380 performanceData.NewSerializingTime = sw.Elapsed; 381 performanceData.NewSerializingMemoryConsumed = endMem - startMem; 382 performanceData.NewFileSize = new FileInfo(tempFile).Length; 383 sw.Reset(); 384 385 386 //new file format deserializing 387 CollectGarbage(); 388 startMem = GC.GetTotalMemory(false); 389 sw.Start(); 390 result = DeserializeNew(); 391 sw.Stop(); 392 endMem = GC.GetTotalMemory(false); 393 performanceData.NewDeserializingTime = sw.Elapsed; 394 performanceData.NewDeserializingMemoryConsumed = endMem - startMem; 395 sw.Reset(); 396 397 return performanceData; 398 } 399 400 public string Profile(Func<object> GenerateDataFunc) { 401 int nrOfRepetitions = 10; 402 StringBuilder report = new StringBuilder(); 403 List<PerformanceData> dataList = new List<PerformanceData>(); 404 405 for (int i = 0; i < nrOfRepetitions; i++) { 406 dataList.Add(ProfileSingleRun(GenerateDataFunc)); 407 } 408 409 report.Append("Performance Report for " + GenerateDataFunc.Method.Name + ": " + Environment.NewLine); 410 report.Append(Environment.NewLine); 411 report.AppendFormat("Avg. old vs. new time for serializing a file: {0} / {1}; Factor: {2}", 412 dataList.Select(x => x.OldSerializingTime).Average(), 413 dataList.Select(x => x.NewSerializingTime).Average(), 414 dataList.Select(x => x.OldSerializingTime.TotalMilliseconds).Average() / dataList.Select(x => x.NewSerializingTime.TotalMilliseconds).Average() 415 ); 416 report.Append(Environment.NewLine); 417 report.AppendFormat("Avg. old vs. new time for deserializing a file: {0} / {1}; Factor: {2}", 418 dataList.Select(x => x.OldDeserializingTime).Average(), 419 dataList.Select(x => x.NewDeserializingTime).Average(), 420 dataList.Select(x => x.OldDeserializingTime.TotalMilliseconds).Average() / dataList.Select(x => x.NewDeserializingTime.TotalMilliseconds).Average() 421 ); 422 report.Append(Environment.NewLine); 423 report.AppendFormat("Avg. old vs. new file size (in bytes): {0} / {1}; Factor: {2}", 424 dataList.Select(x => x.OldFileSize).Average(), 425 dataList.Select(x => x.NewFileSize).Average(), 426 dataList.Select(x => x.OldFileSize).Average() / dataList.Select(x => x.NewFileSize).Average() 427 ); 428 report.Append(Environment.NewLine); 429 report.AppendFormat("Avg. old vs. new memory consumption for serializing a file (in bytes): {0} / {1}; Factor: {2}", 430 dataList.Select(x => x.OldSerializingMemoryConsumed).Average(), 431 dataList.Select(x => x.NewSerializingMemoryConsumed).Average(), 432 dataList.Select(x => x.OldSerializingMemoryConsumed).Average() / dataList.Select(x => x.NewSerializingMemoryConsumed).Average() 433 ); 434 report.Append(Environment.NewLine); 435 report.AppendFormat("Avg. old vs. new memory consumption for deserializing a file (in bytes): {0} / {1}; Factor: {2}", 436 dataList.Select(x => x.OldDeserializingMemoryConsumed).Average(), 437 dataList.Select(x => x.NewDeserializingMemoryConsumed).Average(), 438 dataList.Select(x => x.OldDeserializingMemoryConsumed).Average() / dataList.Select(x => x.NewDeserializingMemoryConsumed).Average() 439 ); 440 report.Append(Environment.NewLine); 441 442 443 return report.ToString(); 444 } 445 #endregion 446 292 447 #region Persistence 4.0 test methods 293 448 [TestMethod] 294 [TestCategory("Persistence ")]449 [TestCategory("Persistence4")] 295 450 [TestProperty("Time", "short")] 296 451 public void TestBool() { 297 bool test = true;298 ProtoBufSerializer serializer = new ProtoBufSerializer(); 299 serializer.Serialize(test , tempFile);452 var test = new Func<object>(() => { return true; }); 453 ProtoBufSerializer serializer = new ProtoBufSerializer(); 454 serializer.Serialize(test(), tempFile); 300 455 object o = serializer.Deserialize(tempFile); 301 456 bool result = (bool)o; 302 Assert.AreEqual(test, result); 303 } 304 305 [TestMethod] 306 [TestCategory("Persistence")] 457 Assert.AreEqual(test(), result); 458 459 string msg = Profile(test); 460 Console.WriteLine(msg); 461 } 462 463 [TestMethod] 464 [TestCategory("Persistence4")] 307 465 [TestProperty("Time", "short")] 308 466 public void TestInt() { 309 int test = 42;310 ProtoBufSerializer serializer = new ProtoBufSerializer(); 311 serializer.Serialize(test , tempFile);467 var test = new Func<object>(() => { return (int)42; }); 468 ProtoBufSerializer serializer = new ProtoBufSerializer(); 469 serializer.Serialize(test(), tempFile); 312 470 object o = serializer.Deserialize(tempFile); 313 471 int result = (int)o; 314 Assert.AreEqual(test, result); 315 } 316 317 [TestMethod] 318 [TestCategory("Persistence")] 472 Assert.AreEqual(test(), result); 473 474 string msg = Profile(test); 475 Console.WriteLine(msg); 476 } 477 478 [TestMethod] 479 [TestCategory("Persistence4")] 480 [TestProperty("Time", "short")] 481 public void TestDouble() { 482 var test = new Func<object>(() => { return 42.5d; }); 483 ProtoBufSerializer serializer = new ProtoBufSerializer(); 484 serializer.Serialize(test(), tempFile); 485 object o = serializer.Deserialize(tempFile); 486 double result = (double)o; 487 Assert.AreEqual(test(), result); 488 Assert.IsTrue(o is double); 489 490 string msg = Profile(test); 491 Console.WriteLine(msg); 492 } 493 494 [TestMethod] 495 [TestCategory("Persistence4")] 496 [TestProperty("Time", "short")] 497 public void TestFloat() { 498 var test = new Func<object>(() => { return 42.5f; }); 499 ProtoBufSerializer serializer = new ProtoBufSerializer(); 500 serializer.Serialize(test(), tempFile); 501 object o = serializer.Deserialize(tempFile); 502 float result = (float)o; 503 Assert.AreEqual(test(), result); 504 Assert.IsTrue(o is float); 505 506 string msg = Profile(test); 507 Console.WriteLine(msg); 508 } 509 510 [TestMethod] 511 [TestCategory("Persistence4")] 512 [TestProperty("Time", "short")] 513 public void TestDecimal() { 514 var test = new Func<object>(() => { return 42.5m; }); 515 ProtoBufSerializer serializer = new ProtoBufSerializer(); 516 serializer.Serialize(test(), tempFile); 517 object o = serializer.Deserialize(tempFile); 518 decimal result = (decimal)o; 519 Assert.AreEqual(test(), result); 520 Assert.IsTrue(o is decimal); 521 522 string msg = Profile(test); 523 Console.WriteLine(msg); 524 } 525 526 [TestMethod] 527 [TestCategory("Persistence4")] 528 [TestProperty("Time", "short")] 529 public void TestLong() { 530 var test = new Func<object>(() => { return 42l; }); 531 ProtoBufSerializer serializer = new ProtoBufSerializer(); 532 serializer.Serialize(test(), tempFile); 533 object o = serializer.Deserialize(tempFile); 534 long result = (long)o; 535 Assert.AreEqual(test(), result); 536 Assert.IsTrue(o is long); 537 538 string msg = Profile(test); 539 Console.WriteLine(msg); 540 } 541 542 [TestMethod] 543 [TestCategory("Persistence4")] 544 [TestProperty("Time", "short")] 545 public void TestUInt() { 546 var test = new Func<object>(() => { return 42u; }); 547 ProtoBufSerializer serializer = new ProtoBufSerializer(); 548 serializer.Serialize(test(), tempFile); 549 object o = serializer.Deserialize(tempFile); 550 uint result = (uint)o; 551 Assert.AreEqual(test(), result); 552 Assert.IsTrue(o is uint); 553 554 string msg = Profile(test); 555 Console.WriteLine(msg); 556 } 557 558 [TestMethod] 559 [TestCategory("Persistence4")] 560 [TestProperty("Time", "short")] 561 public void TestShort() { 562 var test = new Func<object>(() => { short s = 42; return s; }); 563 ProtoBufSerializer serializer = new ProtoBufSerializer(); 564 serializer.Serialize(test(), tempFile); 565 object o = serializer.Deserialize(tempFile); 566 short result = (short)o; 567 Assert.IsTrue(o is short); 568 Assert.AreEqual(test(), result); 569 570 string msg = Profile(test); 571 Console.WriteLine(msg); 572 } 573 574 [TestMethod] 575 [TestCategory("Persistence4")] 576 [TestProperty("Time", "short")] 577 public void TestByte() { 578 var test = new Func<object>(() => { byte b = 42; return b; }); 579 ProtoBufSerializer serializer = new ProtoBufSerializer(); 580 serializer.Serialize(test(), tempFile); 581 object o = serializer.Deserialize(tempFile); 582 byte result = (byte)o; 583 Assert.IsTrue(o is byte); 584 Assert.AreEqual(test(), result); 585 586 string msg = Profile(test); 587 Console.WriteLine(msg); 588 } 589 590 [TestMethod] 591 [TestCategory("Persistence4")] 319 592 [TestProperty("Time", "short")] 320 593 public void TestEnumSimple() { 321 SimpleEnum se = SimpleEnum.two;322 ComplexEnum ce = ComplexEnum.three; 323 ProtoBufSerializer serializer = new ProtoBufSerializer(); 324 serializer.Serialize( se, tempFile);594 var test = new Func<object>(() => { return SimpleEnum.two; }); 595 596 ProtoBufSerializer serializer = new ProtoBufSerializer(); 597 serializer.Serialize(test(), tempFile); 325 598 object o = serializer.Deserialize(tempFile); 326 599 SimpleEnum result = (SimpleEnum)o; 327 Assert.AreEqual(se, result); 328 } 329 330 [TestMethod] 331 [TestCategory("Persistence")] 600 Assert.AreEqual(test(), result); 601 602 string msg = Profile(test); 603 Console.WriteLine(msg); 604 } 605 606 [TestMethod] 607 [TestCategory("Persistence4")] 332 608 [TestProperty("Time", "short")] 333 609 public void TestEnumComplex() { 334 ComplexEnum ce = ComplexEnum.three;335 ProtoBufSerializer serializer = new ProtoBufSerializer(); 336 serializer.Serialize( ce, tempFile);610 var test = new Func<object>(() => { return ComplexEnum.three; }); 611 ProtoBufSerializer serializer = new ProtoBufSerializer(); 612 serializer.Serialize(test(), tempFile); 337 613 object o = serializer.Deserialize(tempFile); 338 614 ComplexEnum result = (ComplexEnum)o; 339 Assert.AreEqual(ce, result); 340 } 341 342 [TestMethod] 343 [TestCategory("Persistence")] 615 Assert.AreEqual(test(), result); 616 617 string msg = Profile(test); 618 Console.WriteLine(msg); 619 } 620 621 [TestMethod] 622 [TestCategory("Persistence4")] 344 623 [TestProperty("Time", "short")] 345 624 public void TestType() { 346 Type test = typeof(HeuristicLab.Algorithms.GeneticAlgorithm.GeneticAlgorithm);347 ProtoBufSerializer serializer = new ProtoBufSerializer(); 348 serializer.Serialize(test , tempFile);625 var test = new Func<object>(() => { return typeof(HeuristicLab.Algorithms.GeneticAlgorithm.GeneticAlgorithm); }); 626 ProtoBufSerializer serializer = new ProtoBufSerializer(); 627 serializer.Serialize(test(), tempFile); 349 628 object o = serializer.Deserialize(tempFile); 350 629 Type result = (Type)o; 351 Assert.AreEqual(test, result); 352 } 353 630 Assert.AreEqual(test(), result); 631 632 string msg = Profile(test); 633 Console.WriteLine(msg); 634 } 635 636 [TestMethod] 637 [TestCategory("Persistence4")] 638 [TestProperty("Time", "short")] 639 public void TestBytes() { 640 var test = new Func<byte[]>(() => { return new byte[] { 3, 1 }; }); 641 ProtoBufSerializer serializer = new ProtoBufSerializer(); 642 serializer.Serialize(test(), tempFile); 643 object o = serializer.Deserialize(tempFile); 644 byte[] result = (byte[])o; 645 Assert.AreEqual(test()[0], result[0]); 646 Assert.AreEqual(test()[1], result[1]); 647 648 string msg = Profile(test); 649 Console.WriteLine(msg); 650 } 651 652 [TestMethod] 653 [TestCategory("Persistence4")] 654 [TestProperty("Time", "short")] 655 public void TestString() { 656 var test = new Func<object>(() => { return "Hello World!"; }); 657 ProtoBufSerializer serializer = new ProtoBufSerializer(); 658 serializer.Serialize(test(), tempFile); 659 object o = serializer.Deserialize(tempFile); 660 string result = (string)o; 661 Assert.AreEqual(test(), result); 662 663 string msg = Profile(test); 664 Console.WriteLine(msg); 665 } 666 667 [TestMethod] 668 [TestCategory("Persistence4")] 669 [TestProperty("Time", "short")] 670 public void TestColor() { 671 var test = new Func<object>(() => { return Color.DeepSkyBlue; }); 672 ProtoBufSerializer serializer = new ProtoBufSerializer(); 673 serializer.Serialize(test(), tempFile); 674 object o = serializer.Deserialize(tempFile); 675 Color result = (Color)o; 676 Assert.AreEqual(test(), result); 677 678 string msg = Profile(test); 679 Console.WriteLine(msg); 680 } 681 682 [TestMethod] 683 [TestCategory("Persistence4")] 684 [TestProperty("Time", "short")] 685 public void TestPoint() { 686 var test = new Func<object>(() => { return new Point(3, 4); }); 687 ProtoBufSerializer serializer = new ProtoBufSerializer(); 688 serializer.Serialize(test(), tempFile); 689 object o = serializer.Deserialize(tempFile); 690 Point result = (Point)o; 691 Assert.AreEqual(((Point)test()).X, result.X); 692 Assert.AreEqual(((Point)test()).Y, result.Y); 693 694 string msg = Profile(test); 695 Console.WriteLine(msg); 696 } 697 698 [TestMethod] 699 [TestCategory("Persistence4")] 700 [TestProperty("Time", "short")] 701 public void TestBoolArray() { 702 var test = new Func<bool[]>(() => { return new[] { true, false, true }; }); 703 ProtoBufSerializer serializer = new ProtoBufSerializer(); 704 serializer.Serialize(test(), tempFile); 705 object o = serializer.Deserialize(tempFile); 706 bool[] result = (bool[])o; 707 Assert.AreEqual(test()[0], result[0]); 708 Assert.AreEqual(test()[1], result[1]); 709 Assert.AreEqual(test()[2], result[2]); 710 711 string msg = Profile(test); 712 Console.WriteLine(msg); 713 } 714 715 [TestMethod] 716 [TestCategory("Persistence4")] 717 [TestProperty("Time", "short")] 718 public void TestIntArray() { 719 var test = new Func<int[]>(() => { return new[] { 41, 22, 13 }; }); 720 ProtoBufSerializer serializer = new ProtoBufSerializer(); 721 serializer.Serialize(test(), tempFile); 722 object o = serializer.Deserialize(tempFile); 723 int[] result = (int[])o; 724 Assert.AreEqual(test()[0], result[0]); 725 Assert.AreEqual(test()[1], result[1]); 726 Assert.AreEqual(test()[2], result[2]); 727 728 string msg = Profile(test); 729 Console.WriteLine(msg); 730 } 731 732 [TestMethod] 733 [TestCategory("Persistence4")] 734 [TestProperty("Time", "short")] 735 public void TestLongArray() { 736 var test = new Func<long[]>(() => { return new[] { 414481188112191633l, 245488586662l, 13546881335845865l }; }); 737 ProtoBufSerializer serializer = new ProtoBufSerializer(); 738 serializer.Serialize(test(), tempFile); 739 object o = serializer.Deserialize(tempFile); 740 long[] result = (long[])o; 741 Assert.AreEqual(test()[0], result[0]); 742 Assert.AreEqual(test()[1], result[1]); 743 Assert.AreEqual(test()[2], result[2]); 744 745 string msg = Profile(test); 746 Console.WriteLine(msg); 747 } 748 749 [TestMethod] 750 [TestCategory("Persistence4")] 751 [TestProperty("Time", "short")] 752 public void TestDoubleArray() { 753 var test = new Func<double[]>(() => { return new[] { 41.5, 22.7, 13.8 }; }); 754 ProtoBufSerializer serializer = new ProtoBufSerializer(); 755 serializer.Serialize(test(), tempFile); 756 object o = serializer.Deserialize(tempFile); 757 double[] result = (double[])o; 758 Assert.AreEqual(test()[0], result[0]); 759 Assert.AreEqual(test()[1], result[1]); 760 Assert.AreEqual(test()[2], result[2]); 761 762 string msg = Profile(test); 763 Console.WriteLine(msg); 764 } 765 766 [TestMethod] 767 [TestCategory("Persistence4")] 768 [TestProperty("Time", "short")] 769 public void TestObjectArray() { 770 var test = new Func<SimpleClass[]>(() => { 771 return new[] { new SimpleClass() { x = 42, y = 43 }, 772 new SimpleClass() { x = 44.44, y = 5677 }, 773 new SimpleClass() { x = 533.33, y = 2345 } }; 774 }); 775 ProtoBufSerializer serializer = new ProtoBufSerializer(); 776 serializer.Serialize(test(), tempFile); 777 object o = serializer.Deserialize(tempFile); 778 SimpleClass[] result = (SimpleClass[])o; 779 Assert.AreEqual(test()[0].x, result[0].x); 780 Assert.AreEqual(test()[0].y, result[0].y); 781 Assert.AreEqual(test()[1].x, result[1].x); 782 Assert.AreEqual(test()[1].y, result[1].y); 783 Assert.AreEqual(test()[2].x, result[2].x); 784 Assert.AreEqual(test()[2].y, result[2].y); 785 786 string msg = Profile(test); 787 Console.WriteLine(msg); 788 } 789 790 [TestMethod] 791 [TestCategory("Persistence4")] 792 [TestProperty("Time", "short")] 793 public void TestIntValueArray() { 794 var test = new Func<IntValue[]>(() => { return new[] { new IntValue(41), new IntValue(22), new IntValue(13) }; }); 795 ProtoBufSerializer serializer = new ProtoBufSerializer(); 796 serializer.Serialize(test(), tempFile); 797 object o = serializer.Deserialize(tempFile); 798 IntValue[] result = (IntValue[])o; 799 Assert.AreEqual(test()[0], result[0]); 800 Assert.AreEqual(test()[1], result[1]); 801 Assert.AreEqual(test()[2], result[2]); 802 803 string msg = Profile(test); 804 Console.WriteLine(msg); 805 } 354 806 #endregion 355 807
Note: See TracChangeset
for help on using the changeset viewer.