Showing posts with label frameworks. Show all posts
Showing posts with label frameworks. Show all posts

Monday, March 13, 2023

Tools for PB-scale data warehouses management - By Kapil Sharma

Managing PB-scale data warehouses requires a combination of tools for data processing, storage, monitoring, and governance. Here are some popular tools for managing PB-scale data warehouses:

Apache Hadoop: Hadoop is a distributed computing platform that enables the storage and processing of large datasets. It includes Hadoop Distributed File System (HDFS) for storage and MapReduce for distributed processing.

Apache Spark: Spark is an open-source distributed computing platform that provides an interface for programming distributed data processing pipelines. It includes a unified engine for big data processing, real-time streaming, machine learning, and graph processing.

Apache Flink: Flink is a distributed computing platform that provides real-time data processing capabilities. It includes a streaming engine for continuous processing, a batch processing engine for offline processing, and a machine learning library.

Amazon Redshift: Redshift is a cloud-based data warehouse service provided by Amazon Web Services (AWS). It enables the storage and analysis of large datasets using distributed computing and columnar storage.

Google BigQuery: BigQuery is a cloud-based data warehouse service provided by Google Cloud Platform. It enables the storage and analysis of large datasets using a serverless architecture and columnar storage.

Apache Cassandra: Cassandra is a distributed database that enables the storage and retrieval of large amounts of structured and unstructured data. It provides scalability, availability, and fault tolerance.

Apache Kafka: Kafka is a distributed streaming platform that enables the collection, storage, and processing of large streams of data in real-time.

Apache NiFi: NiFi is a data flow management tool that enables the collection, processing, and distribution of data across multiple systems.

Tableau: Tableau is a data visualization and analytics tool that enables users to create interactive dashboards and visualizations from large datasets.

Apache Atlas: Atlas is a data governance and metadata management tool that enables the management of data lineage, data classification, and data security policies.

These are just a few examples of the many tools available for managing PB-scale data warehouses. The choice of tools will depend on the specific needs and requirements of the organization.

Management of PB-scale data warehouses - Kapil Sharma

Managing PB-scale data warehouses requires a combination of technical expertise, efficient processes, and effective tools. Here are some best practices for managing PB-scale data warehouses:

Data modeling: A well-designed data model is essential for a PB-scale data warehouse. It should be optimized for performance, scalability, and flexibility. Consider using a star schema or snowflake schema to organize the data for fast querying and efficient storage.

Distributed computing: PB-scale data warehouses require distributed computing to handle the large volume of data. Use technologies such as Hadoop, Spark, or Apache Flink to distribute the data and processing across multiple servers.

Data partitioning: Partitioning is the process of breaking up data into smaller chunks and distributing them across multiple servers. Partitioning helps to improve performance and scalability by enabling parallel processing and reducing data movement.

Compression: Compressing data can help to reduce storage costs and improve query performance. Consider using compression techniques such as gzip or snappy to compress data.

Data governance: Implement data governance processes to ensure data quality, security, and compliance. This includes establishing data policies, defining data standards, and monitoring data quality.

Monitoring and optimization: Monitor the performance of the PB-scale data warehouse regularly and optimize it as needed. Use tools such as monitoring dashboards, performance tuning, and query optimization to identify and address performance issues.

Disaster recovery and backup: Develop a disaster recovery and backup plan to ensure the availability and reliability of the data warehouse. This includes regular backups, data replication, and testing of the recovery plan.

Managing a PB-scale data warehouse is a complex and challenging task. It requires a team of experts with knowledge of data architecture, distributed computing, big data technologies, and data governance. By following best practices, organizations can effectively manage their PB-scale data warehouses and leverage the data to gain insights and make informed decisions.


PB-Scale data warehouse - By Kapil Sharma

A PB-scale data warehouse is a data warehousing system that can store and process petabytes (PB) of data. This means that it can handle extremely large datasets, making it suitable for companies that need to manage and analyze vast amounts of data.

Data warehouses are designed to store and manage large amounts of data from various sources, and to provide users with the ability to analyze that data to gain insights and make informed decisions. PB-scale data warehouses take this to the next level, with the ability to handle data at a much larger scale than traditional data warehouses.

PB-scale data warehouses typically use distributed computing and storage technologies to handle the large volume of data. This involves breaking up the data into smaller chunks and storing them across multiple servers, which allows for parallel processing and faster query response times.

PB-scale data warehouses are often used by large enterprises that generate and collect massive amounts of data, such as social media platforms, e-commerce companies, and financial institutions. They enable these companies to perform complex data analysis and generate insights at a scale that was previously impossible.

However, building and managing a PB-scale data warehouse is a complex and challenging task. It requires expertise in data architecture, distributed computing, and big data technologies. Additionally, the cost of storing and processing PB-scale data can be significant, as it often requires a large infrastructure and specialized hardware.

CoreML Machine learning framework - By Kapil Sharma

 CoreML is a framework developed by Apple that allows developers to integrate machine learning models into their iOS, macOS, and tvOS applications. With CoreML, developers can add machine learning capabilities to their apps without requiring extensive knowledge of machine learning or data science.

CoreML supports a wide range of machine learning models, including neural networks, decision trees, and support vector machines. These models can be trained using a variety of popular machine learning libraries, such as TensorFlow, Keras, and scikit-learn.

Using CoreML, developers can perform tasks such as image and video analysis, natural language processing, and predictive modeling. The framework provides a set of pre-built models that can be used out of the box, as well as tools for converting custom models to the CoreML format.

CoreML is designed to be efficient and optimized for use on mobile devices, with features such as automatic model optimization and hardware acceleration. This means that machine learning models can be run directly on the device, without requiring a connection to a cloud-based service.

Overall, CoreML makes it easier for developers to incorporate machine learning into their applications, allowing them to build more intelligent and personalized experiences for their users.

How to productionizing models and implementing machine learning - By Kapil Sharma

Productionizing machine learning models involves taking a trained model and integrating it into a larger system, so that it can be used in real-world applications. Here are some steps to consider when productionizing machine learning models:

Data collection and preprocessing: Collecting and preprocessing the data is an essential step in building any machine learning model. You need to ensure that the data is properly cleaned, normalized, and transformed into the appropriate format for the model.

Model training: Once the data is preprocessed, you can train your machine learning model. This involves selecting the appropriate algorithm, hyperparameters, and training the model on the available data.

Model evaluation: After training, it's important to evaluate the model's performance on a validation dataset. This will help you to identify any issues with the model and tune it for better performance.

Model deployment: Once the model has been trained and evaluated, you need to deploy it in a production environment. This involves integrating the model with the rest of the system, creating an API to interact with the model, and setting up monitoring to ensure that the model is performing as expected.

Model maintenance: Machine learning models require ongoing maintenance to ensure that they continue to perform well. This involves monitoring the model's performance, retraining the model on new data, and updating the model as needed to keep up with changing requirements.

Here are some additional tips to keep in mind when implementing machine learning:

Start small: It's important to start with small, manageable projects when implementing machine learning. This will help you to learn the basics and avoid getting overwhelmed.

Use existing libraries and frameworks: There are many existing machine learning libraries and frameworks available, such as TensorFlow, PyTorch, and scikit-learn. These can save you time and effort when implementing machine learning.

Consider scalability: When implementing machine learning, it's important to consider scalability. Make sure that your system can handle large amounts of data and can scale up as needed.

Choose the right algorithm: There are many different machine learning algorithms to choose from, each with its own strengths and weaknesses. Make sure to choose the algorithm that is best suited to your specific use case.

Keep learning: Machine learning is a rapidly evolving field, and there are always new techniques and technologies to learn. Keep up with the latest developments to stay at the forefront of the field.

Monday, July 11, 2016

Testing Your Go App: Get Started The Right Way

When learning anything new, it’s important to have a fresh state of mind.
If you’re fairly new to Go and are coming from languages such as JavaScript or Ruby, you are likely accustomed to using existing frameworks that help you mock, assert, and do other testing wizardry.
Now eradicate the idea of reliance on external dependencies or frameworks! Testing was the first impediment I stumbled upon when learning this remarkable programming language a couple of years ago, a time when there were far fewer resources available.
I now know that testing success in Go means traveling light on dependencies (as with all things Go), relying minimally on external libraries, and writing good re-usable code. This presentation of Blake Mizerany’s experiences venturing forth with third party testing libraries is a great start to adjusting your mindset. You will see some good arguments about using external libraries and frameworks versus doing it “the Go way”.
It may seem counter-intuitive to build your own testing framework and mocking concepts, but is easier than one would think, and a good starting point for learning the language. Plus, unlike when I was learning, you have this article to guide you through common testing scenarios as well as introduce techniques that I consider best practices for efficiently testing and keeping code clean.
Do things “the Go Way”, eradicate dependencies on external frameworks.

Table Testing in Go

The basic testing unit - of ‘unit testing’ fame - can be any component of a program in its simplest form which takes an input and returns an output. Let’s take a look at a simple function we’d like to write tests for. It is nowhere near perfect or complete, but it’s good enough for demonstration purposes:
avg.go
func Avg(nos ...int) int {
 sum := 0
 for _, n := range nos {
  sum += n
 }
 if sum == 0 {
  return 0
 }
 return sum / len(nos)
}
The function above, func Avg(nos ...int), returns either zero or the integer average of a series of numbers that are given to it. Now let’s write a test for it.
In Go, it is considered best practice to name a test file with the same name as the file which contains the code being tested, with the added suffix _test. For example, the above code is in a file named avg.go, so our test file will be named avg_test.go.
Note that these examples are only excerpts of actual files, as the package definition and imports have been omitted for simplicity.
Here’s a test for the Avg function:
avg__test.go
func TestAvg(t *testing.T) {
 for _, tt := range []struct {
  Nos    []int
  Result int
 }{
  {Nos: []int{2, 4}, Result: 3},
  {Nos: []int{1, 2, 5}, Result: 2},
  {Nos: []int{1}, Result: 1},
  {Nos: []int{}, Result: 0},
  {Nos: []int{2, -2}, Result: 0},
 } {
  if avg := Average(tt.Nos...); avg != tt.Result {
   t.Fatalf("expected average of %v to be %d, got %d\n", tt.Nos, tt.Result, avg)
  }
 }
}
There are several things to note about the function definition:
  • First, the prefix of ‘Test’ on the test function name. This is necessary so that the tool will pick it up as a valid test.
  • The latter part of the function name is generally the name of the function or method being tested, in this case Avg.
  • We also need to pass in the testing structure called testing.T, which allows for control of the test’s flow. For more details on this API, please visit the documentation page.
Now let’s talk about the form in which the example is written. A test suite (a series of tests) is being run through the function Avg(), and each test contains a specific input and the expected output. In our case, each test sends in a slice of integers (Nos) and expects a specific return value (Result).
Table testing gets its name from its structure, easily represented by a table with two columns: the input variable and the expected output variable.

Golang Interface Mocking

One of the greatest and most powerful feature that the Go language has to offer is called an interface. Besides the power and flexibility that we get from interfacing when architecting our programs, interfacing also gives us amazing opportunities to decouple our components and test them thoroughly at their meeting point.
An interface is a named collection of methods, but also a variable type.
Let’s take an imaginary scenario where we need to read the first N bytes from an io.Reader and return them as a string. It would look something like this:
readn.go
// readN reads at most n bytes from r and returns them as a string.
func readN(r io.Reader, n int) (string, error) {
 buf := make([]byte, n)
 m, err := r.Read(buf)
 if err != nil {
  return "", err
 }
 return string(buf[:m]), nil
}
Obviously, the main thing to test is that the function readN, when given various input, returns the correct output. This can be done with table testing. But there are two other non-trivial aspects we should cover, which are checking that:
  • r.Read is called with a buffer of size n.
  • r.Read returns an error if one is thrown.
In order to know the size of the buffer that is passed to r.Read, as well as take control of the error that it returns, we need to mock the r being passed to readN. If we look at the Go documentation on type Reader, we see what io.Reader looks like:
type Reader interface {
    Read(p []byte) (n int, err error)
}
That seems rather easy. All we have to do in order to satisfy io.Reader is have our mock own a Readmethod. So our ReaderMock can be as follows:
type ReaderMock struct {
 ReadMock func([]byte) (int, error)
}

func (m ReaderMock) Read(p []byte) (int, error) {
 return m.ReadMock(p)
}
Let’s analyze the above code for a little bit. Any instance of ReaderMock clearly satisfies the io.Readerinterface because it implements the necessary Read method. Our mock also contains the field ReadMock, allowing us to set the exact behavior of the mocked method, which makes it super easy for us to dynamically instantiate whatever we need.
A great memory-free trick for ensuring that the interface is satisfied at run time is to insert the following into our code:
var _ io.Reader = (*MockReader)(nil)
This checks the assertion but doesn’t allocate anything, which lets us make sure that the interface is correctly implemented at compile time, before the program actually runs into any functionality using it. An optional trick, but helpful.
Moving on, let’s write our first test, in which r.Read is called with a buffer of size n. To do this, we use our ReaderMock as follows:
func TestReadN_bufSize(t *testing.T) {
 total := 0
 mr := &MockReader{func(b []byte) (int, error) {
  total = len(b)
  return 0, nil
 }}
 readN(mr, 5)
 if total != 5 {
  t.Fatalf("expected 5, got %d", total)
 }
}
As you can see above, we’ve defined the behavior for the Read function of our “fake” io.Reader with a scope variable, which can be later used to assert the validity of our test. Easy enough.
Let’s look at the second scenario we need to test, which requires us to mock Read to return an error:
func TestReadN_error(t *testing.T) {
 expect := errors.New("some non-nil error")
 mr := &MockReader{func(b []byte) (int, error) {
  return 0, expect
 }}
 _, err := readN(mr, 5)
 if err != expect {
  t.Fatal("expected error")
 }
}
In the above testing, any call to mr.Read (our mocked Reader) will return the defined error, thus it is safe to assume that the correct functioning of readN will do the same.

Function Mocking with Go

It isn’t often that we need to mock a function, because we tend to use structures and interfaces instead. These are easier to control, but occasionally we can bump into this necessity, and I frequently see confusion around the topic. Some people have even asked how to mock things like log.Println. Although it is rarely the case that we need to test input given to log.Println, we will use this opportunity to demonstrate.
Consider this simple if statement below that logs output depending on the value of n:
func printSize(n int) {
 if n < 10 {
  log.Println("SMALL")
 } else {
  log.Println("LARGE")
 }
}
In the above example, we assume the ridiculous scenario where we specifically test that log.Println is called with the correct values. In order for us to mock this function, we have to wrap it inside our own first:
var show = func(v ...interface{}) {
 log.Println(v...)
}
Declaring the function in this manner - as a variable - allows us to overwrite it in our tests and assign whatever behavior we want to it. Implicitly, lines referring to log.Println are replaced with show, so our program becomes:
func printSize(n int) {
 if n < 10 {
  show("SMALL")
 } else {
  show("LARGE")
 }
}
Now we can test:
func TestPrintSize(t *testing.T) {
 var got string
 oldShow := show
 show = func(v ...interface{}) {
  if len(v) != 1 {
   t.Fatalf("expected show to be called with 1 param, got %d", len(v))
  }
  var ok bool
  got, ok = v[0].(string)
  if !ok {
   t.Fatal("expected show to be called with a string")
  }
 }

 for _, tt := range []struct{
  N int
  Out string
 }{
  {2, "SMALL"},
  {3, "SMALL"},
  {9, "SMALL"},
  {10, "LARGE"},
  {11, "LARGE"},
  {100, "LARGE"},
 } {
  got = ""
  printSize(tt.N)
  if got != tt.Out {
   t.Fatalf("on %d, expected '%s', got '%s'\n", tt.N, tt.Out, got)
  }
 }

 // careful though, we must not forget to restore it to its original value
 // before finishing the test, or it might interfere with other tests in our
 // suite, giving us unexpected and hard to trace behavior.
 show = oldShow
}
Our takeaway shouldn’t be ‘mock log.Println’, but that in those very occasional scenarios when we do need to mock a package-level function for legitimate reasons, the only way to do so (as far as I am aware) is by declaring it as a package-level variable so that we can take control of its value.
However, if we ever do need to mock things like log.Println, a much more elegant solution can be written if we were to use a custom logger.

Go Template Rendering Tests

Another fairly common scenario is to test that the output of a rendered template is according to expectations. Let’s consider a GET request to http://localhost:3999/welcome?name=Frank, which returns the following body:
<html>
 <head><title>Welcome page</title></head>
 <body>
  <h1 class="header-name">
   Welcome <span class="name">Frank</span>!
  </h1>
 </body>
</html>
In case it wasn’t obvious enough by now, it’s not a coincidence that the query parameter name matches the content of the span classed as “name”. In this case, the obvious test would be to verify that this happens correctly every time across multiple outputs. I found the GoQuery library to be immensely helpful here.
GoQuery uses a jQuery-like API to query an HTML structure, which is indispensable for testing the validity of the markup output of your programs.
Now we can write our test in this manner:
welcome__test.go
func TestWelcome_name(t *testing.T) {
 resp, err := http.Get("http://localhost:3999/welcome?name=Frank")
 if err != nil {
  t.Fatal(err)
 }
 if resp.StatusCode != http.StatusOK {
  t.Fatalf("expected 200, got %d", resp.StatusCode)
 }
 doc, err := goquery.NewDocumentFromResponse(resp)
 if err != nil {
  t.Fatal(err)
 }
 if v := doc.Find("h1.header-name span.name").Text(); v != "Frank" {
  t.Fatalf("expected markup to contain 'Frank', got '%s'", v)
 }
}
First, we check that the response code was 200/OK before proceeding.
I believe that it’s not too far-fetched to assume that the rest of the code snippet above is self-explanatory: we retrieve the URL using the http package and create a new goquery-compatible document from the response, which we then use to query the DOM that was returned. We check that the span.name inside h1.header-name encapsulates the text ‘Frank’.

Testing JSON APIs

Go is frequently used to write APIs of some sort, so last but not least, let’s look into some high-level ways of testing JSON APIs.
Consider if the endpoint previously returned JSON instead of HTML, so from http://localhost:3999/welcome.json?name=Frank we would expect the response body to look something like:
{"Salutation": "Hello Frank!"}
Asserting JSON responses, as one might have already guessed, is not much different from asserting template responses, with the exception that we don’t need any external libraries or dependencies. Go’s standard libraries are sufficient. Here is our test confirming that the correct JSON is returned for the given parameters:
welcome__test.go
func TestWelcome_name_JSON(t *testing.T) {
 resp, err := http.Get("http://localhost:3999/welcome.json?name=Frank")
 if err != nil {
  t.Fatal(err)
 }
 if resp.StatusCode != 200 {
  t.Fatalf("expected 200, got %d", resp.StatusCode)
 }
 var dst struct{ Salutation string }
 if json.NewDecoder(resp.Body).Decode(&dst); err != nil {
  t.Fatal(err)
 }
 if dst.Salutation != "Hello Frank!" {
  t.Fatalf("expected 'Hello Frank!', got '%s'", dst.Salutation)
 }
}
If anything other than the structure that we decode against would be returned, json.NewDecoder will instead return an error and the test will fail. Considering that the response decodes against the structure successfully, we check that the contents of the field are as expected - in our case “Hello Frank!”.

Setup & Teardown

Testing with Go is easy, but there is one problem with both the JSON test above and the template rendering test before that. They both assume that the server is running, and this creates an unreliable dependency. Also, it’s not a great idea to go against a “live” server.
It’s never a good idea to test against “live” data on a “live” production server; spin up local or development copies so there’s no damage done with things go horribly wrong.
Luckily, Go offers the httptest package to create test servers. Tests spark up their own separate server, independent from our main one, and so testing won’t interfere with production.
In these cases it’s ideal to create generic setup and teardown functions to be called by all tests requiring a running server. Following this new, safer pattern, our tests would end up looking something like this:
func setup() *httptest.Server {
 return httptest.NewServer(app.Handler())
}

func teardown(s *httptest.Server) {
 s.Close()
}

func TestWelcome_name(t *testing.T) {
 srv := setup()

 url := fmt.Sprintf("%s/welcome.json?name=Frank", srv.URL)
 resp, err := http.Get(url)
 // verify errors & run assertions as usual

 teardown(srv)
}
Note the app.Handler() reference. This is a best practice function that returns the application’s http.Handler, which can instantiate either your production server or a test server.

Conclusion

Testing in Go is a great opportunity to assume the outer perspective of your program and take on the shoes of your visitors, or in most cases, the users of your API. It provides the great opportunity to make sure you are both delivering good code and a quality experience.
Whenever you’re unsure of the more complex functionalities in your code, testing comes in handy as a reassurance, and also guarantees that the pieces will continue to work together well when modifying parts of larger systems.
I hope this article was of use to you, and you’re welcome to comment if you know of any other testing tricks.
This article was written by Gabriel Aszalos, a Toptal Javascript developer.

Navigating Portfolio Management Services (PMS): Evaluation, Strategy, and Return Dynamics.

  What is Portfolio Management Services (PMS)? Portfolio Management Services (PMS) are customized, professional investment solutions designe...